1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-05 02:22:31 +01:00

json serializer updates.

This commit is contained in:
Keivan Beigi 2013-04-17 16:32:06 -07:00
parent 67995a5cf6
commit f9bb4178ed
13 changed files with 46 additions and 37 deletions

View File

@ -1,4 +1,5 @@
using System.IO; using System;
using System.IO;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
@ -9,6 +10,7 @@ public interface IJsonSerializer
T Deserialize<T>(string json) where T : class, new(); T Deserialize<T>(string json) where T : class, new();
string Serialize(object obj); string Serialize(object obj);
void Serialize<TModel>(TModel model, Stream outputStream); void Serialize<TModel>(TModel model, Stream outputStream);
object Deserialize(string json, Type type);
} }
public class JsonSerializer : IJsonSerializer public class JsonSerializer : IJsonSerializer
@ -42,6 +44,11 @@ public JsonSerializer()
return JsonConvert.DeserializeObject<T>(json); return JsonConvert.DeserializeObject<T>(json);
} }
public object Deserialize(string json, Type type)
{
return JsonConvert.DeserializeObject(json, type);
}
public string Serialize(object obj) public string Serialize(object obj)
{ {
return JsonConvert.SerializeObject(obj); return JsonConvert.SerializeObject(obj);
@ -54,5 +61,7 @@ public void Serialize<TModel>(TModel model, Stream outputStream)
_jsonNetSerializer.Serialize(jsonTextWriter, model); _jsonNetSerializer.Serialize(jsonTextWriter, model);
jsonTextWriter.Flush(); jsonTextWriter.Flush();
} }
} }
} }

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -67,7 +67,7 @@
</Reference> </Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.5.0.2\lib\net35\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.5.0.3\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL"> <Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="DotNetZip" version="1.9.1.8" targetFramework="net40" /> <package id="DotNetZip" version="1.9.1.8" targetFramework="net40" />
<package id="Nancy" version="0.16.1" targetFramework="net40" /> <package id="Nancy" version="0.16.1" targetFramework="net40" />
<package id="Nancy.Hosting.Self" version="0.16.1" targetFramework="net40" /> <package id="Nancy.Hosting.Self" version="0.16.1" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.2" targetFramework="net40" /> <package id="Newtonsoft.Json" version="5.0.3" targetFramework="net35" />
<package id="NLog" version="2.0.1.2" targetFramework="net40" /> <package id="NLog" version="2.0.1.2" targetFramework="net40" />
</packages> </packages>

View File

