std.json: fix roundtrip stringify for large integers

std.json follows interoperability recommendations from RFC8259 to limit
JSON number values to those that fit inside an f64.  However, since Zig
supports arbitrarily large JSON numbers, this breaks roundtrip data
congruence.

To appease both use cases, I've added an option `emit_big_numbers_quoted`
to StringifyOptions.  It's disabled by default which preserves roundtrip
but can be enabled to favor interoperability.
This commit is contained in:
Jonathan Marler
2023-08-05 21:56:00 -06:00
parent 68f84964b3
commit 7dacf77745
2 changed files with 19 additions and 14 deletions

View File

@@ -126,6 +126,7 @@ test "stringify basic types" {
try testStringify("4.2e+01", 42.0, .{});
try testStringify("42", @as(u8, 42), .{});
try testStringify("42", @as(u128, 42), .{});
try testStringify("9999999999999999", 9999999999999999, .{});
try testStringify("4.2e+01", @as(f32, 42), .{});
try testStringify("4.2e+01", @as(f64, 42), .{});
try testStringify("\"ItBroke\"", @as(anyerror, error.ItBroke), .{});
@@ -432,3 +433,8 @@ test "print" {
;
try std.testing.expectEqualStrings(expected, result);
}
test "big integers" {
try testStringify("9999999999999999", 9999999999999999, .{});
try testStringify("\"9999999999999999\"", 9999999999999999, .{ .emit_big_numbers_quoted = true });
}