mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 14:25:16 +00:00
std.json properly handle comptime int/float
This commit is contained in:
parent
ebbd137a0e
commit
b1ebaba408
@ -2194,7 +2194,7 @@ test "write json then parse it" {
|
||||
try jw.emitBool(true);
|
||||
|
||||
try jw.objectField("int");
|
||||
try jw.emitNumber(@as(i32, 1234));
|
||||
try jw.emitNumber(1234);
|
||||
|
||||
try jw.objectField("array");
|
||||
try jw.beginArray();
|
||||
@ -2203,7 +2203,7 @@ test "write json then parse it" {
|
||||
try jw.emitNull();
|
||||
|
||||
try jw.arrayElem();
|
||||
try jw.emitNumber(@as(f64, 12.34));
|
||||
try jw.emitNumber(12.34);
|
||||
|
||||
try jw.endArray();
|
||||
|
||||
|
||||
@ -148,7 +148,6 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
|
||||
self.popState();
|
||||
}
|
||||
|
||||
// TODO better handling of ComptimeInt and ComptimeFloat
|
||||
pub fn emitNumber(
|
||||
self: *Self,
|
||||
/// An integer, float, or `std.math.BigInt`. Emitted as a bare number if it fits losslessly
|
||||
@ -169,8 +168,11 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
|
||||
return;
|
||||
}
|
||||
},
|
||||
.Float => if (@floatCast(f64, value) == value) {
|
||||
try self.stream.print("{}", .{value});
|
||||
.ComptimeInt => {
|
||||
return self.emitNumber(@as(std.math.IntFittingRange(value, value), value));
|
||||
},
|
||||
.Float, .ComptimeFloat => if (@floatCast(f64, value) == value) {
|
||||
try self.stream.print("{}", .{@floatCast(f64, value)});
|
||||
self.popState();
|
||||
return;
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user