json response from API are now in pascalCasing

This commit is contained in:
Keivan Beigi 2013-01-25 11:03:28 -08:00 committed by kay.one
parent e720094f9c
commit 9969f66201
11 changed files with 75 additions and 11 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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