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

查看:2105
本文介绍了如何从文件流式传输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对象(然后可以对其进行解码)来做到这一点.此过程类似于使用encode_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流"时显示为第一个?

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天全站免登陆