I usually test my code on https://zig-play.dev, and I came upon this error:
An error occurred:
/usr/local/bin/lib/std/fmt.zig:458:5: error: invalid format string 's' for type 'void'
@compileError("invalid format string '" ++ fmt ++ "' for type '" ++ @typeName(@TypeOf(value)) ++ "'");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
formatType__anon_6398: /usr/local/bin/lib/std/fmt.zig:489:38
format__anon_6386: /usr/local/bin/lib/std/fmt.zig:183:13
remaining reference traces hidden; use '-freference-trace' to see all reference traces
Here's my reference code. I found changing {s} (what we usually use for strings) to {any} solves it, but what other acceptable string formats are there for handling this? For reference, json.stringify() can return a void which seems to be what it trips over.
const std = @import("std");
const json = std.json;
const outWriter = std.io.getStdOut().writer();
pub fn main() !void {
const Foo = struct {
bar: usize,
baz: []const u8
};
const foo = Foo{ .bar = 5, .baz = "hello" };
const testJson = try json.stringify(foo, .{ .string = .Array }, outWriter);
std.debug.print("{any}", .{testJson}); // we originally use {s}
}