diff --git a/lib/std/json/stringify.zig b/lib/std/json/stringify.zig index 81a09d26ba..1654da421c 100644 --- a/lib/std/json/stringify.zig +++ b/lib/std/json/stringify.zig @@ -35,7 +35,7 @@ pub const StringifyOptions = struct { 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, + emit_nonportable_numbers_as_strings: bool = false, }; /// Writes the given value to the `std.io.Writer` stream. @@ -164,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 range of f64), it is rendered as a JSON string in base 10. Otherwise, it is rendered as JSON number. +/// * When option `emit_nonportable_numbers_as_strings` is true, 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". @@ -404,12 +404,12 @@ pub fn WriteStream( switch (@typeInfo(T)) { .Int => { try self.valueStart(); - if (!self.options.emit_big_numbers_quoted or - (value > -(1 << 53) and value < (1 << 53))) + if (self.options.emit_nonportable_numbers_as_strings and + (value <= -(1 << 53) or value >= (1 << 53))) { - try self.stream.print("{}", .{value}); - } else { try self.stream.print("\"{}\"", .{value}); + } else { + try self.stream.print("{}", .{value}); } self.valueDone(); return; diff --git a/lib/std/json/stringify_test.zig b/lib/std/json/stringify_test.zig index c2c6b6f5e7..cc825ff64a 100644 --- a/lib/std/json/stringify_test.zig +++ b/lib/std/json/stringify_test.zig @@ -434,7 +434,7 @@ test "print" { try std.testing.expectEqualStrings(expected, result); } -test "big integers" { +test "nonportable numbers" { try testStringify("9999999999999999", 9999999999999999, .{}); - try testStringify("\"9999999999999999\"", 9999999999999999, .{ .emit_big_numbers_quoted = true }); + try testStringify("\"9999999999999999\"", 9999999999999999, .{ .emit_nonportable_numbers_as_strings = true }); }