Azure的移动应用定制JSON序列化 [英] Azure Mobile App customizing json serialization
问题描述
我不能似乎能够自定义在Azure中移动应用JSON序列化。
I can't seem to be able to customize JSON serialization in an Azure Mobile App.
要避免的我自己的代码,我建立一个新的项目的复杂性从头开始。 Visual Studio的社区2015年更新2的Azure应用服务工具V2.9(如果该事项)。新建项目时,Visual C#,云,Azure的移动应用。
To avoid the complexity of my own code, I setup a new project from scratch. Visual Studio Community 2015 Update 2, Azure App Service Tools v2.9 (if that matters). New Project, Visual C#, Cloud, Azure Mobile App.
在 App_Start\Startup.MobileApp.cs
此是什么模板:
public static void ConfigureMobileApp(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
new MobileAppConfiguration()
.UseDefaultConfiguration()
.ApplyTo(config);
// Use Entity Framework Code First to create database tables based on your DbContext
Database.SetInitializer(new MobileServiceInitializer());
MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();
if (string.IsNullOrEmpty(settings.HostName))
{
app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
{
// This middleware is intended to be used locally for debugging. By default, HostName will
// only have a value when running in an App Service application.
SigningKey = ConfigurationManager.AppSettings["SigningKey"],
ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] },
ValidIssuers = new[] { ConfigurationManager.AppSettings["ValidIssuer"] },
TokenHandler = config.GetAppServiceTokenHandler()
});
}
app.UseWebApi(config);
}
这是我曾尝试:
public static void ConfigureMobileApp(IAppBuilder app)
{
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
Converters = { new StringEnumConverter { CamelCaseText = true }, },
ContractResolver = new CamelCasePropertyNamesContractResolver { IgnoreSerializableAttribute = true },
DefaultValueHandling = DefaultValueHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented
};
HttpConfiguration config = new HttpConfiguration();
config.Formatters.JsonFormatter.SerializerSettings = JsonConvert.DefaultSettings();
new MobileAppConfiguration()
.UseDefaultConfiguration()
.ApplyTo(config);
...
}
运行此和访问的http://本地主机:53370 /表/内的TodoItem
,JSON是没有缩进,并有一堆假
字段,这表明被忽略的设置。
Running this and accessing http://localhost:53370/tables/TodoItem
, json is not indented, and has a bunch of false
fields, which shows settings are being ignored.
所以,我怎么能更改序列化设置,以便他们将在这个配置得到尊重?返回一个 JsonResult
与每个控制器的作品我自己的自定义设置,但只允许我发送 200 OK
状态(我有赴汤蹈火返回 201创建
尊重我的设置)。
So how can I change serializer settings so that they will be respected in this configuration? Returning a JsonResult
with my own custom settings from every controller works, but only allows me to send 200 OK
status (I have to jump through hoops to return a 201 Created
that respects my settings).
推荐答案
在此花费了大量的时间后,我觉得你可以做的最好的就是创建一个 MobileAppControllerConfigProvider
,并把它传递给 WithMobileAppControllerConfigProvider
。
After spending a lot of time on this, I think the best you can do is create a MobileAppControllerConfigProvider
and pass it to WithMobileAppControllerConfigProvider
.
这是我在做什么,让所有控制器尊重 JsonConvert.DefaultSettings
:
This is what I'm doing, to get all controllers to respect JsonConvert.DefaultSettings
:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings { /* something */ };
var provider = new MobileConfigProvider();
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
config.Formatters.JsonFormatter.SerializerSettings = provider.Settings;
new MobileAppConfiguration().
MapApiControllers().
AddMobileAppHomeController().
AddPushNotifications().
WithMobileAppControllerConfigProvider(provider).
ApplyTo(config);
和
sealed class MobileConfigProvider : MobileAppControllerConfigProvider
{
readonly Lazy<JsonSerializerSettings> settings = new Lazy<JsonSerializerSettings>(JsonConvert.DefaultSettings);
public JsonSerializerSettings Settings => settings.Value;
public override void Configure(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
base.Configure(controllerSettings, controllerDescriptor);
controllerSettings.Formatters.JsonFormatter.SerializerSettings = Settings;
}
}
这篇关于Azure的移动应用定制JSON序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!