[英]how to read a character stream of a json formatted text
I am dealing with a sub-process (created using fork/exec -- aka the child
) that outputs on its std-out some json formatted text. 我正在处理一个子过程(使用fork / exec创建-aka
the child
), the child
在其std-out上输出一些json格式的文本。
I have a pipe between the parent and child process, where the parent is reading the child std-out a character at a time (using getc
). 我在父进程和子进程之间有一个管道,其中父进程一次读取子标准输出一个字符(使用
getc
)。
The parent is not using the blocking waitpid function but keeps checking the io pipe for any characters in it. 父级不使用阻塞的waitpid函数,而是继续检查io管道中是否包含任何字符。
-- Up to here everything is implemented and functional -- -至此一切都已实现并发挥作用-
I am using a json c++ library that can be used to read json files. 我正在使用可用于读取json文件的json c ++库。
But obviously a half written json file can't be read; 但是显然一半的json文件无法读取。 And since the parent is reading from the child a character at a time, how can it keep track of the characters until the first valid json object is detected and so on?
而且由于父级一次从子级读取一个字符,因此它如何跟踪这些字符,直到检测到第一个有效的json对象,依此类推?
Please let me know if I am not clear so I try to rephrase. 如果不清楚,请告诉我,所以我尝试重新措辞。
If your parser does not support streaming, consider another library. 如果您的解析器不支持流式传输,请考虑使用另一个库。
yajl is an ANSI-C parser for streaming JSON, it should work with your C++ code. yajl是用于流JSON的ANSI-C解析器,它应与您的C ++代码一起使用。
There are a bunch of other C and C++ tools listed at the bottom of the json.org homepage. json.org主页底部列出了许多其他C和C ++工具。 One of them might work even better.
其中之一可能会更好。
"First valid json object" is troubling. “第一个有效的json对象”令人担忧。 JSON only permits one top-level object, so you should be able to just read until EOF (assuming you've closed your pipes properly).
JSON仅允许一个顶级对象,因此您应该能够读取直到EOF(假设您已正确关闭管道)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.