Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newtonsoft json serializer returns empty object

Tags:

json

json.net

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

like image 356
Adam Avatar asked Jun 15 '12 04:06

Adam


People also ask

Does an empty string trigger serialization exception in NewtonSoft?

I'd want to rely on json serializer to throw serialization exception if it can't correctly deserialize required fields, but an empty string doesn't trigger serialization exception (be it just an empty string "" or an emtpy json string """" ); using Newtonsoft. Json ; using Newtonsoft.

How can I resize an array in a JSON file?

The arrays can be resized using Array.Resize. But it is easier to use List<Child> (which behaves like arrays too). I think that the classes generated by Paste Special is just a suggestion. Try using public classes and properties. Thanks a lot, so easy, now works. Do you still have suggestions if I have a JSON file, structure unknown.

Is it possible to null a string in NewtonSoft?

I would like to stress one point here - since C# we have nullable reference types in the language. If the assembly calling to Newtonsoft has opted in to nullable aware context, then 'null' is no longer a legitimate value for a string type from the developers' perspective.


1 Answers

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
        };
like image 78
Adam Avatar answered Oct 20 '22 21:10

Adam