M
Modularβ€’6mo ago
Maxim

Quick and Dirty JSON parser

I hacked together a JSON parser. https://gist.github.com/mzaks/c1947212fd23ba010129d2f6b7c0fdfa As it is just a hack I will not even put it into a proper repo, but I think it might be useful as a learning exercise, or for some other hacks πŸ™‚
Gist
Quick and Dirty JSON parser in Mojo
Quick and Dirty JSON parser in Mojo. GitHub Gist: instantly share code, notes, and snippets.
5 Replies
a2svior
a2sviorβ€’6mo ago
Lessss gooooo JSON serialization in Mojo πŸ”₯ I'm surprised how few lines of code is required to make a simple parser
Maxim
MaximOPβ€’6mo ago
There are still a few special cases which are not covered, like scientific notation for numeric values, hex and escaping stuff. But generally this should be it. It is a slow implementation as I am duplicating Strings everywhere.
a2svior
a2sviorβ€’6mo ago
@toasty has updated his great StringBuilder implementation to use recently introduced Spans, maybe that would be helpful?
Maxim
MaximOPβ€’6mo ago
Best case scenario JSONValue has a reference to the initial string and start, stop indices. This way no copying needed. We still scan through the string a lot but we don’t need to copy it all the time. Also just realized there is a bug. [β€œa”, β€œ]”, β€œb”] would not parse correctly. Will fix tomorrow. Now I am already on myπŸ“±. Or maybe someone else wants to take a stab at it? Updated the Gist with a more robust version of the parser and a few tests for the previous buggy cases like:
{ "a": [1, "]", 3] }
[ [1, "]", 3] ]
[ {"a": 1, "b": "}", "c": 3} ]
{"first": {"a": 1, "b": "}", "c": 3}, "second" : true }
{ "a": [1, "]", 3] }
[ [1, "]", 3] ]
[ {"a": 1, "b": "}", "c": 3} ]
{"first": {"a": 1, "b": "}", "c": 3}, "second" : true }
Moosems / Three chickens
I don’t doubt that with a little time and love that this could be pretty good
Want results from more Discord servers?
Add your server