Search code examples
c#.netwindows-phone-7.1json.net

Json.net slow serialization and deserialization


I have a problem - Json.Net serializing my objects realy slow. I have some basic class:

public class authenticationRequest
{
    public string userid;
    public string tid;
    public string token;
    public string platform;
    public string version;
}

And I'm serializing it with

string jsonDataToSend = JsonConvert.SerializeObject(dataToSend); 

This operation takes about 1900 ms. In compare to info from Json.net CodePlex page:

enter image description here

It takes a really long time. For test purposes I swapped my class for a simple string:

string jsonDataToSend = JsonConvert.SerializeObject("fsdfsdfsdfs");

And it still takes ~900 ms to convert. What is the reason? What I can do to serialize this data faster?


Solution

  • What I believe is happening here is that you are getting a delay when the Json.Net library is being loaded. You should try compiling in Release mode to see if things speed up considerably, as this should prevent symbols from being loaded (which can add to the library load time).

    If it is still an issue, find a good time in your app that you can do a dummy serialization (perhaps even on a background thread) to force the library to load. That has a bit of code-smell to it, though, so there may be a better way of forcing the load, but that's a brute force method that should work all the time.