kj4tmp
1
What is the recommended way to parse and serialize a string-string map to/from JSON?
Here is an example json:
{
"user23" : "john doe",
"user26" : "jane eyre"
}
I’m not sure how best to represent this in zig. It could be StringArrayHashMap or []const struct {[]const u8, []const u8}.
vulpesx
2
std.json.ArrayHashMap would be the easiest, it is a wrapper around StringArrayHashMap and just provides json parsing/stringifying.
define ‘best’, what are your requirements?
1 Like
kj4tmp
3
Thanks, for others looking at this, just put std.json.ArrayHashMap as the type.
In this example, annotations is a string-string map.
/// Ref: https://specs.opencontainers.org/image-spec/image-index/?v=v1.1.1
const Index = @This();
const std = @import("std");
const Descriptor = @import("Descriptor.zig");
schemaVersion: u8 = 2,
mediaType: []const u8,
artifactType: ?[]const u8 = null,
manifests: []const Descriptor,
subject: ?Descriptor = null,
annotations: ?std.json.ArrayHashMap([]const u8) = null,
const index: oci.spec.image.Index = .{
.mediaType = "application/vnd.oci.image.index.v1+json",
.manifests = manifest_descriptors.items,
.annotations = .{ .map = try .init(arena, &.{"org.opencontainers.image.ref.name"}, &.{args.tag}) },
};
var index_file = try std.fs.createFileAbsolute(args.output_path_index_json, .{});
defer index_file.close();
var index_file_writer_buffer: [4096]u8 = undefined;
var index_file_writer = index_file.writer(&index_file_writer_buffer);
var stringifier = std.json.Stringify{
.writer = &index_file_writer.interface,
.options = .{
.emit_null_optional_fields = false,
.whitespace = .indent_4,
},
};
try stringifier.write(index);
try index_file_writer.interface.flush();