1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Core/Datastore/Converters/EmbeddedDocumentConverter.cs

50 lines
1.1 KiB
C#
Raw Normal View History

using System;
using Marr.Data.Converters;
using Marr.Data.Mapping;
2013-04-18 02:32:06 +03:00
using NzbDrone.Common;
namespace NzbDrone.Core.Datastore.Converters
{
public class EmbeddedDocumentConverter : IConverter
{
2013-04-18 02:32:06 +03:00
private readonly IJsonSerializer _serializer;
public EmbeddedDocumentConverter(IJsonSerializer serializer)
{
_serializer = serializer;
}
public object FromDB(ColumnMap map, object dbValue)
{
if (dbValue == DBNull.Value)
{
return DBNull.Value;
}
var stringValue = (string)dbValue;
if (string.IsNullOrWhiteSpace(stringValue))
{
return null;
}
2013-04-18 02:32:06 +03:00
return _serializer.Deserialize(stringValue, map.FieldType);
}
public object ToDB(object clrValue)
{
if (clrValue == null) return null;
2013-04-18 02:32:06 +03:00
var json = _serializer.Serialize(clrValue);
return json;
}
public Type DbType
{
get
{
return typeof(string);
}
}
}
}