Newtonsoft JSON序列化器返回空对象 [英] Newtonsoft json serializer returns empty object
问题描述
好吧-我已经将这个问题打了几个小时.是时候寻求帮助了.
Ok - I've been beating my head against this for a few of hours now. Time to ask for help.
我刚刚将我的Web应用程序项目升级到ASP.NET MVC 4 RC和新的WebApi. 我的网络api方法现在返回EMPTY json"{}"-即使我的对象已完全填充.
I have just upgraded my Web application project to ASP.NET MVC 4 RC, and the new WebApi. My web api method is now returning EMPTY json "{}" - even though my object is fully populated.
我已经用自己的MediaTypeFormatter替换了序列化程序,该介质类型也称为Newtonsoft Json序列化程序,这样我就可以挂接并看到工作正常. 我看到的是一个对象进入序列化器,并以"{}"的形式出现.
I have replace the serializer with my own MediaTypeFormatter that also calls the Newtonsoft Json serializer, just so I can hook in and see things working. What I see is an object going in to the serializer, and coming out as "{}".
此功能在我升级之前就可以使用.
This USED to work before I upgraded.
这是我的对象
[Serializable]
public class Parameters
{
public string ApplicantName { get; set; }
}
我只是打电话给
var result = JsonConvert.SerializeObject(new Parameters(){ Name = "test" });
我回来了
"{}"
怎么回事?
其他人也有相同的问题...在浏览完Newtonsoft源代码后,我发现我们在最近的更改中也遇到了完全相同的问题.
Someone else having the same problem... after running through the Newtonsoft source code, I can see we're having the exact same problem from a recent change.
http://json.codeplex.com/discussions/357850
推荐答案
好-进行了许多更改,结果是对Json输出进行了相当大的更改.这些更改还包括如何应用自定义TypeConverters.
Ok - there have been numerous changes, which result is some pretty radical changes to the Json output. These changes also include how custom TypeConverters are applied.
我已经写了一个基本的解析器,(至少对我们而言)使Newtonsoft序列化器的行为更像基本的Serializable对象序列化器-即序列化所有PROPERTIES,并且不使用自定义TypeConverters ...
I have written a basic resolver which (for us at least) causes the Newtonsoft serializer to behave more like a basic Serializable object serializer - i.e. serializes all PROPERTIES, and doesnt use custom TypeConverters...
/// <summary>
/// A resolver that will serialize all properties, and ignore custom TypeConverter attributes.
/// </summary>
public class SerializableContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
protected override IList<Newtonsoft.Json.Serialization.JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var properties = base.CreateProperties(type, memberSerialization);
foreach (var p in properties)
p.Ignored = false;
return properties;
}
protected override Newtonsoft.Json.Serialization.JsonContract CreateContract(Type objectType)
{
var contract = base.CreateContract(objectType);
if (contract is Newtonsoft.Json.Serialization.JsonStringContract)
return CreateObjectContract(objectType);
return contract;
}
}
*注册* 在您的MvcApplication中,"Application_Start" ...
* REGISTRATION * In your MvcApplication "Application_Start"...
GlobalConfiguration.Configuration.Formatters
.JsonFormatter.SerializerSettings.ContractResolver =
new SerializableContractResolver()
{
IgnoreSerializableAttribute = true
};
这篇关于Newtonsoft JSON序列化器返回空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!