开发者

Streaming JSON Algorithm - WITHOUT STACK

Is it possible to design a streaming JSON algorithm that writes JSON directly to a socket with the following properties:

* can only write to, but cannot delete or seek within the stream
* does not use either an IMPLICIT or explicit stack
* uses only a constant amount of memory stack depth no matter how deep the object nesting within the json

{"1":{"1":{"1":{"1":{"1":{"1":{"1":{"1":{"1":{"1":{"1":开发者_StackOverflow社区{"1":{"1":{"1":{"1":{"1":{...}}}}}}}}}}}}}}}}}


Short answer: No.

Slightly longer: At least not in the general case. If you could guarantee that the nesting has no branching, you could use a simple counter to close the braces at the end.


No, because you could use such a program to compress infinite amounts of memory into a finite space.

Encoding implementation:

input = read('LIBRARY_OF_CONGRESS.tar.bz2')
input_binary = convert_to_binary(input)
json_opening = replace({'0':'[', '1':'{'}, input_binary)
your_program <INPUTPIPE >/dev/null
INPUTPIPE << json_opening

Execute the above program then clone virtual machine it is running on. That is your finite-space compressed version of the infinitely-large input data set. Then to decode...

Decoding implementation:

set_output_pipe(your_program, OUTPUTPIPE)
INPUTPIPE << EOL
json_closing << OUTPUTPIPE
output_binary = replace({']':'0', '}':'1'}, reverse(json_closing))
output = convert_from_binary(output_binary)
write(output, 'LIBRARY_OF_CONGRESS-copy.tar.bz2')

And of course, all good code should have a test case...

Test case:

bc 'LIBRARY_OF_CONGRESS.tar.bz2' 'LIBRARY_OF_CONGRESS-copy.tar.bz2'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