Looks like you have to add a second parameter when calling std.json.fmt
, a StringifyOptions struct. Using default field values could be OK, is this case just do
std.json.fmt(response, .{})
Or go with one of the indentation options instead:
std.json.fmt(response, .{ .whitespace = .indent_4 })
Also, today I needed to pretty print some structs for debugging purposes, and I have not noticed this std.json.fmt
trick, so… I wrote a quick and dirty pretty print for myself. Here it is.
Had I known about std.json.fmt
, I would not have spent my time writing it. On the other hand, my code (which does not cover many types, e.g. optionals, enums, unions) still produces a different printout compared to std.json.fmt
- for example it prints field types.
Also you can change this code however you want, if you need different output. Sure, you can do the same with std.json.fmt
, but I guess if you also use it for its intended purpose, this could become a hassle.
Sample output of the linked code:
{
comptime x: u8 = 123
tea: u32 = 123
lemon: f32 = 1.2300000190734863
cake: []const u8 = "Yes, please!"
ham: test_ret_anon_struct_3.main.Eggs.Ham =
{
bread: i16 = 123
butter: f32 = 1.2300000190734863
nails: [2]i32 = [ 123, 123, ]
cream: test_ret_anon_struct_3.main.Eggs.Ham.Milk =
{
cow: u8 = 123
soy: i8 = 123
almond: bool = true
}
more_cream: [2]test_ret_anon_struct_3.main.Eggs.Ham.Milk = [ { cow: u8 = 123, soy: i8 = 123, almond: bool = true, }, { cow: u8 = 123, soy: i8 = 123, almond: bool = true, }, ]
nuts: [2][2]u8 = [ [ 123, 123, ], [ 123, 123, ], ]
}
bells: [3]u8 = [ 123, 123, 123, ]
pointer: *u32 = u32@7ffd56d0fa8c
optional: ?*u32 = -
}
BTW, I made it into a small library.