January 27, 2026, 7:30pm 1
Zig supports JSON serialization. How can this be used for complex real-world tasks?
I need to parse an object, which contains strings and arrays of strings. I have the idea to use a Zig struct for the object. For the strings I use slices of u8. But how can I use what for an array of strings? My first idea is to use std.ArrayList([] u8) but I’m not sure if that is what the authors of the stdlib intend me to use here. What is it?
tholmes January 27, 2026, 7:47pm 2
The Programming Without Pointers style comes to mind.
Basically, have a std.ArrayList(u8), and then have something like a second std.ArrayList(u32) for the string lengths, or perhaps offsets. (Offsets can be good; to slice the items, you just use the of…
January 27, 2026, 7:30pm 1
Zig supports JSON serialization. How can this be used for complex real-world tasks?
I need to parse an object, which contains strings and arrays of strings. I have the idea to use a Zig struct for the object. For the strings I use slices of u8. But how can I use what for an array of strings? My first idea is to use std.ArrayList([] u8) but I’m not sure if that is what the authors of the stdlib intend me to use here. What is it?
tholmes January 27, 2026, 7:47pm 2
The Programming Without Pointers style comes to mind.
Basically, have a std.ArrayList(u8), and then have something like a second std.ArrayList(u32) for the string lengths, or perhaps offsets. (Offsets can be good; to slice the items, you just use the offset at the current index and the offset at the next index).
If you were storing an array of slices, you’d be storing a pointer with length to a list of pointers with lengths; by using this method, you’re storing one pointer with length, and then a list of lengths with no separate pointers.
On 64-bit architectures, you get a further benefit in that the string length/offset is stored smaller than it would be with a usize.
At deserialization time, when you encounter a new string, you appendSlice() it into the std.ArrayList(u8) and append() its length into the std.ArrayList(u32), and then how you store it in your representation of the JSON object is basically your own choice.
You can store a slice into the items of the std.ArrayList(u8), or you can store the index of the u32 length/offset as a cheaper type of handle.
In a similar sense, when you encounter an array of strings inside a JSON object, you can return a slice of the std.ArrayList(u32) as a compact representation of that array.
vulpesx January 28, 2026, 1:01am 3
you can just use slices, the parser/serialiser understands them.
If you need anything it doesn’t understand, like an ArrayList, then you will need a custom jsonParse/jsonStringify function, they can be on the specific type it needs or a wrapper or on the larger type in question.
fdik January 28, 2026, 6:02am 4
Where can I find documentation what it understands and how it will generate JSON out of it?
What type do I deliver so it will generate an object? What type do I deliver so it will generate an array?
vulpesx January 28, 2026, 7:13am 5
as always, std docs are a mess, there is some here and there, but the source is easier to find and is readable, innerParse is most useful here.
it understands primitive types, and has default logic for compound/user defined types, such types can override that with the functions I mentioned.
The mapping with json is:
object - struct, tagged union
array - slice, array, tuple, vector
string - slice of u8, enum
bool - bool
null - optional
number (can be in string form) - ints/floats, enum
std.json.Value is a tagged union for dynamic json values, implemented with the afore mentioned override functions.
jsonParse should have the signature fn(Allocator, token_source: anytype, json.Options) json.ParseError(@TypeOf(token_source.*)!T, T is the type being parsed and token_source is either *json.Scanner or *json.Reader (these should be combined eventually). It’s common to just infer the errorset.
jsonStringify is fn(json.Stringify) json.Stringify.Error!void
the stringify API is better documented as it was more recently overhauled.
I can also answer more specific questions if you need.