Hello! I'm trying to use the new std.json interface to efficiently parse json, and I'm experiencing 2 issues. It's likely that I just don't know how to use the interface very well, so here are my questions that someone might be able to fix:
- The Value polymorphic parsing seems useful, but pretty wasteful for my use case.
- Attempting to parse from a Scanner into a struct containing strings appears to cause some sort of memory corruption (The strings reference the internal Reader's long-gone memory).
I have a json that looks like this:
{
"outer": {
"key1": {
// Large struct with known keys/values, including arrays and strings
},
"key2": {
// Same struct
},
// Many more unknown keys containing the same struct.
}
}
So, my json is only partially polymorphic. My understanding is that parsing the entire json with the std.json.Value type essentially creates a bunch of nested StringArrayHashMaps all the way down. Really, what I want is one StringArrayHashMap, mapping the string keys (key1, key2, ...) to the actual struct. I could parse the whole thing to this Value type, then pull my struct out of them... but I'm learning Zig to be fast an efficient
! Instead, What would be nice would be to tell the parser to only make 1 hashmap. Here's a solution that I came up with, which actually works really well!
<message too long, see solution below>
However, this has an issue; it totally doesn't work. Specifically because of this pesky line here:
https://github.com/ziglang/zig/blob/689f3163af48fd6e0c08bb76fadfb711db30c97c/lib/std/json/static.zig#L128
Basically, all of the std.json.parseFrom* functions won't let you parse partial json objects with them. Which I guess makes sense, it prevents you from parsing malformed json, it just blocks us from being able to do this kind of parsing while streaming.
<long post continued below>