From c80a65ed501c73062353d4eeec9c91b55a81281c Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Sun, 5 Sep 2021 14:32:52 -0700 Subject: [PATCH] add ability to have custom stringification --- src/json.zig | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/json.zig b/src/json.zig index 9934069..10eaf32 100644 --- a/src/json.zig +++ b/src/json.zig @@ -1410,7 +1410,7 @@ fn parsedEqual(a: anytype, b: @TypeOf(a)) bool { if (a == null or b == null) return false; return parsedEqual(a.?, b.?); }, - .Union => { + .Union => |info| { if (info.tag_type) |UnionTag| { const tag_a = std.meta.activeTag(a); const tag_b = std.meta.activeTag(b); @@ -2866,20 +2866,26 @@ pub fn stringify( try out_stream.writeByte('\n'); try child_whitespace.outputIndent(out_stream); } - if (comptime std.meta.trait.hasFn("jsonFieldNameFor")(T)) { - const name = value.jsonFieldNameFor(Field.name); - try stringify(name, options, out_stream); - } else { - try stringify(Field.name, options, out_stream); - } + var field_written = false; + if (comptime std.meta.trait.hasFn("jsonStringifyField")(T)) + field_written = try value.jsonStringifyField(Field.name, child_options, out_stream); - try out_stream.writeByte(':'); - if (child_options.whitespace) |child_whitespace| { - if (child_whitespace.separator) { - try out_stream.writeByte(' '); + if (!field_written) { + if (comptime std.meta.trait.hasFn("jsonFieldNameFor")(T)) { + const name = value.jsonFieldNameFor(Field.name); + try stringify(name, options, out_stream); + } else { + try stringify(Field.name, options, out_stream); } + + try out_stream.writeByte(':'); + if (child_options.whitespace) |child_whitespace| { + if (child_whitespace.separator) { + try out_stream.writeByte(' '); + } + } + try stringify(@field(value, Field.name), child_options, out_stream); } - try stringify(@field(value, Field.name), child_options, out_stream); } if (field_output) { if (options.whitespace) |whitespace| {