提供 JSON 输入流的最佳方式
在不同的语言中,我需要为用户提供一个 JSON 对象流,其接口类似于以下内容:
JSONObject json = stream.nextJSON();
由于它是一个流,因此每个调用都会阻塞,直到检索到完整的对象。这意味着尝试将每个 JSON 对象封装在一个大数组中是没有意义的。必须向流中添加额外的结构和处理层。
我想到了两种选择:
- 使用空终止字符对流进行分段。
- 编写一个理解 JSON 范围的原始解析器,以便可以检测对象的结尾。
上述每个问题都有许多潜在问题需要讨论:空终止如何与 C++、Java 和其他语言中的文件系统、套接字或底层流交互?解析时我们需要考虑哪些边缘情况? (例如,不同类型的引号可能会使解析器感到困惑)。此外,可能还有上述两种的替代方案。
所以问题是:提供 JSON 输入流的最佳方式是什么?
In different languages I need to provide users with a stream of JSON objects with an interface similar to the following:
JSONObject json = stream.nextJSON();
Since it is a stream, each call will block until a full object has been retrieved. This means it makes no sense to try and encapsulate each JSON object inside a big array. An extra layer of structure and processing has to be added to the stream.
I have thought of two options:
- Segmenting the stream with the null-termination character.
- Writing a primitive parser that understand JSON scope so can detect the end of an object.
Each of the above have a number of potential issues to discuss: How will null-termination interact with the file system, socket or underlying streams in C++, Java and other languages? What edge cases would we need to take in to account when parsing? (different types of quote symbol might confuse a parser, for example). Furthermore, there might be alternatives to the two above.
So the question is: What is the best way to provide a JSON InputStream?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谷歌显然已经考虑到了这一点:
http://sites.google.com/site/gson/streaming
Well Google already thought about it apparently:
http://sites.google.com/site/gson/streaming