json response from API are now in pascalCasing

pull/23/head
Keivan Beigi 12 years ago committed by kay.one
parent e720094f9c
commit 9969f66201

@ -5,6 +5,7 @@ using NLog;
using Nancy.Bootstrapper; using Nancy.Bootstrapper;
using Nancy.Bootstrappers.Autofac; using Nancy.Bootstrappers.Autofac;
using NzbDrone.Api.ErrorManagment; using NzbDrone.Api.ErrorManagment;
using NzbDrone.Api.Extentions;
using NzbDrone.Api.QualityProfiles; using NzbDrone.Api.QualityProfiles;
using NzbDrone.Api.QualityType; using NzbDrone.Api.QualityType;
using NzbDrone.Api.Resolvers; using NzbDrone.Api.Resolvers;
@ -83,6 +84,7 @@ namespace NzbDrone.Api
var internalConfig = NancyInternalConfiguration.Default; var internalConfig = NancyInternalConfiguration.Default;
internalConfig.StatusCodeHandlers.Add(typeof(ErrorHandler)); internalConfig.StatusCodeHandlers.Add(typeof(ErrorHandler));
internalConfig.Serializers.Add(typeof(NancyJsonSerializer));
return internalConfig; return internalConfig;

@ -3,6 +3,7 @@ using System.Linq;
using Nancy; using Nancy;
using Nancy.Responses; using Nancy.Responses;
using Newtonsoft.Json; using Newtonsoft.Json;
using NzbDrone.Api.Extentions;
using NzbDrone.Api.QualityType; using NzbDrone.Api.QualityType;
namespace NzbDrone.Api.ErrorManagment namespace NzbDrone.Api.ErrorManagment

@ -1,6 +1,7 @@
using System.Linq; using System.Linq;
using Nancy; using Nancy;
using Nancy.ErrorHandling; using Nancy.ErrorHandling;
using NzbDrone.Api.Extentions;
using NzbDrone.Api.QualityType; using NzbDrone.Api.QualityType;
namespace NzbDrone.Api.ErrorManagment namespace NzbDrone.Api.ErrorManagment

@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Nancy;
using Newtonsoft.Json;
namespace NzbDrone.Api.Extentions
{
public class NancyJsonSerializer : ISerializer
{
public readonly static NancyJsonSerializer Instance = new NancyJsonSerializer();
public bool CanSerialize(string contentType)
{
return true;
}
public void Serialize<TModel>(string contentType, TModel model, Stream outputStream)
{
var jsonTextWriter = new JsonTextWriter(new StreamWriter(outputStream));
Serializer.Instance.Serialize(jsonTextWriter, model);
jsonTextWriter.Flush();
}
public IEnumerable<string> Extensions { get; private set; }
}
}

@ -1,11 +1,10 @@
using System; using System.IO;
using System.IO;
using System.Linq; using System.Linq;
using Nancy; using Nancy;
using Nancy.Responses; using Nancy.Responses;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace NzbDrone.Api.QualityType namespace NzbDrone.Api.Extentions
{ {
public static class JsonExtensions public static class JsonExtensions
{ {
@ -14,17 +13,12 @@ namespace NzbDrone.Api.QualityType
var reader = new StreamReader(body, true); var reader = new StreamReader(body, true);
body.Position = 0; body.Position = 0;
var value = reader.ReadToEnd(); var value = reader.ReadToEnd();
return JsonConvert.DeserializeObject<T>(value, new JsonSerializerSettings return JsonConvert.DeserializeObject<T>(value, Serializer.Settings);
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
});
} }
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK) public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
{ {
ISerializer serializer = new DefaultJsonSerializer(); var jsonResponse = new JsonResponse<TModel>(model, new NancyJsonSerializer()) { StatusCode = statusCode };
var jsonResponse = new JsonResponse<TModel>(model, serializer) { StatusCode = statusCode };
return jsonResponse; return jsonResponse;
} }
} }

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

@ -89,6 +89,8 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Extentions\NancyJsonSerializer.cs" />
<Compile Include="Extentions\Serializer.cs" />
<Compile Include="QualityProfiles\RootFolderModule.cs" /> <Compile Include="QualityProfiles\RootFolderModule.cs" />
<Compile Include="Series\SeriesModule.cs" /> <Compile Include="Series\SeriesModule.cs" />
<Compile Include="ErrorManagment\ApiException.cs" /> <Compile Include="ErrorManagment\ApiException.cs" />
@ -103,7 +105,7 @@
<Compile Include="QualityProfiles\QualityProfilesModule.cs" /> <Compile Include="QualityProfiles\QualityProfilesModule.cs" />
<Compile Include="QualityType\QualityTypeModel.cs" /> <Compile Include="QualityType\QualityTypeModel.cs" />
<Compile Include="QualityType\QualityTypeModule.cs" /> <Compile Include="QualityType\QualityTypeModule.cs" />
<Compile Include="QualityType\RequestExtensions.cs" /> <Compile Include="Extentions\RequestExtensions.cs" />
<Compile Include="Resolvers\AllowedToQualitiesResolver.cs" /> <Compile Include="Resolvers\AllowedToQualitiesResolver.cs" />
<Compile Include="Resolvers\QualitiesToAllowedResolver.cs" /> <Compile Include="Resolvers\QualitiesToAllowedResolver.cs" />
<Compile Include="Resolvers\QualityTypesToIntResolver.cs" /> <Compile Include="Resolvers\QualityTypesToIntResolver.cs" />

@ -2,6 +2,7 @@
using System.Linq; using System.Linq;
using AutoMapper; using AutoMapper;
using Nancy; using Nancy;
using NzbDrone.Api.Extentions;
using NzbDrone.Core.Providers; using NzbDrone.Core.Providers;
using NzbDrone.Core.Repository.Quality; using NzbDrone.Core.Repository.Quality;
using NzbDrone.Api.QualityType; using NzbDrone.Api.QualityType;

@ -1,4 +1,5 @@
using Nancy; using Nancy;
using NzbDrone.Api.Extentions;
using NzbDrone.Core.Providers; using NzbDrone.Core.Providers;
using NzbDrone.Api.QualityType; using NzbDrone.Api.QualityType;
using NzbDrone.Core.Repository; using NzbDrone.Core.Repository;

@ -2,6 +2,7 @@
using System.Linq; using System.Linq;
using AutoMapper; using AutoMapper;
using Nancy; using Nancy;
using NzbDrone.Api.Extentions;
using NzbDrone.Api.QualityProfiles; using NzbDrone.Api.QualityProfiles;
using NzbDrone.Core.Providers; using NzbDrone.Core.Providers;

@ -1,5 +1,6 @@
using System.Linq; using System.Linq;
using Nancy; using Nancy;
using NzbDrone.Api.Extentions;
using NzbDrone.Api.QualityType; using NzbDrone.Api.QualityType;
using NzbDrone.Core.Providers; using NzbDrone.Core.Providers;

Loading…
Cancel
Save