NewtonSoft Json serializer performance

Have you tried manually serialising your object into JSON using JSON.NET? I've found it a lot faster when you have large data and many properties. Below is an example:

public static string Serialise(YourObject data)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

using (JsonWriter writer = new JsonTextWriter(sw))
{
    writer.WriteStartObject();

    writer.WritePropertyName("propertyName1");

    if (data.Property1 == null)
    {
        writer.WriteNull();
    }
    else
    {
        writer.WriteValue(data.Property1);
    }

    writer.WritePropertyName("propertyName2");

    writer.WriteStartArray();

    foreach (var something in data.CollectionProperty)
    {
        writer.WriteStartObject();

        writer.WritePropertyName("p1");
        writer.WriteValue(something.prop1);

        writer.WritePropertyName("p2");
        writer.WriteValue(something.prop2);

        writer.WritePropertyName("p3");
        writer.WriteValue(something.prop3);

        writer.WriteEndObject();
    }

    writer.WriteEndArray();

    writer.WriteEndObject();
}

return sb.ToString();
}

It means more work, but if performance in your goal, you will not find a faster option.


You should give Jon Bellamy the points for the answer, but here is a little more detail:

I had the same problem with a project I'm working on and I solved it by following the advice on this page:

http://www.newtonsoft.com/json/help/html/Performance.htm

Specifically, they recommend manually serializing your objects when performance is critical:

public static string ToJson(this Person p)
{
    StringWriter sw = new StringWriter();
    JsonTextWriter writer = new JsonTextWriter(sw);

    // {
    writer.WriteStartObject();

    // "name" : "Jerry"
    writer.WritePropertyName("name");
    writer.WriteValue(p.Name);

    // "likes": ["Comedy", "Superman"]
    writer.WritePropertyName("likes");
    writer.WriteStartArray();
    foreach (string like in p.Likes)
    {
        writer.WriteValue(like);
    }
    writer.WriteEndArray();

    // }
    writer.WriteEndObject();

    return sw.ToString();
}

My example in VB looks like this:

    Public Function SerializeWords(ByRef oWords As List(Of Word))
        Dim sb As New StringBuilder
        Dim sw As New IO.StringWriter(sb)
        Using oWriter As Newtonsoft.Json.JsonWriter = New Newtonsoft.Json.JsonTextWriter(sw)
            With oWriter
                .WriteStartArray()
                For Each oWord As Word In oWords
                    .WriteStartObject()

                    .WritePropertyName("ID")
                    .WriteValue(oWord.ID)

                    .WritePropertyName("Phonics")
                    .WriteValue(oWord.Phonics)

                    .WritePropertyName("Word_")
                    .WriteValue(oWord.Word_)

                    .WritePropertyName("WordLength")
                    .WriteValue(oWord.WordLength)

                    .WriteEndObject()
                Next
                .WriteEndArray()

            End With
        End Using
        Return sb.ToString

    End Function

Notice how both functions are strongly typed. I believe when you use Newtonsoft.Json.JsonConvert.SerializeObject() it's using reflection to get the job done (which can really add up when you have many objects with many properties).

Anyways... once I wrote my own serializer, my time serializing a list of 250 "Word" objects went from 28 seconds using JsonConvert.SerializeObject() method to 31 milliseconds using my own function.

Tags:

C#

Json.Net