2

Has anyone come across thread contention issues using JsonConvert in JSON.NET? I've got a large number of threads calling JsonConvert.Deserialize concurrently, and they appear to be blocking each other. VS Profiler is showing me that all my concurrency issues are buried in Newtonsoft.*. Is this a known thing? Is there something I should be doing to make the deserialize calls run in parallel?

I realize any one call is a forward-only op and won't thread itself, but why would independent deserializations contend?

2
  • Do you mean you're seeing incorrect results, or just unexpected slowdowns? Jul 26, 2014 at 12:34
  • It gets progressively slower, the more threads i run Jul 26, 2014 at 20:04

1 Answer 1

1

If you are calling a non-async method like the following:

var obj = JsonConvert.DeserializeObject<T>(jsonValue);

It is a blocking call. Consider using the async version as follows:

 var task = Task.Factory.StartNew(() => JsonConvert.DeserializeObject<T>(jsonString));
 var value = await task;  

It turns out JsonConvert.DeserializeObjectAsync() isn't truly async as mentioned by Stephen and shown here(http://james.newtonking.com/json/help/index.html?topic=html/M_Newtonsoft_Json_JsonConvert_DeserializeObjectAsync_1.htm)

Hope this helps.

Happy coding!

cleankoder

2
  • 1
    DeserializeObjectAsync is a "fake asynchronous" method; it has no async work to do, so it just pushes (synchronous) work onto the thread pool. This will negatively impact scalability. For this reason, DeserializeObjectAsync has been marked obsolete. Jul 26, 2014 at 12:33
  • Thanks for pointing this out Stephen. It turns out I wasn't getting the obsolete warning and now i see its marked obsolete. And this is what I got from their documentation:[ObsoleteAttribute("DeserializeObjectAsync is obsolete. Use the Task.Factory.StartNew method to deserialize JSON asynchronously: Task.Factory.StartNew(() => JsonConvert.DeserializeObject<T>(value, settings))")] public static Task<T> DeserializeObjectAsync<T>( string value, JsonSerializerSettings settings ) Jul 27, 2014 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.