c# - NewtonSoft Json serializer performance -


i have object serializing json using newtonsoft json.net. object relatively large, resulting json 300kb, serialization process takes around 60 seconds.

the objects serialized plain poco's.

the code using

string json = newtonsoft.json.jsonconvert.serializeobject(data, formatting.indented); 

is there can done speed serialization, adding attributes etc.

edit: have tested servicestack.text json serializer , takes 48 seconds, still pretty slow.

[serializable] public class appointmentitemviewmodel  {     public appointmentitemviewmodel()     {         data = new appointmentdata();         statuses = new list<status>();         closeddays = new list<closedday>();         openhours = new list<openhours>();     }      public int currentday { get; set; }      public int currentmonth { get; set; }      public int currentyear { get; set; }      public int day { get; set; }      public int month { get; set; }      public int year { get; set; }      public int firsthour { get; set; }      public int lasthour { get; set; }      public int currenthour { get; set; }      public int step { get; set; }      public bool staffonlybookown { get; set; }      public bool allowpastappointments { get; set; }      public bool allowblocks { get; set; }      public bool allowgooglecalendarsync { get; set; }      public long currentuser { get; set; }      public string debuginfo { get; set; }      public bool hasresources { get; set; }      public string organisationid { get; set; }      public string defaulttab { get; set; }      public string startday { get; set; }      public bool appointmentbreaksonweek { get; set; }      public bool appointmentbreaksonmonth { get; set; }      public appointmentdata data { get; set; }      public ienumerable<status> statuses { get; set; }      public ienumerable<locationstaff> staff { get; set; }      public ienumerable<closedday> closeddays { get; set; }      public ienumerable<openhours> openhours { get; set; }      public iusercontext usercontext()     {         return servicelocator.current.getinstance<iusercontext>();     }      public override string tostring()     {         // serialize json         var sb = new stringbuilder();          stringwriter sw = new stringwriter(sb);          using (jsonwriter writer = new jsontextwriter(sw))         {             writer.writestartobject();              writeproperty(writer, "currentday", this.currentday);             writeproperty(writer, "currentmonth", this.currentmonth);             writeproperty(writer, "currentyear", this.currentyear);             writeproperty(writer, "day", this.day);             writeproperty(writer, "month", this.month);             writeproperty(writer, "year", this.year);             writeproperty(writer, "firsthour", this.firsthour);             writeproperty(writer, "lasthour", this.lasthour);             writeproperty(writer, "currenthour", this.currenthour);             writeproperty(writer, "step", this.step);             writeproperty(writer, "staffonlybookown", this.staffonlybookown);             writeproperty(writer, "allowpastappointments", this.allowpastappointments);             writeproperty(writer, "allowblocks", this.allowblocks);             writeproperty(writer, "allowgooglecalendarsync", this.allowgooglecalendarsync);             writeproperty(writer, "currentuser", this.currentuser);             writeproperty(writer, "hasresources", this.hasresources);             writeproperty(writer, "organisationid", this.organisationid);             writeproperty(writer, "defaulttab", this.defaulttab);             writeproperty(writer, "startday", this.startday);             writeproperty(writer, "appointmentbreaksonweek", this.appointmentbreaksonweek);             writeproperty(writer, "appointmentbreaksonmonth", this.appointmentbreaksonmonth);               writer.writepropertyname("statuses");             writer.writestartarray();             foreach (var item in this.statuses)             {                 writer.writestartobject();                 writeproperty(writer, "id", item.id);                 writeproperty(writer, "description", item.description);                 writeproperty(writer, "color", item.color);                 writer.writeendobject();             }             writer.writeendarray();               writer.writepropertyname("staff");             writer.writestartarray();             foreach (var item in this.staff)             {                 writer.writestartobject();                 writeproperty(writer, "id", item.id);                 writeproperty(writer, "name", item.name);                 writer.writeendobject();             }             writer.writeendarray();               writer.writepropertyname("closeddays");             writer.writestartarray();             foreach (var item in this.closeddays)             {                 writer.writestartobject();                 writeproperty(writer, "year", item.year);                 writeproperty(writer, "month", item.month);                 writeproperty(writer, "day", item.day);                 writer.writeendobject();             }             writer.writeendarray();               writer.writepropertyname("openhours");             writer.writestartarray();             foreach (var item in this.openhours)             {                 writer.writestartobject();                 writeproperty(writer, "dayofweek", item.dayofweek);                 writeproperty(writer, "openhour", item.openhour);                 writeproperty(writer, "closehour", item.closehour);                 writer.writeendobject();             }             writer.writeendarray();              // main data             writer.writepropertyname("data");             writer.writestartobject();              writer.writepropertyname("appointments");             writer.writestartarray();             foreach (var item in this.data.appointments)             {                 writer.writestartobject();                  writeproperty(writer, "id", item.id);                 writeproperty(writer, "appointmentid", item.appointmentid);                 writeproperty(writer, "year", item.year);                 writeproperty(writer, "month", item.month);                 writeproperty(writer, "day", item.day);                 writeproperty(writer, "starthour", item.starthour);                 writeproperty(writer, "startminute", item.startminute);                 writeproperty(writer, "endhour", item.endhour);                 writeproperty(writer, "endminute", item.endminute);                 writeproperty(writer, "resourceid", item.resourceid);                 writeproperty(writer, "description", item.description);                 writeproperty(writer, "status", item.status);                 writeproperty(writer, "isclass", item.isclass);                 writeproperty(writer, "processinglength", item.processinglength);                 writeproperty(writer, "clientid", item.clientid);                 writeproperty(writer, "clientname", item.clientname);                 writeproperty(writer, "clientphone", item.clientphone);                 writeproperty(writer, "clientnotes", item.clientnotes);                 writeproperty(writer, "clienthasmobile", item.clienthasmobile);                 writeproperty(writer, "classfull", item.classfull);                 writeproperty(writer, "clientwaiting", item.clientwaiting);                 writeproperty(writer, "promotioncode", item.promotioncode);                 writeproperty(writer, "arrivalnote", item.arrivalnote);                 writeproperty(writer, "labels", item.labels);                 writeproperty(writer, "remindersent", item.remindersent);                 writeproperty(writer, "cancelled", item.cancelled);                   writer.writepropertyname("items");                 writer.writestartarray();                 foreach (var appointmentitem in item.items)                 {                     writer.writestartobject();                     writeproperty(writer, "name", appointmentitem.name);                     writeproperty(writer, "length", appointmentitem.length);                     writeproperty(writer, "processingtime", appointmentitem.processingtime);                     writeproperty(writer, "resource", appointmentitem.resource);                     writer.writeendobject();                 }                 writer.writeendarray();                  writer.writeendobject();             }             writer.writeendarray();              writer.writepropertyname("resources");             writer.writestartarray();             foreach (var item in this.data.resources)             {                 writer.writestartobject();                  writeproperty(writer, "id", item.id);                 writeproperty(writer, "name", item.name);                 writeproperty(writer, "blocklength", item.blocklength);                 writeproperty(writer, "starthour", item.starthour);                 writeproperty(writer, "endhour", item.endhour);                   writer.writepropertyname("breaks");                 writer.writestartarray();                 foreach (var breakitem in item.breaks)                 {                     writer.writestartobject();                     writeproperty(writer, "year", breakitem.year);                     writeproperty(writer, "month", breakitem.month);                     writeproperty(writer, "day", breakitem.day);                     writeproperty(writer, "dayofweek", breakitem.dayofweek);                     writeproperty(writer, "starthour", breakitem.starthour);                     writeproperty(writer, "startminute", breakitem.startminute);                     writeproperty(writer, "length", breakitem.length);                     writeproperty(writer, "description", breakitem.description);                     writeproperty(writer, "otherbreak", breakitem.otherbreak);                     writeproperty(writer, "userbreak", breakitem.userbreak);                     writer.writeendobject();                 }                 writer.writeendarray();                   writer.writepropertyname("openclosebreaks");                 writer.writestartarray();                 foreach (var breakitem in item.openclosebreaks)                 {                     writer.writestartobject();                     writeproperty(writer, "year", breakitem.year);                     writeproperty(writer, "month", breakitem.month);                     writeproperty(writer, "day", breakitem.day);                     writeproperty(writer, "dayofweek", breakitem.dayofweek);                     writeproperty(writer, "starthour", breakitem.starthour);                     writeproperty(writer, "startminute", breakitem.startminute);                     writeproperty(writer, "length", breakitem.length);                     writeproperty(writer, "description", breakitem.description);                     writeproperty(writer, "otherbreak", breakitem.otherbreak);                     writeproperty(writer, "userbreak", breakitem.userbreak);                     writer.writeendobject();                 }                 writer.writeendarray();                  writer.writeendobject();             }             writer.writeendarray();              writer.writeendobject();         }          return sb.tostring();     }      private void writeproperty(jsonwriter writer, string name, object value)     {         writer.writepropertyname(name);          if (value == null)         {             writer.writenull();         }         else         {             writer.writevalue(value);         }     }  }  [serializable] public class appointmentdata {     public ienumerable<externalevent> exteralevents { get; set; }      public ienumerable<appointment> appointments { get; set; }      public ienumerable<resource> resources { get; set; } }  [serializable] public class closedday {     public int year { get; set; }      public int month { get; set; }      public int day { get; set; } }  [serializable] public class appointment {     public long id { get; set; }      public long appointmentid { get; set; }      public int year { get; set; }      public int month { get; set; }      public int day { get; set; }      public int starthour { get; set; }      public int startminute { get; set; }      public int endhour { get; set; }      public int endminute { get; set; }      public long resourceid { get; set; }      public string description { get; set; }      public long status { get; set; }      public bool isclass { get; set; }      public int processinglength { get; set; }       public long clientid { get; set; }      public string clientname { get; set; }      public string clientphone { get; set; }      public string clientnotes { get; set; }      public bool clienthasmobile { get; set; }      public bool classfull { get; set; }      public string clientwaiting { get; set; }      public string promotioncode { get; set; }      public string arrivalnote { get; set; }      public string labels { get; set; }      public bool remindersent { get; set; }      public bool cancelled { get; set; }      public ienumerable<appointmentitems> items { get; set; } }  [serializable] public class appointmentitems {     public string name { get; set; }      public int length { get; set; }      public int processingtime { get; set; }      public string resource { get; set; } }  [serializable] public class openhours {     public int dayofweek { get; set; }      public int? openhour { get; set; }      public int? closehour { get; set; } }  [serializable] public class resource {     public resource()     {         breaks = new list<resourcebreak>();         blocks = new list<resourceblock>();         openclosebreaks = new list<resourcebreak>();     }      public long id { get; set; }      public string name { get; set; }      public int blocklength { get; set; }      public int starthour { get; set; }      public int endhour { get; set; }      public ienumerable<resourcebreak> breaks { get; set; }      public ienumerable<resourceblock> blocks { get; set; }      public ienumerable<resourcebreak> openclosebreaks { get; set; }  }  [serializable] public class externalevent {     public long id { get; set; }      public int year { get; set; }      public int month { get; set; }      public int day { get; set; }      public int dayofweek { get; set; }      public int starthour { get; set; }      public int startminute { get; set; }      public int endhour { get; set; }      public int endminute { get; set; }      public int length { get; set; }      public string description { get; set; } }  [serializable] public class resourcebreak {     public int year { get; set; }      public int month { get; set; }      public int day { get; set; }      public int dayofweek { get; set; }      public int starthour { get; set; }      public int startminute { get; set; }      public int length { get; set; }      public string description { get; set; }      public bool otherbreak { get; set; }      public bool userbreak { get; set; } }  [serializable] public class resourceblock {     public int starthour { get; set; }      public int startminute { get; set; }      public int length { get; set; } }  [serializable] public class status {     public long id { get; set; }      public string description { get; set; }      public int color { get; set; } }  [serializable] public class locationstaff {     public long id { get; set; }      public string name { get; set; } } 

have tried manually serialising object json using json.net? i've found lot faster when have large data , many properties. below 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 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, if performance in goal, not find faster option.


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -