Here is one in 73 lines of F# by Jon Harrop: https://gist.github.com/isaksky/6681cfad8ced1708a04b2eca92fc...
Parsing JSON in 500 lines of Rust
11–20 of 59 posts
Re: Parsing JSON in 500 lines of Rust
#12I'm pretty sure you'd already know of this, but once you've written your own version, it might help to compare and take notes from a popular, well-established, benchmarked library: https://github.com/serde-rs/json.
Re: Parsing JSON in 500 lines of Rust
#13I had a goal of making it "async", so it could periodically yield for large input strings, but I'm not so sure it matters much.
Currently the API is pretty impractical to use, because of the arena structure that is set up. It could be improved
Re: Parsing JSON in 500 lines of Rust
#14Re: Parsing JSON in 500 lines of Rust
#15It'd be easy and cleaner to use miette for pretty error printing.
Re: Parsing JSON in 500 lines of Rust
#16I once (maybe a long time ago?) made a parser for JSON by: 1. Reading the entire file into RAM. 2. Providing a `const char *get_value(const char *jstring, const char *path, ...)` function with a NULL-terminated parameter list that would return the position of the value of the key at the specified path. 3. Providing a `copy_value(const char *position)` function to copy the value at the specified position. Slow? Yup! B…
Re: Parsing JSON in 500 lines of Rust
#17Re: Parsing JSON in 500 lines of Rust
#18match object(src) { Ok(res) => return Ok(res), Err(JSONParseError::NotFound) => {} // if not found, that ok Err(e) => return Err(e), } You probably have realized that this is really tedious, and this is where macros would really shine: macro_rules! try_parse_as { ($f:expr) => ( match $f(src) { Ok(res) => return Ok(res), Err(JSONParseError::NotFound) => {} // if not found, that ok Err(e) => return Err(e), } ); } try_p…
I wonder, given a 500 line problem, can any of the current cutting edge AIs make the code obviously, dramatically better?
How far can they go?
Re: Parsing JSON in 500 lines of Rust
#19match object(src) { Ok(res) => return Ok(res), Err(JSONParseError::NotFound) => {} // if not found, that ok Err(e) => return Err(e), } You probably have realized that this is really tedious, and this is where macros would really shine: macro_rules! try_parse_as { ($f:expr) => ( match $f(src) { Ok(res) => return Ok(res), Err(JSONParseError::NotFound) => {} // if not found, that ok Err(e) => return Err(e), } ); } try_p…
Just for the sake of completeness, and not to imply that you don't know this, but the JSON spec doesn't limit the size or precision of numbers, although it allows implementations set other limits.
I have encountered JSON documents that (annoyingly) required the use of a parser with bigint-support.