1

In my code I use Newtonsoft.Json :

MyCSharpTargetClass foo = JsonConvert.DeserializeObject<MyCSharpTargetClass>(json_string);

The content of the json_string is constant (loaded from a file that never changes).

The functionality is fine but the deserializing process is slow at execution.

My question: is there a way to pre-calculate the object, or to put it in a constant*, or whatever to speed up the execution ?

Note: Actually I didn't manage to put it a constant the result of JsonConvert.DeserializeObject<...>(), but I'm new to C#, so I should have missed something.

Thanks for your help

[EDIT] Solution : Force the Newtonsoft assembly to be loaded at the start of the application.

7
  • What is your project type? Winforms? Web? Console? Commented Jul 4, 2017 at 17:00
  • "the deserializing process is slow at execution" - how certain are you of that and how did you determine it? Is the string that big? Also, why do you read it again and again to notice if it changed? If it's a big file, why can't you just read the relevant part? Why can't you watch for events to the file instead? Commented Jul 4, 2017 at 17:17
  • @msd : Application UWP (mainly for windows 10 mobile) Commented Jul 4, 2017 at 17:19
  • @CodeCaster : The json file is read only once. It is not big (about 100 small lines). On desktop, the process duration is barely noticeable; but on my windows 10 mobile Nokia 1520, there is a big difference (about 1.5s) between full constant (hardcoded) and the deserializing operation (even if the json string itself is in constant). That's how a guess the root of the slowing down. Commented Jul 4, 2017 at 17:28
  • 1
    That would be the loading of the Newtonsoft assembly. Commented Jul 4, 2017 at 17:53

2 Answers 2

1

You do it the same way you would any other object: make it a private member variable, whose value is computed exactly once:

private static readonly MyCSharpTargetClass foo = JsonConvert.DeserializeObject<MyCSharpTargetClass>(json_string);

Or do it in the constructor:

private readonly MyCSharpTargetClass foo;

public MyObject()
{
    foo = JsonConvert.DeserializeObject<MyCSharpTargetClass>(json_string);
}

That way the work to deserialize the string is done exactly once.

Sign up to request clarification or add additional context in comments.

Comments

1

There's actualy not a lot of thinks you can do to speed this up.

  1. Using ignore attribute, so this fields will not be added. Use this for some properties or calcualatable properties/fields, which can be recalculated later. Hope this link helps :

    https://msdn.microsoft.com/en-us/library/system.runtime.serialization.ignoredatamemberattribute.aspx

  2. Using threads to read/write your objects, if you can customize your data into many files and serialize/deserialize in threads.

  3. Deserialization JSON algorithms requires to read whole file about 3 times. For better perfomance you can use YAML. Simple as XML/JSON, but with much better perfomance, since it reads file only 1 time.
  4. Using shorter DataMember names to fields [DataMember(Name = "id")] public int MyObjectForSomethink{get;set;}

1 Comment

Do you have any evidence to support number 3? I've had no issues in the past reading an entire JSON file incrementally in a single pass.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.