json serializer updates.

pull/4/head
Keivan Beigi 12 years ago
parent 67995a5cf6
commit f9bb4178ed

@ -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 @@ namespace NzbDrone.Common
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 @@ namespace NzbDrone.Common
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 @@ namespace NzbDrone.Common
_jsonNetSerializer.Serialize(jsonTextWriter, model); _jsonNetSerializer.Serialize(jsonTextWriter, model);
jsonTextWriter.Flush(); jsonTextWriter.Flush();
} }
} }
} }

@ -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>

@ -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>

@ -3,6 +3,7 @@ using System.Collections.Generic;
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 @@ namespace NzbDrone.Core.Test.Datastore
[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());
} }

@ -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>

@ -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" />

@ -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 @@ namespace NzbDrone.Core.DataAugmentation.DailySeries
{ {
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 @@ namespace NzbDrone.Core.DataAugmentation.DailySeries
{ {
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;
} }

@ -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 @@ namespace NzbDrone.Core.DataAugmentation.Scene
{ {
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);
} }
} }
} }

@ -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 @@ namespace NzbDrone.Core.Datastore.Converters
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;
} }

@ -3,8 +3,8 @@ using System.Collections.Generic;
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 @@ namespace NzbDrone.Core.Datastore
.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)
{ {

@ -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>

@ -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>

@ -74,6 +74,7 @@ namespace NzbDrone.Test.Common
Mocker.SetConstant(LogManager.GetLogger("TestLogger")); Mocker.SetConstant(LogManager.GetLogger("TestLogger"));
Mocker.SetConstant<IJsonSerializer>(new JsonSerializer());
LogManager.ReconfigExistingLoggers(); LogManager.ReconfigExistingLoggers();

Loading…
Cancel
Save