@ -3,6 +3,7 @@
using FluentAssertions; using FluentAssertions;
using Marr.Data; using Marr.Data;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Datastore.Converters; using NzbDrone.Core.Datastore.Converters;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
@ -40,8 +41,8 @@ public class TypeWithNoMappableProperties
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
MapRepository.Instance.RegisterTypeConverter(typeof(List<EmbeddedType>), new EmbeddedDocumentConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(List<EmbeddedType>), new EmbeddedDocumentConverter(new JsonSerializer()));
MapRepository.Instance.RegisterTypeConverter(typeof(EmbeddedType), new EmbeddedDocumentConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(EmbeddedType), new EmbeddedDocumentConverter(new JsonSerializer()));
MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter()); MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter());
} }

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -93,7 +93,7 @@
</Reference> </Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.5.0.2\lib\net35\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.5.0.3\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL"> <Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="AutoMoq" version="1.6.1" targetFramework="net40" /> <package id="AutoMoq" version="1.6.1" targetFramework="net40" />
<package id="CommonServiceLocator" version="1.0" targetFramework="net40" /> <package id="CommonServiceLocator" version="1.0" targetFramework="net40" />
@ -7,7 +7,7 @@
<package id="Moq" version="4.0.10827" /> <package id="Moq" version="4.0.10827" />
<package id="NBuilder" version="3.0.1.1" /> <package id="NBuilder" version="3.0.1.1" />
<package id="NCrunch.Framework" version="1.45.0.11" targetFramework="net40" /> <package id="NCrunch.Framework" version="1.45.0.11" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.2" targetFramework="net40" /> <package id="Newtonsoft.Json" version="5.0.3" targetFramework="net35" />
<package id="NLog" version="2.0.1.2" targetFramework="net40" /> <package id="NLog" version="2.0.1.2" targetFramework="net40" />
<package id="NUnit" version="2.6.2" targetFramework="net40" /> <package id="NUnit" version="2.6.2" targetFramework="net40" />
<package id="Prowlin" version="0.9.4456.26422" targetFramework="net40" /> <package id="Prowlin" version="0.9.4456.26422" targetFramework="net40" />

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using NLog; using NLog;
using Newtonsoft.Json;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
@ -17,12 +16,14 @@ public class DailySeriesDataProxy : IDailySeriesDataProxy
{ {
private readonly IHttpProvider _httpProvider; private readonly IHttpProvider _httpProvider;
private readonly IConfigService _configService; private readonly IConfigService _configService;
private readonly IJsonSerializer _jsonSerializer;
private readonly Logger _logger; private readonly Logger _logger;
public DailySeriesDataProxy(IHttpProvider httpProvider, IConfigService configService, Logger logger) public DailySeriesDataProxy(IHttpProvider httpProvider, IConfigService configService, IJsonSerializer jsonSerializer, Logger logger)
{ {
_httpProvider = httpProvider; _httpProvider = httpProvider;
_configService = configService; _configService = configService;
_jsonSerializer = jsonSerializer;
_logger = logger; _logger = logger;
} }
@ -32,7 +33,7 @@ public IEnumerable<int> GetDailySeriesIds()
{ {
var dailySeriesIds = _httpProvider.DownloadString(_configService.ServiceRootUrl + "/DailySeries/AllIds"); var dailySeriesIds = _httpProvider.DownloadString(_configService.ServiceRootUrl + "/DailySeries/AllIds");
var seriesIds = JsonConvert.DeserializeObject<List<int>>(dailySeriesIds); var seriesIds = _jsonSerializer.Deserialize<List<int>>(dailySeriesIds);
return seriesIds; return seriesIds;
} }

View File

@ -1,5 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using Newtonsoft.Json;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
@ -14,17 +13,19 @@ public class SceneMappingProxy : ISceneMappingProxy
{ {
private readonly IHttpProvider _httpProvider; private readonly IHttpProvider _httpProvider;
private readonly IConfigService _configService; private readonly IConfigService _configService;
private readonly IJsonSerializer _jsonSerializer;
public SceneMappingProxy(IHttpProvider httpProvider, IConfigService configService) public SceneMappingProxy(IHttpProvider httpProvider, IConfigService configService, IJsonSerializer jsonSerializer)
{ {
_httpProvider = httpProvider; _httpProvider = httpProvider;
_configService = configService; _configService = configService;
_jsonSerializer = jsonSerializer;
} }
public List<SceneMapping> Fetch() public List<SceneMapping> Fetch()
{ {
var mappingsJson = _httpProvider.DownloadString(_configService.ServiceRootUrl + "/SceneMapping/Active"); var mappingsJson = _httpProvider.DownloadString(_configService.ServiceRootUrl + "/SceneMapping/Active");
return JsonConvert.DeserializeObject<List<SceneMapping>>(mappingsJson); return _jsonSerializer.Deserialize<List<SceneMapping>>(mappingsJson);
} }
} }
} }

View File

