在对的WebAPI模型中使用Serializable属性 [英] Using Serializable attribute on Model in WebAPI
问题描述
我有以下情形:我使用的WebAPI和返回JSON结果基于模型消费者。我现在有额外要求的车型序列为base64才能够坚持他们在高速缓存和/或使用他们的审核。问题是,当我在 [Serializable接口]
属性添加到模型,以便为模型转换为Base64的JSON输出变化如下:
I have the following scenario: I am using WebAPI and returning JSON results to the consumer based on a model. I now have the additional requirement to serialize the models to base64 to be able to persist them in cache and/or use them for auditing purposes. Problem is that when I add the [Serializable]
attribute to the model so for converting the the model to Base64, the JSON output changes as follows:
示范:
[Serializable]
public class ResortModel
{
public int ResortKey { get; set; }
public string ResortName { get; set; }
}
如果没有 [Serializable接口]
属性的JSON输出是:
Without the [Serializable]
attribute the JSON output is:
{
"ResortKey": 1,
"ResortName": "Resort A"
}
随着 [Serializable接口]
属性的JSON输出是:
With the [Serializable]
attribute the JSON output is:
{
"<ResortKey>k__BackingField": 1,
"<ResortName>k__BackingField": "Resort A"
}
我
如何将能够使用 [Serializable接口]
属性不改变JSON的输出?
How would I be able to use the [Serializable]
attribute without changing the output of the JSON?
推荐答案
默认情况下,Json.NET忽略序列化
属性。不过,根据意见通过的这个答案 /玛姬莹>张曼玉颖(以下引用,因为意见并非持续)的WebAPI覆盖这种行为,这会导致你的输出。
By default, Json.NET ignores the Serializable
attribute. However, according to a comment to this answer by Maggie Ying (quoted below because comments are not meant to last), WebAPI overrides that behavior, which causes your output.
Json.NET串行器默认设置IgnoreSerializableAttribute为true。在的WebAPI,我们设置为false。为什么你打这个问题的原因是因为Json.NET忽略属性:现在Json.NET检测到有SerializableAttribute类型和序列化公共和私人所有在该类型字段,并忽略的属性(摘自<带引号的href=\"http://james.newtonking.com/archive/2012/04/11/json-net-4-5-release-2-serializable-support-and-bug-fixes\">james.newtonking.com/archive/2012/04/11/…)
Json.NET serializer by default set the IgnoreSerializableAttribute to true. In WebAPI, we set that to false. The reason why you hit this issue is because Json.NET ignores properties: "Json.NET now detects types that have the SerializableAttribute and serializes all the fields on that type, both public and private, and ignores the properties" (quoted from james.newtonking.com/archive/2012/04/11/…)
这表明没有的WebAPI相同的行为,一个简单的例子可以是这样的:
A simple example that demonstrates the same behavior without WebAPI can look like this:
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
namespace Scratch
{
[Serializable]
class Foo
{
public string Bar { get; set; }
}
class Program
{
static void Main()
{
var foo = new Foo() { Bar = "Blah" };
Console.WriteLine(JsonConvert.SerializeObject(foo, new JsonSerializerSettings()
{
ContractResolver = new DefaultContractResolver()
{
IgnoreSerializableAttribute = false
}
}));
}
}
}
有解决此问题的几种方法。一种是用普通来装饰你的模型的JSONObject
属性:
There are several ways around this behavior. One is to decorate your model with a plain JsonObject
attribute:
[Serializable]
[JsonObject]
class Foo
{
public string Bar { get; set; }
}
另一种方法是在你的来覆盖默认设置的Application_Start()
。根据这个答案,默认的设置应该这样做:
Another way is to override the default settings in your Application_Start()
. According to this answer, the default settings should do it:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
如果不工作,你可以明确一下:
If that doesn't work, you could be explicit about it:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings()
{
ContractResolver = new DefaultContractResolver()
{
IgnoreSerializableAttribute = true
}
};
这篇关于在对的WebAPI模型中使用Serializable属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!