Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

json.net - 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.

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.

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.

This is my object

[Serializable]
public class Parameters
{
    public string ApplicantName { get; set; }
}

And I am just calling:

var result = JsonConvert.SerializeObject(new Parameters(){ Name = "test" });

I get back

"{}"

Whats going on??

[EDIT]

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

* REGISTRATION * In your MvcApplication "Application_Start"...

GlobalConfiguration.Configuration.Formatters
    .JsonFormatter.SerializerSettings.ContractResolver = 
        new SerializableContractResolver()
        {
            IgnoreSerializableAttribute = true
        };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...