@ -1,12 +1,19 @@
using System; using System;
using Marr.Data.Converters; using Marr.Data.Converters;
using Marr.Data.Mapping; using Marr.Data.Mapping;
using Newtonsoft.Json; using NzbDrone.Common;
namespace NzbDrone.Core.Datastore.Converters namespace NzbDrone.Core.Datastore.Converters
{ {
public class EmbeddedDocumentConverter : IConverter public class EmbeddedDocumentConverter : IConverter
{ {
private readonly IJsonSerializer _serializer;
public EmbeddedDocumentConverter(IJsonSerializer serializer)
{
_serializer = serializer;
}
public object FromDB(ColumnMap map, object dbValue) public object FromDB(ColumnMap map, object dbValue)
{ {
if (dbValue == DBNull.Value) if (dbValue == DBNull.Value)
@ -21,14 +28,14 @@ public object FromDB(ColumnMap map, object dbValue)
return null; return null;
} }
return JsonConvert.DeserializeObject(stringValue, map.FieldType); return _serializer.Deserialize(stringValue, map.FieldType);
} }
public object ToDB(object clrValue) public object ToDB(object clrValue)
{ {
if (clrValue == null) return null; if (clrValue == null) return null;
var json = JsonConvert.SerializeObject(clrValue); var json = _serializer.Serialize(clrValue);
return json; return json;
} }

View File

@ -3,8 +3,8 @@
using System.Linq; using System.Linq;
using Marr.Data; using Marr.Data;
using Marr.Data.Mapping; using Marr.Data.Mapping;
using NzbDrone.Common;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.DataAugmentation;
using NzbDrone.Core.DataAugmentation.Scene; using NzbDrone.Core.DataAugmentation.Scene;
using NzbDrone.Core.Datastore.Converters; using NzbDrone.Core.Datastore.Converters;
using NzbDrone.Core.ExternalNotification; using NzbDrone.Core.ExternalNotification;
@ -75,7 +75,7 @@ private static void RegisterEmbeddedConverter()
.Where(c => c.GetInterfaces().Any(i => i == typeof(IEmbeddedDocument))); .Where(c => c.GetInterfaces().Any(i => i == typeof(IEmbeddedDocument)));
var embeddedConvertor = new EmbeddedDocumentConverter(); var embeddedConvertor = new EmbeddedDocumentConverter(new JsonSerializer());
var genericListDefinition = typeof(List<>).GetGenericTypeDefinition(); var genericListDefinition = typeof(List<>).GetGenericTypeDefinition();
foreach (var embeddedType in embeddedTypes) foreach (var embeddedType in embeddedTypes)
{ {

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -144,7 +144,7 @@
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.5.0.2\lib\net35\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.5.0.3\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL"> <Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
@ -171,9 +171,6 @@
<Reference Include="System.Web.Extensions" /> <Reference Include="System.Web.Extensions" />
<Reference Include="System.XML" /> <Reference Include="System.XML" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="Twitterizer2, Version=2.4.0.26532, Culture=neutral, PublicKeyToken=69d1469eac671567, processorArchitecture=MSIL">
<HintPath>..\packages\twitterizer.2.4.0.26532\lib\net40\Twitterizer2.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="..\NzbDrone.Common\Properties\SharedAssemblyInfo.cs"> <Compile Include="..\NzbDrone.Common\Properties\SharedAssemblyInfo.cs">
@ -370,7 +367,6 @@
<Compile Include="Download\Clients\Sabnzbd\SabModel.cs" /> <Compile Include="Download\Clients\Sabnzbd\SabModel.cs" />
<Compile Include="Download\Clients\Sabnzbd\SabQueueItem.cs" /> <Compile Include="Download\Clients\Sabnzbd\SabQueueItem.cs" />
<Compile Include="Download\Clients\Sabnzbd\SabVersionModel.cs" /> <Compile Include="Download\Clients\Sabnzbd\SabVersionModel.cs" />
<Compile Include="Model\Twitter\TwitterAuthorizationModel.cs" />
<Compile Include="Model\Xbmc\ActionType.cs" /> <Compile Include="Model\Xbmc\ActionType.cs" />
<Compile Include="Model\Xbmc\ActivePlayersDharmaResult.cs" /> <Compile Include="Model\Xbmc\ActivePlayersDharmaResult.cs" />
<Compile Include="Model\Xbmc\ActivePlayersEdenResult.cs" /> <Compile Include="Model\Xbmc\ActivePlayersEdenResult.cs" />
@ -445,9 +441,6 @@
<Compile Include="ExternalNotification\Smtp.cs"> <Compile Include="ExternalNotification\Smtp.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="ExternalNotification\Twitter.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="ExternalNotification\Xbmc.cs"> <Compile Include="ExternalNotification\Xbmc.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
@ -498,9 +491,6 @@
<Compile Include="Providers\SmtpProvider.cs"> <Compile Include="Providers\SmtpProvider.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="Providers\TwitterProvider.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Providers\XbmcProvider.cs"> <Compile Include="Providers\XbmcProvider.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>

View File

@ -1,12 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="DotNetZip" version="1.9.1.8" /> <package id="DotNetZip" version="1.9.1.8" />
<package id="FluentMigrator" version="1.0.6.0" targetFramework="net40" /> <package id="FluentMigrator" version="1.0.6.0" targetFramework="net40" />
<package id="Growl" version="0.6" /> <package id="Growl" version="0.6" />
<package id="MediaInfoNet" version="0.3" targetFramework="net40" /> <package id="MediaInfoNet" version="0.3" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.2" targetFramework="net40" /> <package id="Newtonsoft.Json" version="5.0.3" targetFramework="net35" />
<package id="NLog" version="2.0.1.2" targetFramework="net40" /> <package id="NLog" version="2.0.1.2" targetFramework="net40" />
<package id="Prowlin" version="0.9.4456.26422" targetFramework="net40" /> <package id="Prowlin" version="0.9.4456.26422" targetFramework="net40" />
<package id="RestSharp" version="104.1" targetFramework="net40" /> <package id="RestSharp" version="104.1" targetFramework="net40" />
<package id="twitterizer" version="2.4.0.26532" />
</packages> </packages>

View File

@ -74,6 +74,7 @@ public void TestBaseSetup()
Mocker.SetConstant(LogManager.GetLogger("TestLogger")); Mocker.SetConstant(LogManager.GetLogger("TestLogger"));
Mocker.SetConstant<IJsonSerializer>(new JsonSerializer());
LogManager.ReconfigExistingLoggers(); LogManager.ReconfigExistingLoggers();