diff options
| author | Jonathan Marler <johnnymarler@gmail.com> | 2023-08-05 21:56:00 -0600 |
|---|---|---|
| committer | Jonathan Marler <johnnymarler@gmail.com> | 2023-08-05 21:56:00 -0600 |
| commit | 7dacf7774523a454fce7216c13eb03c6badf8d7b (patch) | |
| tree | 4ae025fb9f546d09d3edeaada625dc89f880eca3 /lib/std/json/stringify.zig | |
| parent | 68f84964b3d80e5b976810208a14c31268a181e1 (diff) | |
| download | zig-7dacf7774523a454fce7216c13eb03c6badf8d7b.tar.gz zig-7dacf7774523a454fce7216c13eb03c6badf8d7b.zip | |
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.
Diffstat (limited to 'lib/std/json/stringify.zig')
| -rw-r--r-- | lib/std/json/stringify.zig | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/lib/std/json/stringify.zig b/lib/std/json/stringify.zig index 3e00531eaa..aa719e620b 100644 --- a/lib/std/json/stringify.zig +++ b/lib/std/json/stringify.zig @@ -33,6 +33,9 @@ pub const StringifyOptions = struct { /// Should unicode characters be escaped in strings? escape_unicode: bool = false, + + /// When true, renders numbers outside the range `±1<<53` (the precise integer range of f64) as JSON strings in base 10. + emit_big_numbers_quoted: bool = false, }; /// Writes the given value to the `std.io.Writer` stream. @@ -161,7 +164,7 @@ pub fn writeStreamArbitraryDepth( /// * Zig `bool` -> JSON `true` or `false`. /// * Zig `?T` -> `null` or the rendering of `T`. /// * Zig `i32`, `u64`, etc. -> JSON number or string. -/// * If the value is outside the range `±1<<53` (the precise integer rage of f64), it is rendered as a JSON string in base 10. Otherwise, it is rendered as JSON number. +/// * If the value is outside the range `±1<<53` (the precise integer range of f64), it is rendered as a JSON string in base 10. Otherwise, it is rendered as JSON number. /// * Zig floats -> JSON number or string. /// * If the value cannot be precisely represented by an f64, it is rendered as a JSON string. Otherwise, it is rendered as JSON number. /// * TODO: Float rendering will likely change in the future, e.g. to remove the unnecessary "e+00". @@ -400,20 +403,16 @@ pub fn WriteStream( const T = @TypeOf(value); switch (@typeInfo(T)) { .Int => |info| { - if (info.bits < 53) { - try self.valueStart(); - try self.stream.print("{}", .{value}); - self.valueDone(); - return; - } - if (value < 4503599627370496 and (info.signedness == .unsigned or value > -4503599627370496)) { - try self.valueStart(); + const emit_unquoted = + if (!self.options.emit_big_numbers_quoted) true + else if (info.bits < 53) true + else (value < 4503599627370496 and (info.signedness == .unsigned or value > -4503599627370496)); + try self.valueStart(); + if (emit_unquoted) { try self.stream.print("{}", .{value}); - self.valueDone(); - return; + } else { + try self.stream.print("\"{}\"", .{value}); } - try self.valueStart(); - try self.stream.print("\"{}\"", .{value}); self.valueDone(); return; }, |
