You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.8 KiB
54 lines
1.8 KiB
using System.Data;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using Dapper;
|
|
using NzbDrone.Common.Serializer;
|
|
|
|
namespace NzbDrone.Core.Datastore.Converters
|
|
{
|
|
public class EmbeddedDocumentConverter<T> : SqlMapper.TypeHandler<T>
|
|
{
|
|
protected readonly JsonSerializerOptions SerializerSettings;
|
|
|
|
public EmbeddedDocumentConverter()
|
|
{
|
|
var serializerSettings = new JsonSerializerOptions
|
|
{
|
|
AllowTrailingCommas = true,
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
|
PropertyNameCaseInsensitive = true,
|
|
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
WriteIndented = true
|
|
};
|
|
|
|
serializerSettings.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase, true));
|
|
serializerSettings.Converters.Add(new STJTimeSpanConverter());
|
|
serializerSettings.Converters.Add(new STJUtcConverter());
|
|
|
|
SerializerSettings = serializerSettings;
|
|
}
|
|
|
|
public EmbeddedDocumentConverter(params JsonConverter[] converters)
|
|
: this()
|
|
{
|
|
foreach (var converter in converters)
|
|
{
|
|
SerializerSettings.Converters.Add(converter);
|
|
}
|
|
}
|
|
|
|
public override void SetValue(IDbDataParameter parameter, T value)
|
|
{
|
|
// Cast to object to get all properties written out
|
|
// https://github.com/dotnet/corefx/issues/38650
|
|
parameter.Value = JsonSerializer.Serialize((object)value, SerializerSettings);
|
|
}
|
|
|
|
public override T Parse(object value)
|
|
{
|
|
return JsonSerializer.Deserialize<T>((string)value, SerializerSettings);
|
|
}
|
|
}
|
|
}
|