Json.NET JsonSerializer线程安全吗? [英] Is the Json.NET JsonSerializer threadsafe?

查看:225
本文介绍了Json.NET JsonSerializer线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试减少Web服务生成的垃圾量,并且我注意到我们正在为每个请求创建一个新的Json.NET JsonSerializer实例.它不是有史以来最轻量的对象,所以我想知道是否可以仅创建一个实例并将其重用于所有请求.首先,这要求它在序列化和反序列化期间必须是线程安全的.

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.

检查代码,发现序列化和反序列化方法是线程安全的,只要您不同时更改对象的任何设置即可.但是,这是一个复杂的课程,因此我不确定100%的分析结果.

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.

有没有人尝试重用JsonSerializer的实例,它是否起作用?重复使用是否存在任何已知问题?

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

推荐答案

检查代码,似乎序列化和反序列化方法是线程安全的,只要您不同时更改对象的任何设置即可.

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.

正确,JsonSerializer是线程安全的.

Correct, JsonSerializer is threadsafe.

序列化时不共享任何状态,但是如果在序列化对象的过程中更改JsonSerializer上的设置,则会自动使用这些状态.

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.

这篇关于Json.NET JsonSerializer线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