diff --git a/NzbDrone.Api/ErrorManagement/ApiException.cs b/NzbDrone.Api/ErrorManagement/ApiException.cs index 27da46d66..0d5842d19 100644 --- a/NzbDrone.Api/ErrorManagement/ApiException.cs +++ b/NzbDrone.Api/ErrorManagement/ApiException.cs @@ -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 @@ -15,7 +13,7 @@ public abstract class ApiException : Exception public HttpStatusCode StatusCode { get; private set; } protected ApiException(HttpStatusCode statusCode, object content = null) - : base(GetMessage(statusCode, content)) + : base(GetMessage(statusCode, content)) { StatusCode = statusCode; Content = content; @@ -32,7 +30,7 @@ private static string GetMessage(HttpStatusCode statusCode, object content) if (content != null) { - result = result + " :" + JsonConvert.SerializeObject(content); + result = result + " :" + content; } return result; diff --git a/NzbDrone.Api/Extensions/NancyJsonSerializer.cs b/NzbDrone.Api/Extensions/NancyJsonSerializer.cs index 12b593785..f0a7818fe 100644 --- a/NzbDrone.Api/Extensions/NancyJsonSerializer.cs +++ b/NzbDrone.Api/Extensions/NancyJsonSerializer.cs @@ -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 @@ public bool CanSerialize(string contentType) public void Serialize(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 Extensions { get; private set; } diff --git a/NzbDrone.Api/Extensions/RequestExtensions.cs b/NzbDrone.Api/Extensions/RequestExtensions.cs index 485245068..861e59f70 100644 --- a/NzbDrone.Api/Extensions/RequestExtensions.cs +++ b/NzbDrone.Api/Extensions/RequestExtensions.cs @@ -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(this Stream body) + private static readonly JsonSerializer Serializer = new JsonSerializer(); + private static readonly NancyJsonSerializer NancySerializer = new NancyJsonSerializer(Serializer); + + public static T FromJson(this Stream body) where T : class, new() { var reader = new StreamReader(body, true); body.Position = 0; var value = reader.ReadToEnd(); - return JsonConvert.DeserializeObject(value, Serializer.Settings); + return Serializer.Deserialize(value); } public static JsonResponse AsResponse(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK) { - var jsonResponse = new JsonResponse(model, new NancyJsonSerializer()) { StatusCode = statusCode }; - return jsonResponse; + return new JsonResponse(model, NancySerializer) { StatusCode = statusCode }; } } } \ No newline at end of file diff --git a/NzbDrone.Api/Extensions/Serializer.cs b/NzbDrone.Api/Extensions/Serializer.cs deleted file mode 100644 index 5d6b0f8f3..000000000 --- a/NzbDrone.Api/Extensions/Serializer.cs +++ /dev/null @@ -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; } - } -} \ No newline at end of file diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index 057c8ad41..3d2f700d3 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -66,10 +66,6 @@ False ..\packages\Nancy.0.16.1\lib\net40\Nancy.dll - - False - ..\packages\Newtonsoft.Json.5.0.2\lib\net40\Newtonsoft.Json.dll - False ..\packages\NLog.2.0.1.2\lib\net40\NLog.dll @@ -86,7 +82,6 @@ - diff --git a/NzbDrone.Api/packages.config b/NzbDrone.Api/packages.config index e36eebf8c..3144ad997 100644 --- a/NzbDrone.Api/packages.config +++ b/NzbDrone.Api/packages.config @@ -3,6 +3,5 @@ - \ No newline at end of file diff --git a/NzbDrone.Common/IJsonSerializer.cs b/NzbDrone.Common/IJsonSerializer.cs new file mode 100644 index 000000000..ee3d86f76 --- /dev/null +++ b/NzbDrone.Common/IJsonSerializer.cs @@ -0,0 +1,58 @@ +using System.IO; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; + +namespace NzbDrone.Common +{ + public interface IJsonSerializer + { + T Deserialize(string json) where T : class, new(); + string Serialize(object obj); + void Serialize(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(string json) where T : class, new() + { + return JsonConvert.DeserializeObject(json); + } + + public string Serialize(object obj) + { + return JsonConvert.SerializeObject(obj); + } + + + public void Serialize(TModel model, Stream outputStream) + { + var jsonTextWriter = new JsonTextWriter(new StreamWriter(outputStream)); + _jsonNetSerializer.Serialize(jsonTextWriter, model); + jsonTextWriter.Flush(); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index e94af7a0b..7010e9995 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -67,7 +67,7 @@ False - ..\packages\Newtonsoft.Json.5.0.2\lib\net40\Newtonsoft.Json.dll + ..\packages\Newtonsoft.Json.5.0.2\lib\net35\Newtonsoft.Json.dll False @@ -115,6 +115,7 @@ + diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 9c3c3db27..8c0a114c4 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -91,8 +91,7 @@ False ..\packages\NCrunch.Framework.1.45.0.11\lib\net35\NCrunch.Framework.dll - - False + ..\packages\Newtonsoft.Json.5.0.2\lib\net40\Newtonsoft.Json.dll @@ -171,7 +170,6 @@ - diff --git a/NzbDrone.Core.Test/Services/ParseErrorServiceFixture.cs b/NzbDrone.Core.Test/Services/ParseErrorServiceFixture.cs deleted file mode 100644 index f246852a8..000000000 --- a/NzbDrone.Core.Test/Services/ParseErrorServiceFixture.cs +++ /dev/null @@ -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; - } - } -} diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 6a84aa78c..dff681406 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -144,7 +144,7 @@ False - ..\packages\Newtonsoft.Json.5.0.2\lib\net40\Newtonsoft.Json.dll + ..\packages\Newtonsoft.Json.5.0.2\lib\net35\Newtonsoft.Json.dll False diff --git a/NzbDrone.Libraries.Test/Json/JsonFixture.cs b/NzbDrone.Libraries.Test/Json/JsonFixture.cs index e5a9cb148..05df5fd1e 100644 --- a/NzbDrone.Libraries.Test/Json/JsonFixture.cs +++ b/NzbDrone.Libraries.Test/Json/JsonFixture.cs @@ -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 { public class TypeWithNumbers { @@ -16,9 +17,9 @@ public void should_be_able_to_deserialize_numbers() { var quality = new TypeWithNumbers { Id = 12 }; - var json = JsonConvert.SerializeObject(quality); + var json = Subject.Serialize(quality); - JsonConvert.DeserializeObject(json); + Subject.Deserialize(json); } } } diff --git a/NzbDrone.Libraries.Test/NzbDrone.Libraries.Test.csproj b/NzbDrone.Libraries.Test/NzbDrone.Libraries.Test.csproj index f27aadabc..08acd5be3 100644 --- a/NzbDrone.Libraries.Test/NzbDrone.Libraries.Test.csproj +++ b/NzbDrone.Libraries.Test/NzbDrone.Libraries.Test.csproj @@ -22,6 +22,7 @@ DEBUG;TRACE prompt 4 + x86 pdbonly @@ -32,9 +33,6 @@ 4 - - ..\packages\Newtonsoft.Json.5.0.2\lib\net40\Newtonsoft.Json.dll - ..\packages\NUnit.2.6.2\lib\nunit.framework.dll @@ -54,10 +52,18 @@ + + {F2BE0FDF-6E47-4827-A420-DD4EF82407F8} + NzbDrone.Common + {FF5EE3B6-913B-47CE-9CEB-11C51B4E1205} NzbDrone.Core + + {CADDFCE0-7509-4430-8364-2074E1EEFCA2} + NzbDrone.Test.Common + diff --git a/NzbDrone.Libraries.Test/packages.config b/NzbDrone.Libraries.Test/packages.config index 253df0920..5c3ca54dd 100644 --- a/NzbDrone.Libraries.Test/packages.config +++ b/NzbDrone.Libraries.Test/packages.config @@ -1,5 +1,4 @@  - \ No newline at end of file