added an abstraction layer for json serializer, should work in mono.

pull/4/head
Keivan Beigi 12 years ago
parent 213c842050
commit 65ae894410

@ -1,8 +1,6 @@
using System;
using System.Linq;
using Nancy;
using Nancy.Responses;
using Newtonsoft.Json;
using NzbDrone.Api.Extensions;
namespace NzbDrone.Api.ErrorManagement
@ -32,7 +30,7 @@ namespace NzbDrone.Api.ErrorManagement
if (content != null)
{
result = result + " :" + JsonConvert.SerializeObject(content);
result = result + " :" + content;
}
return result;

@ -1,14 +1,19 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Nancy;
using Newtonsoft.Json;
using NzbDrone.Common;
namespace NzbDrone.Api.Extensions
{
public class NancyJsonSerializer : ISerializer
{
public readonly static NancyJsonSerializer Instance = new NancyJsonSerializer();
private readonly IJsonSerializer _jsonSerializer;
public NancyJsonSerializer(IJsonSerializer jsonSerializer)
{
_jsonSerializer = jsonSerializer;
}
public bool CanSerialize(string contentType)
{
@ -17,9 +22,7 @@ namespace NzbDrone.Api.Extensions
public void Serialize<TModel>(string contentType, TModel model, Stream outputStream)
{
var jsonTextWriter = new JsonTextWriter(new StreamWriter(outputStream));
Serializer.Instance.Serialize(jsonTextWriter, model);
jsonTextWriter.Flush();
_jsonSerializer.Serialize(model, outputStream);
}
public IEnumerable<string> Extensions { get; private set; }

@ -1,25 +1,26 @@
using System.IO;
using System.Linq;
using Nancy;
using Nancy.Responses;
using Newtonsoft.Json;
using NzbDrone.Common;
namespace NzbDrone.Api.Extensions
{
public static class JsonExtensions
{
public static T FromJson<T>(this Stream body)
private static readonly JsonSerializer Serializer = new JsonSerializer();
private static readonly NancyJsonSerializer NancySerializer = new NancyJsonSerializer(Serializer);
public static T FromJson<T>(this Stream body) where T : class, new()
{
var reader = new StreamReader(body, true);
body.Position = 0;
var value = reader.ReadToEnd();
return JsonConvert.DeserializeObject<T>(value, Serializer.Settings);
return Serializer.Deserialize<T>(value);
}
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
{
var jsonResponse = new JsonResponse<TModel>(model, new NancyJsonSerializer()) { StatusCode = statusCode };
return jsonResponse;
return new JsonResponse<TModel>(model, NancySerializer) { StatusCode = statusCode };
}
}
}

@ -1,33 +0,0 @@
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace NzbDrone.Api.Extensions
{
public static class Serializer
{
static Serializer()
{
Settings = new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented,
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate
};
Instance = new JsonSerializer
{
DateTimeZoneHandling = Settings.DateTimeZoneHandling,
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented,
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate,
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
}
public static JsonSerializerSettings Settings { get; private set; }
public static JsonSerializer Instance { get; private set; }
}
}

@ -66,10 +66,6 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Nancy.0.16.1\lib\net40\Nancy.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.5.0.2\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NLog.2.0.1.2\lib\net40\NLog.dll</HintPath>
@ -86,7 +82,6 @@
<Compile Include="Episodes\EpisodeModule.cs" />
<Compile Include="Episodes\EpisodeResource.cs" />
<Compile Include="Extensions\NancyJsonSerializer.cs" />
<Compile Include="Extensions\Serializer.cs" />
<Compile Include="Frontend\IndexModule.cs" />
<Compile Include="Frontend\StaticResourceProvider.cs" />
<Compile Include="Frontend\StaticResourceMapper.cs" />

@ -3,6 +3,5 @@
<package id="AutoMapper" version="2.2.1" targetFramework="net40" />
<package id="FluentValidation" version="3.4.6.0" targetFramework="net40" />
<package id="Nancy" version="0.16.1" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.2" targetFramework="net40" />
<package id="NLog" version="2.0.1.2" targetFramework="net40" />
</packages>

@ -0,0 +1,58 @@
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace NzbDrone.Common
{
public interface IJsonSerializer
{
T Deserialize<T>(string json) where T : class, new();
string Serialize(object obj);
void Serialize<TModel>(TModel model, Stream outputStream);
}
public class JsonSerializer : IJsonSerializer
{
private readonly Newtonsoft.Json.JsonSerializer _jsonNetSerializer;
public JsonSerializer()
{
var setting = new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented,
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate
};
_jsonNetSerializer = new Newtonsoft.Json.JsonSerializer()
{
DateTimeZoneHandling = setting.DateTimeZoneHandling,
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented,
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate,
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
}
public T Deserialize<T>(string json) where T : class, new()
{
return JsonConvert.DeserializeObject<T>(json);
}
public string Serialize(object obj)
{
return JsonConvert.SerializeObject(obj);
}
public void Serialize<TModel>(TModel model, Stream outputStream)
{
var jsonTextWriter = new JsonTextWriter(new StreamWriter(outputStream));
_jsonNetSerializer.Serialize(jsonTextWriter, model);
jsonTextWriter.Flush();
}
}
}

@ -67,7 +67,7 @@
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.5.0.2\lib\net40\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.5.0.2\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
@ -115,6 +115,7 @@
<Compile Include="Expansive\TreeNode.cs" />
<Compile Include="Expansive\TreeNodeList.cs" />
<Compile Include="HostController.cs" />
<Compile Include="IJsonSerializer.cs" />
<Compile Include="Instrumentation\VersionLayoutRenderer.cs" />
<Compile Include="StringExtention.cs" />
<Compile Include="HttpProvider.cs" />

@ -91,8 +91,7 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NCrunch.Framework.1.45.0.11\lib\net35\NCrunch.Framework.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.5.0.2\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
@ -171,7 +170,6 @@
<Compile Include="UpdateTests\UpdateServiceFixture.cs" />
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetSceneTvdbMappingsFixture.cs" />
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetXemSeriesIdsFixture.cs" />
<Compile Include="Services\ParseErrorServiceFixture.cs" />
<Compile Include="HelperTests\SortHelperTest.cs" />
<Compile Include="DecisionEngineTests\AcceptableSizeSpecificationFixture.cs" />
<Compile Include="Qualities\QualitySizeServiceFixture.cs" />

@ -1,30 +0,0 @@
using System;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Services
{
[TestFixture]
public class ParseErrorServiceFixture : CoreTest
{
public ParseErrorServiceFixture()
{
AppDomain.CurrentDomain.AssemblyResolve +=
new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var name = new AssemblyName(args.Name);
if (name.Name == "Newtonsoft.Json")
{
return typeof(Newtonsoft.Json.JsonSerializer).Assembly;
}
return null;
}
}
}

@ -144,7 +144,7 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.5.0.2\lib\net40\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.5.0.2\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>

@ -1,10 +1,11 @@
using NUnit.Framework;
using Newtonsoft.Json;
using NzbDrone.Common;
using NzbDrone.Test.Common;
namespace NzbDrone.Libraries.Test.Json
{
[TestFixture]
public class JsonFixture
public class JsonFixture : TestBase<JsonSerializer>
{
public class TypeWithNumbers
{
@ -16,9 +17,9 @@ namespace NzbDrone.Libraries.Test.Json
{
var quality = new TypeWithNumbers { Id = 12 };
var json = JsonConvert.SerializeObject(quality);
var json = Subject.Serialize(quality);
JsonConvert.DeserializeObject(json);
Subject.Deserialize<TypeWithNumbers>(json);
}
}
}

@ -22,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -32,9 +33,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.5.0.2\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
</Reference>
@ -54,10 +52,18 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NzbDrone.Common\NzbDrone.Common.csproj">
<Project>{F2BE0FDF-6E47-4827-A420-DD4EF82407F8}</Project>
<Name>NzbDrone.Common</Name>
</ProjectReference>
<ProjectReference Include="..\NzbDrone.Core\NzbDrone.Core.csproj">
<Project>{FF5EE3B6-913B-47CE-9CEB-11C51B4E1205}</Project>
<Name>NzbDrone.Core</Name>
</ProjectReference>
<ProjectReference Include="..\NzbDrone.Test.Common\NzbDrone.Test.Common.csproj">
<Project>{CADDFCE0-7509-4430-8364-2074E1EEFCA2}</Project>
<Name>NzbDrone.Test.Common</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="5.0.2" targetFramework="net40" />
<package id="NUnit" version="2.6.2" targetFramework="net40" />
</packages>
Loading…
Cancel
Save