1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-21 01:49:57 +02:00
Sonarr/NzbDrone.Core/Datastore/Converters/EmbeddedDocumentConverter.cs

76 lines
1.8 KiB
C#
Raw Normal View History

using System;
using Marr.Data.Converters;
using Marr.Data.Mapping;
2013-05-12 18:18:17 +03:00
using NzbDrone.Common.Serializer;
namespace NzbDrone.Core.Datastore.Converters
{
public class ProviderSettingConverter : EmbeddedDocumentConverter
{
public override object FromDB(ConverterContext context)
{
if (context.DbValue == DBNull.Value)
{
return DBNull.Value;
}
2013-04-18 02:32:06 +03:00
var stringValue = (string)context.DbValue;
if (string.IsNullOrWhiteSpace(stringValue))
{
return null;
}
var ordinal = context.DataRecord.GetOrdinal("ConfigContract");
var implementation = context.DataRecord.GetString(ordinal);
var impType = Type.GetType(implementation, true, true);
return Json.Deserialize(stringValue, impType);
}
}
public class EmbeddedDocumentConverter : IConverter
{
public virtual object FromDB(ConverterContext context)
{
if (context.DbValue == DBNull.Value)
{
return DBNull.Value;
}
var stringValue = (string)context.DbValue;
if (string.IsNullOrWhiteSpace(stringValue))
{
return null;
}
return Json.Deserialize(stringValue, context.ColumnMap.FieldType);
}
public object FromDB(ColumnMap map, object dbValue)
{
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
}
public object ToDB(object clrValue)
{
if (clrValue == null) return null;
return clrValue.ToJson();
}
public Type DbType
{
get
{
return typeof(string);
}
}
}
}