I want to encode my struct with a HashMap type field to json to save to a file.
const Some = struct {
some_field: std.AutoHashMap(u64, u64),
};
var some_hash_map = std.AutoHashMap(u64, u64).init(allocator);
try some_hash_map.put(123, 321);
const some = Some{
.some_field = some_hash_map,
};
const string = try std.json.stringifyAlloc(allocator, some, .{});
std.debug.print("{s}\n", .{string});
When compiling I get these errors:
/home/d/zig/zig-0.12.0/lib/std/json/stringify.zig:552:52: error: cannot load opaque type 'anyopaque'
return self.write(value.*);
~~~~~^~
referenced by:
write__anon_8312: /home/d/zig/zig-0.12.0/lib/std/json/stringify.zig:533:43
write__anon_8170: /home/d/zig/zig-0.12.0/lib/std/json/stringify.zig:533:43
remaining reference traces hidden; use '-freference-trace' to see all reference traces
/home/d/zig/zig-0.12.0/lib/std/json/stringify.zig:557:29: error: unable to stringify type '[*]hash_map.HashMapUnmanaged(u64,u64,hash_map.AutoContext(u64),80).Metadata' without sentinel
@compileError("unable to stringify type '" ++ @typeName(T) ++ "' without sentinel");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/d/zig/zig-0.12.0/lib/std/json/stringify.zig:552:52: error: values of type 'fn (*anyopaque, usize, u8, usize) ?[*]u8' must be comptime-known, but operand value is runtime-known
return self.write(value.*);
~~~~~^~
/home/d/zig/zig-0.12.0/lib/std/json/stringify.zig:552:52: note: use '*const fn (*anyopaque, usize, u8, usize) ?[*]u8' for a function pointer type
/home/d/zig/zig-0.12.0/lib/std/json/stringify.zig:552:52: error: values of type 'fn (*anyopaque, []u8, u8, usize, usize) bool' must be comptime-known, but operand value is runtime-known
return self.write(value.*);
~~~~~^~
/home/d/zig/zig-0.12.0/lib/std/json/stringify.zig:552:52: note: use '*const fn (*anyopaque, []u8, u8, usize, usize) bool' for a function pointer type
/home/d/zig/zig-0.12.0/lib/std/json/stringify.zig:552:52: error: values of type 'fn (*anyopaque, []u8, u8, usize) void' must be comptime-known, but operand value is runtime-known
return self.write(value.*);
~~~~~^~
/home/d/zig/zig-0.12.0/lib/std/json/stringify.zig:552:52: note: use '*const fn (*anyopaque, []u8, u8, usize) void' for a function pointer type
I recently started learning this programming language, tried to do something similar and got stuck. How to do it right and make it work?