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,20 +2866,26 @@ 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);
} }
if (comptime std.meta.trait.hasFn("jsonFieldNameFor")(T)) { var field_written = false;
const name = value.jsonFieldNameFor(Field.name); if (comptime std.meta.trait.hasFn("jsonStringifyField")(T))
try stringify(name, options, out_stream); field_written = try value.jsonStringifyField(Field.name, child_options, out_stream);
} else {
try stringify(Field.name, options, out_stream);
}
try out_stream.writeByte(':'); if (!field_written) {
if (child_options.whitespace) |child_whitespace| { if (comptime std.meta.trait.hasFn("jsonFieldNameFor")(T)) {
if (child_whitespace.separator) { const name = value.jsonFieldNameFor(Field.name);
try out_stream.writeByte(' '); 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 (field_output) {
if (options.whitespace) |whitespace| { if (options.whitespace) |whitespace| {