Techniques for parsing strings (and more JSON)

I’m not quite sure what it is you’re trying to show here.

As a tangent, page_allocator should probably not be used even for examples unless you have a specific reason to use it. It only allocates in multiples of the page size (usually 4096) regardless of the size you give it. If you ask for 1 byte, it’ll allocate 4096.

For example,

   var out = std.ArrayList(u8).init(std.heap.page_allocator);
   defer out.deinit();

this will allocate 4096 bytes (the page size) on the first append call. This is not really what you want.

Hard coding allocators (in your read function) is also something you want to avoid. Take the allocator as a parameter instead:

pub fn read(allocator: std.mem.Allocator, json: *const T, cmd: []const u8) !void {

This will allow callers to decide the allocation strategy they want to use, and will make it very simple to use std.testing.allocator during tests to get leak checking/double free checking/etc for free.

1 Like