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
Lessss gooooo JSON serialization in Mojo π₯
I'm surprised how few lines of code is required to make a simple parser
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.
@toasty has updated his great StringBuilder implementation to use recently introduced
Span
s, maybe that would be helpful?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:
I donβt doubt that with a little time and love that this could be pretty good