authorgravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2023-08-06 09:25:21-06:00
committergravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2023-08-06 09:25:21-06:00
log2046880de868c50766d3cbfb6dd78b45c0aa39aa
tree8b3302bf4b53d0f5225d151aadf20e7d556a7217
parent1cce539ddcba1e21615e11dcf75f011074942414

std.json: josh review fixes

* renamed enum_big_numbers_quoted option to enum_nonportable_numbers_as_strings * updated stringify doc to mention the option I also reversed the logic to determine whether an integer is nonportable, it seemed easier to reason about. I also took a stab at applying the new option to floats, but, I got stuck at trying to print large floats, not sure if Zig supports that yet.

2 files changed, 8 insertions(+), 8 deletions(-)

lib/std/json/stringify.zig+6-6
......@@ -35,7 +35,7 @@ pub const StringifyOptions = struct {
3535 escape_unicode: bool = false,
3636
3737 /// When true, renders numbers outside the range `+-1<<53` (the precise integer range of f64) as JSON strings in base 10.
38 emit_big_numbers_quoted: bool = false,
38 emit_nonportable_numbers_as_strings: bool = false,
3939};
4040
4141/// Writes the given value to the `std.io.Writer` stream.
......@@ -164,7 +164,7 @@ pub fn writeStreamArbitraryDepth(
164164/// * Zig `bool` -> JSON `true` or `false`.
165165/// * Zig `?T` -> `null` or the rendering of `T`.
166166/// * Zig `i32`, `u64`, etc. -> JSON number or string.
167/// * 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.
167/// * 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.
168168/// * Zig floats -> JSON number or string.
169169/// * If the value cannot be precisely represented by an f64, it is rendered as a JSON string. Otherwise, it is rendered as JSON number.
170170/// * TODO: Float rendering will likely change in the future, e.g. to remove the unnecessary "e+00".
......@@ -404,12 +404,12 @@ pub fn WriteStream(
404404 switch (@typeInfo(T)) {
405405 .Int => {
406406 try self.valueStart();
407 if (!self.options.emit_big_numbers_quoted or
408 (value > -(1 << 53) and value < (1 << 53)))
407 if (self.options.emit_nonportable_numbers_as_strings and
408 (value <= -(1 << 53) or value >= (1 << 53)))
409409 {
410 try self.stream.print("{}", .{value});
411 } else {
412410 try self.stream.print("\"{}\"", .{value});
411 } else {
412 try self.stream.print("{}", .{value});
413413 }
414414 self.valueDone();
415415 return;
lib/std/json/stringify_test.zig+2-2
......@@ -434,7 +434,7 @@ test "print" {
434434 try std.testing.expectEqualStrings(expected, result);
435435}
436436
437test "big integers" {
437test "nonportable numbers" {
438438 try testStringify("9999999999999999", 9999999999999999, .{});
439 try testStringify("\"9999999999999999\"", 9999999999999999, .{ .emit_big_numbers_quoted = true });
439 try testStringify("\"9999999999999999\"", 9999999999999999, .{ .emit_nonportable_numbers_as_strings = true });
440440}