std.json properly handle comptime int/float

This commit is contained in:
Vexu 2020-05-12 15:15:21 +03:00
parent ebbd137a0e
commit b1ebaba408
No known key found for this signature in database
GPG Key ID: 59AEB8936E16A6AC
2 changed files with 7 additions and 5 deletions

View File

@ -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();

View File

@ -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;
},