简体   繁体   English

用于 Java 的增量流 JSON 库

[英]Incremental streaming JSON library for Java

Can anyone recommend a JSON library for Java which allows me to give it chunks of data as they come in, in a non-blocking fashion?任何人都可以为 Java 推荐一个 JSON 库,它允许我在它们进入时以非阻塞方式为其提供数据块? I have read through A better Java JSON library and similar questions, and haven't found precisely what I'd like.我已经阅读了更好的 Java JSON 库和类似问题,但还没有找到我想要的。

Essentially, what I'd like is a library which allows me to do something like the following:本质上,我想要的是一个允许我执行以下操作的库:

String jsonString1 = "{ \"A broken";
String jsonString2 = " json object\" : true }";

JSONParser p = new JSONParser(...);
p.parse(jsonString1);
p.isComplete(); // returns false
p.parse(jsonString2);
p.isComplete(); // returns true
Object o = p.getResult();

Notice the actual key name ("A broken json object") is split between pieces.请注意,实际的键名(“损坏的 json 对象”)被拆分为多个部分。

The closest I've found is this async-json-library which does almost exactly what I'd like, except it cannot recover objects where actual strings or other data values are split between pieces.我发现的最接近的是这个async-json-library ,它几乎完全符合我的要求,除了它无法恢复实际字符串或其他数据值在片段之间分割的对象。

There are a few blocking streaming/incemental JSON parsers (as per Is there a streaming API for JSON? );有一些阻塞流/增量 JSON 解析器(根据Is there a streaming API for JSON? ); but for async nothing yet that I am aware of.但是对于我所知道的异步还没有。 The lib you refer to seems badly named;您所指的 lib 名称似乎很糟糕; it does not seem to do real asynchronous processing, but merely allow one to parse sequence of JSON documents (which multiple other libs allow doing as well)它似乎没有进行真正的异步处理,而只是允许解析 JSON 文档的序列(其他多个库也允许这样做)

If there were people who really wanted this, writing one is not impossible -- for XML there is Aalto , and handling JSON is quite a bit simpler than XML.如果有人真的想要这个,写一个也不是不可能的——对于 XML 有Aalto ,处理 JSON 比 XML 简单得多。 For what it is worth, there is actually this feature request to add non-blocking parsing mode for Jackson ;值得一提的是,实际上有这个功能请求Jackson添加非阻塞解析模式; but very few users have expressed interest in getting that done (via voting for the feature request).但很少有用户表示有兴趣完成这项工作(通过投票支持功能请求)。

EDIT: (2016-01) while not async, Jackson ObjectMapper allows for convenient sub-tree by sub-tree binding of parts of the stream as well -- see ObjectReader.readValues() ( ObjectReader created from ObjectMapper ), or short-cut versions of ObjectMapper.readValues(...) .编辑:(2016-01)虽然不是异步的,但 Jackson ObjectMapper 还允许通过子树对 stream 的部分进行方便的子树绑定 - 请参见ObjectReader.readValues() (从ObjectMapper创建的ObjectReader ),或ObjectMapper.readValues(...)的版本。 Note the trailing s in there, which implies a stream of Objects, not just a single one.请注意其中的尾随s ,这意味着 stream 对象,而不仅仅是一个对象。

Google Gson can incrementally parse Json from an InputStream https://sites.google.com/site/gson/streaming Google Gson 可以从 InputStream https://sites.google.com/site/gson/streaming增量解析 Json

I wrote such a parser: JsonParser.java .我写了这样一个解析器: JsonParser.java See examples how to use it: JsonParserTest.java .查看如何使用它的示例: JsonParserTest.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM