如何从文件中流式传输 JSON? [英] How can I stream JSON from a file?

查看:65
本文介绍了如何从文件中流式传输 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将有一个可能非常大的 JSON 文件,我想从中流式传输而不是将其全部加载到内存中.基于来自 JSON 的以下声明(我添加了重点)::XS,我相信它不适合我的需求.是否有 Perl 5 JSON 模块可以从磁盘传输结果?

I will have a possibly very large JSON file and I want to stream from it instead of load it all into memory. Based on the following statement (I added the emphasis) from JSON::XS, I believe it won't suit my needs. Is there a Perl 5 JSON module that will stream the results from the disk?

在某些情况下,需要对 JSON 文本进行增量解析.虽然此模块总是必须同时将 JSON 文本和生成的 Perl 数据结构同时保存在内存中,但它确实允许您增量解析 JSON 流.它通过累积文本直到它有一个完整的 JSON 对象来实现,然后它可以解码.此过程类似于使用 decode_prefix 查看完整的 JSON 对象是否可用,但效率更高(并且可以通过最少的方法调用实现).

In some cases, there is the need for incremental parsing of JSON texts. While this module always has to keep both JSON text and resulting Perl data structure in memory at one time, it does allow you to parse a JSON stream incrementally. It does so by accumulating text until it has a full JSON object, which it then can decode. This process is similar to using decode_prefix to see if a full JSON object is available, but is much more efficient (and can be implemented with a minimum of method calls).

澄清一下,JSON 将包含一个对象数组.我想一次从文件中读取一个对象.

To clarify, the JSON will contain an array of objects. I want to read one object at a time from the file.

推荐答案

你看过 JSON::Streaming::Reader 在 search.cpan.org 上搜索JSON Stream"时显示为第一个?

Have you looked at JSON::Streaming::Reader which shows up as first while searching for 'JSON Stream' on search.cpan.org?

或者通过搜索JSON SAX"找到 JSON::SL -不是很明显的搜索词,但您描述的内容听起来像是 XML 的 SAX 解析器.

Alternatively JSON::SL found by searching for 'JSON SAX' - not quite as obvious search terms, but what you describe sounds like a SAX parsers for XML.

这篇关于如何从文件中流式传输 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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