During serialization ShouldSerialize...() function is not invoked

Mar 19, 2010 at 11:20 PM
Edited Mar 19, 2010 at 11:21 PM

Json.Net is a wonderful piece of software, but it is odd that no has faced this issue before.

In traditional .Net XmlSerialization if you wanted to determine if a field/property should be serialized at runtime then you could control it by implementing a function ShouldSerialize...() which would return a bool value determining the outcome.

Lets take an example.

[Serializable]
public class Testing
{
    [XmlElement]
    [DataMember]
    public int Id;

    [XmlElement]
    [DataMember]
    public List Test;

    public bool ShouldSerializeTest()
    {
        return this.Test != null && this.Test.Count > 0;
    }
}

static void Main(string[] args)
{
    Testing t1, t2, t3;
    t1 = new Testing { Id = 1 };
    t2 = new Testing { Id = 2, Test = new List() };
    t3 = new Testing { Id = 3};
    t3.Test = new List();
    t3.Test.Add("Saleem");

    string j1 = JsonConvert.SerializeObject(t1);
    string j2 = JsonConvert.SerializeObject(t2);
    string j3 = JsonConvert.SerializeObject(t3);

    string x1 = WriteXml(t1);
    string x2 = WriteXml(t2);
    string x3 = WriteXml(t3);

    Console.WriteLine("j1:{0}\n j2:{1}\n j3:{2}\n", j1, j2, j3);
    Console.WriteLine("x1:{0}\n x2:{1}\n x3:{2}\n", x1, x2, x3);
}
static string WriteXml(Testing t)
{
    var ser = new XmlSerializer(t.GetType());
    var sb = new StringBuilder();
    using (var sw = new StringWriter(sb))
    {
        ser.Serialize(sw, t);
    }
    return sb.ToString();
}

The result from JsonConvert.SerializeObject is 

j1:{"Id":1,"Test":null}
j2:{"Id":2,"Test":[]}
j3:{"Id":3,"Test":["Saleem"]}

and result from XmlSerializer is 

<?xml version="1.0" encoding="utf-16"?>
<Testing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Id>1</Id>
</Testing>

<?xml version="1.0" encoding="utf-16"?>
<Testing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Id>2</Id>
</Testing>

<?xml version="1.0" encoding="utf-16"?>
<Testing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Id>3</Id>
  <Test>Saleem</Test>
</Testing>

they are both different because the ShouldSerializeTest() function is invoked by the XmlSerializer at runtime to determine if the value needs to be serialized.

Is this feature supported in Json.Net at all, or am i missing something.

It would be a great help if this functionality could be introduced.

Thanks,

Saleem

Coordinator
Mar 20, 2010 at 10:18 AM

I never knew you could do that with the XmlSerializer. Weird.

The feature amuses me and it wasn't hard to implement so I've added it to Json.NET.

http://json.codeplex.com/SourceControl/list/changesets

Mar 22, 2010 at 6:24 PM

Thank you for responding quickly.

I'll get the latest code and then test it out.

When are you planning to have the next stable release out.

-Saleem

Coordinator
Mar 23, 2010 at 2:14 AM

A month or two? No idea :)