Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Json.NET JsonSerializer threadsafe?

Tags:

c#

json.net

I'm trying to reduce the amount of garbage my web service generates, and I noticed that we're creating a new Json.NET JsonSerializer instance for each request. It is not the most lightweight object ever, so I'm wondering if I can just create a single instance and reuse it for all requests. Primarily this requires that it be threadsafe during serialization and deserialization.

The documentation doesn't say whether it's threadsafe or not.

Inspecting the code it appears that the serialization and deserialization methods are threadsafe, as long as you don't change any settings on the object at the same time. However, it's a complex class so I'm not 100% sure of my analysis.

Has anyone tried reusing instances of JsonSerializer and had it work or not? Are there any known problems with reusing it?

like image 707
Brian Reischl Avatar asked Mar 23 '16 18:03

Brian Reischl


People also ask

Is JSON net the same as Newtonsoft JSON?

Thanks. Json.NET vs Newtonsoft. Json are the same thing. You must be trying to use the same code with different versions of Json.NET.

Is Newtonsoft JSON still supported?

Yet Newtonsoft. Json was basically scrapped by Microsoft with the coming of . NET Core 3.0 in favor of its newer offering designed for better performance, System.

Is Newtonsoft JSON open source?

Licensing. Json.NET is open source under the MIT license and is free for commercial use.

Is Jsondeserializer thread safe?

As such, a single instance of JsonDateTimeSerializer (per type), which will have been registered with the JsonDateTimeSerializer , will be reused for all serializations. It must therefore be thread safe if you plan to use the ObjectMapper across multiple threads.


3 Answers

Inspecting the code it appears that the serialization and deserialization methods are threadsafe, as long as you don't change any settings on the object at the same time.

Correct, JsonSerializer is threadsafe.

No state is shared while serializing but if you change a setting on the JsonSerializer while in the middle of serializing an object then those will automatically be used.

like image 133
James Newton-King Avatar answered Oct 14 '22 03:10

James Newton-King


According to the Feature Comparison on the Newtonsoft site, it is thread safe, as are DataContractJsonSerializer and JavaScriptSerializer.

enter image description here

like image 31
Joe Enos Avatar answered Oct 14 '22 03:10

Joe Enos


If you don't use references, JsonSerializer is thread-safe. There are however a few issues when using references in a multi-threaded context.

First, there is a bug in the default ReferenceResolver that may cause duplicate reference id's to be used. See the GitHub issue here.

Secondly, when reusing a JsonSerializer the default ReferenceResolver is stateful so that if you use references your reference ids will continue to increment with each serialization call you make instead of starting at 1 for each. I created a GitHub issue to address this problem here.

like image 20
TylerBrinkley Avatar answered Oct 14 '22 04:10

TylerBrinkley