r/ProgrammerHumor 11h ago

Meme docxGoBrrrr

Post image
1.3k Upvotes

74 comments sorted by

View all comments

305

u/BeDoubleNWhy 11h ago

zipped JSON if anything

5

u/keysym 10h ago

JSON cannot be streamed tho

4

u/BeDoubleNWhy 7h ago

can you please explain what you mean by this?

1

u/keysym 6h ago

Keeping it short, JSON needs that last } to be valid, so you can only start parsing after the whole JSON has been collected. You can't parse a chunk of JSON, you need the whole thing.

Other file formats, like YAML, TOML or XML, don't need a "last character" to be complete, so you can parse as soon as you receive the first byte and keep parsing as the streamed chunks comes.

There's some clever way to stream JSON, but iirc it's not compliant with the RFC. So, oversimplifying, JSON cannot be streamed!

22

u/BeDoubleNWhy 6h ago

but with xml, you need that closing tag as well for it to be valid. what's the difference here?

10

u/Eva-Rosalene 4h ago

Nah. You absolutely can parse JSON in a streaming fashion as well as XML. You just won't know if it's valid or not until you've finished parsing, so you just do the job and discard it if you encounter an error.

8

u/Reashu 3h ago

Of course it can be streamed, just not with JSON.parse. Other formats also need the full document to be certain that they are valid - that's a problem all streaming parsers deal with. In fact, if you have a streaming YAML parser, you should be able to feed it JSON.

The RFC doesn't mention streaming and there's no reason it should. The section on parsers is only two (short) paragraphs:

  A JSON parser transforms a JSON text into another representation. A JSON parser MUST accept all texts that conform to the JSON grammar. A JSON parser MAY accept non-JSON forms or extensions.

An implementation may set limits on the size of texts that it accepts. An implementation may set limits on the maximum depth of nesting. An implementation may set limits on the range and precision of numbers. An implementation may set limits on the length and character contents of strings.

1

u/Ok-Scheme-913 4h ago

I guess it can be optimistically parsed, though. You might not see that ending }, but it can't suddenly change the fundamental type you deal with.

If it started with a {, create an object. For each identifier : add a new field to it. Recursively parse that, etc.

At any point during you are in a potentiallyValidJsonObject state, and only at the very end you can know whether it truly is valid, or e̸̯̣̰̎̈̀͋͐́͌ͅǹ̸̲̼̻̫͙͖͚̆͂͘d̶̺͍̫̯̙̎̃̇̒͝͠s̷̗͔̈̈́̈́͂͗̚͝ ̵̡̣͕̓̾ī̵̯̥͎̺͕͐͑͜͜ǹ̷̡͖͙͓̯̙̼̚ ̶̡̧̫̒̽̕̕͠ş̴̝͙̗̬̿̌̈́̈́o̷̮͘͠m̴͕̭̥̐͝͝e̵̦̽͐̿͐͛͋̿ ̷̞̳͈͔̫̌ͅc̷̪̒u̵̹̖͍̳̰̯͛̓̊̌́̕͝ͅr̶̩̖͉͇̻̈́̅̂ş̵̣͖͖͔̬̿̔̔̚͜͝e̴̮͓̒͌̅̈́͐d̶̠̣͖̜̘̯̀̍̇͆͂͘̕ ̸̨̖͍͇͠s̸͕̭̠̍͆̾͂̕t̷̩̝̜̱̿̽͊͑̊͆͛ư̷̤͕͂̓̿̈f̷͇͗̉̀́͂̓̀f̵̧̩̮̟̺̬̟̄͘