是否有一个拉风的JSON的StreamReader解析器.NET?有一个、拉风、JSON、StreamReader

2023-09-06 13:08:08 作者:为了防止我这个月又乱花钱

我需要从一个JSON反序列化的StreamReader ,没有实例中间对象为流中的每一个对象。

JSON.NET 像调用 JsonConvert.DeserializeObject< T>( JSON字符串,JsonConverter []转换器) 在不接受的StreamReader 和 在内存中创建了一整套 Newtonsoft.Json的。* 班的JSON structore,如内存分析器节目。 _I'm实际上并不一定是这样的话,因为我的 JsonConverter 取值_ JavaScriptSerializer 也似乎不支持 s的一切。 甚至undocummented 新JsonSerializer()。反序列化(新JsonTextReader(的StreamReader))

我想在一个零的做法是 SAX 或的 StAX的风格的JSON解析器。

我要的理想,就像

的API

 对象JsonCooler.DeserializeObject< T>(
  StreamReader的jsonStream,转换器[]转换器)
 

这将直接进入反序列化类 T 的新实例,而不是保持所有的中间类DOM对象在内存中。

有没有这样的解串器存在吗?还是我缺少什么停留在概念层面?

解决方案

我怀疑这是一个用例变得越来越频繁。我正在考虑支持它,通过这个小家伙,我也努力保持短期和快速的一段时间:

https://github.com/ysharplanguage/FastJsonParser

我还在忙着使其稳居第一,但如果你想给一个尝试,在实现在一个叉之前,我得到一个机会,那会希望有足够的小幅修正OBJ(INT外)和编曲(INT外)的方法,以及添加(可选)齐磊回调委托给所属类别缓存。这人会允许应用程序决定是否/何时下一个对象{...}或阵列[...]流中需要它的内容完全反序列化与否。

心连心,

json server 详解

I need to deserialize from a JSON StreamReader, without instantiating intermediate objects for every object in that stream.

JSON.NET invoked like JsonConvert.DeserializeObject<T>(string json, JsonConverter[] converters)

doesn't accept StreamReader, and creates in memory the whole set of Newtonsoft.Json.* classes for the JSON structore, as memory profiler shows. _I'm not actually sure this is the case, because my JsonConverters _

JavaScriptSerializer also doesn't seem to support Streams at all.

And even undocummented new JsonSerializer().Deserialize(new JsonTextReader(streamReader))

What I want in a zero approach is a SAX or StAX-style JSON parser.

What I want ideally, is an API like

object JsonCooler.DeserializeObject<T>(
  StreamReader jsonStream, Converter[] converters)

which will deserialize directly into a new instance of class T, not keeping all of its intermediate DOM-like objects in memory.

Does such a deserializer exist? Or what am I missing at the concept level?

解决方案

I suspect it's a use case becoming more and more frequent. I am considering to support it some time via this little one that I also strive to keep short and fast:

https://github.com/ysharplanguage/FastJsonParser

I'm still busy making it stable first, but if you want to give a try at implementing that in a fork before I get a chance, that'd hopefully be enough to amend slightly the Obj(int outer) and Arr(int outer) methods, along with adding an (optional) "reviver" callback delegate to the TypeInfo cache. That one would allow the application to decide whether/when the next object "{ ... }" or array "[ ... ]" in the stream needs its content be fully deserialized or not.

'HTH,