用于Java的增量流式JSON库 [英] Incremental streaming JSON library for Java
问题描述
任何人都可以推荐一个Java的JSON库,它允许我以非阻塞的方式提供数据块吗?我已阅读更好的Java JSON库和类似问题,但尚未找到正是我想要的。
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? I have read through A better Java JSON library and similar questions, and haven't found precisely what I'd like.
基本上,我想要的是一个允许我做类似以下内容的库:
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();
注意实际的密钥名称(破损的json对象)在各个部分之间分开。
Notice the actual key name ("A broken json object") is split between pieces.
我发现的最近的是 async-json-library 几乎正是我想要的,除了它无法恢复实际字符串或其他数据值在各个部分之间分割的对象。
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.
推荐答案
有一些阻止流式/内置JSON解析器(根据是否有JSON的流媒体API?);但是对于异步我还没有意识到。
你提到的lib看起来很糟糕;它似乎没有进行真正的异步处理,而只是允许一个解析JSON文档的序列(其他多个库也允许这样做)
There are a few blocking streaming/incemental JSON parsers (as per 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; 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)
如果有人真的想要这个,编写一个并非不可能 - 对于XML,有 Aalto ,处理JSON是相当的比XML简单一点。
对于它的价值,实际上有此功能请求要添加 Jackson 的非阻塞解析模式;但很少有用户表示有兴趣完成这项工作(通过对功能请求进行投票)。
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. For what it is worth, there is actually this feature request to add non-blocking parsing mode for Jackson; but very few users have expressed interest in getting that done (via voting for the feature request).
编辑:(2016-01)虽然不是异步,但Jackson ObjectMap允许通过子树的子树绑定方便的子树 - 参见 ObjectReader.readValues()
( ObjectReader
从 ObjectMapper
创建,或 ObjectMapper.readValues(...)
的快捷版本。请注意那里的尾随 s
,这意味着一个对象流,而不仅仅是一个对象。
(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(...)
. Note the trailing s
in there, which implies a stream of Objects, not just a single one.
这篇关于用于Java的增量流式JSON库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!