1

Using Newtonsoft.Json.12.0.3 for serialization/deserialization, it's taking to 10 to 40 seconds to serialize the object, below is the code snippet, the amount of data is being serialized is a dynamic C# object which can be in MBs.

JsonConvert.SerializeObject(r_Value, Formatting.Indented, new JsonSerializerSettings
{
    DateFormatString = Constants.DateTimeFormat.REQUEST_DATE_FORMAT
});

r_Value is a C# object

Dev Doc
  • 188
  • 1
  • 12
  • 4
    Well, what is `r_Value`? Is it *expected* that the output is huge, or is that unexpected? Meaning: how big is the *data* you're trying to serialize? Is it possible that you've captured some object graph that you didn't want to serialize, and you just need to tell the serializer to ignore some specific property? Basically: how can we repro what you're seeing? Because if I do `var r_Value = new { Foo = 123 };` on the line above, it is going to be virtually instantaneous. – Marc Gravell Oct 02 '21 at 09:31
  • What is the value of `r_Value.GetType()`? – mjwills Oct 02 '21 at 09:45
  • r_Value is a JsonString, which is an external API response. – Dev Doc Oct 02 '21 at 13:30
  • The size can be in MBs sometime, its dynamic. – Dev Doc Oct 02 '21 at 13:31
  • 1
    *r_Value is a JsonString, which is an external API response.* -- if `r_Value` is already a JSON string, why are you serializing it a second time? Can you share a [mcve]? One thing to consider: if your data is huge, serializing to an intermediate string can be extremely wasteful of memory. Better to use a streaming solution. See: [Can Json.NET serialize / deserialize to / from a stream?](https://stackoverflow.com/q/8157636/3744182). – dbc Oct 02 '21 at 16:55
  • My bad, r_Value is a C# object. – Dev Doc Oct 04 '21 at 06:54
  • My bad, r_Value is a C# object. The size after serializing to jsonstring is in KBs (6,526,024) sometime, since the C# object has huge amout of data. – Dev Doc Oct 04 '21 at 07:06
  • What is the question? – Tom W Oct 04 '21 at 07:24
  • Serialization is taking a too long to Serialize the C# object, is there any issue with my code? The way I am calling method? Or some issue with Newtonsoft library (12.03) while dealing with the huge amount of data. – Dev Doc Oct 04 '21 at 09:17

0 Answers0