add ability to have custom stringification

This commit is contained in:
Emil Lerch 2021-09-05 14:32:52 -07:00
parent c6b3f8d6e1
commit c80a65ed50
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -1410,7 +1410,7 @@ fn parsedEqual(a: anytype, b: @TypeOf(a)) bool {
if (a == null or b == null) return false; if (a == null or b == null) return false;
return parsedEqual(a.?, b.?); return parsedEqual(a.?, b.?);
}, },
.Union => { .Union => |info| {
if (info.tag_type) |UnionTag| { if (info.tag_type) |UnionTag| {
const tag_a = std.meta.activeTag(a); const tag_a = std.meta.activeTag(a);
const tag_b = std.meta.activeTag(b); const tag_b = std.meta.activeTag(b);
@ -2866,6 +2866,11 @@ pub fn stringify(
try out_stream.writeByte('\n'); try out_stream.writeByte('\n');
try child_whitespace.outputIndent(out_stream); try child_whitespace.outputIndent(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);
if (!field_written) {
if (comptime std.meta.trait.hasFn("jsonFieldNameFor")(T)) { if (comptime std.meta.trait.hasFn("jsonFieldNameFor")(T)) {
const name = value.jsonFieldNameFor(Field.name); const name = value.jsonFieldNameFor(Field.name);
try stringify(name, options, out_stream); try stringify(name, options, out_stream);
@ -2881,6 +2886,7 @@ pub fn stringify(
} }
try stringify(@field(value, Field.name), child_options, out_stream); try stringify(@field(value, Field.name), child_options, out_stream);
} }
}
if (field_output) { if (field_output) {
if (options.whitespace) |whitespace| { if (options.whitespace) |whitespace| {
try out_stream.writeByte('\n'); try out_stream.writeByte('\n');