authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-05-12 15:15:21+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-05-12 15:15:21+03:00
logb1ebaba40811a6131152b9fc2eb8fb533e9fe5f3
tree9bc8cd0e095ce28bbc1fa46f9a76f9c719fddd8a
parentebbd137a0e90a8e78655d2a5f74cf87120b5a5a1
signature Commit is signed but in an unrecognized format.

std.json properly handle comptime int/float


2 files changed, 7 insertions(+), 5 deletions(-)

lib/std/json.zig+2-2
......@@ -2194,7 +2194,7 @@ test "write json then parse it" {
21942194 try jw.emitBool(true);
21952195
21962196 try jw.objectField("int");
2197 try jw.emitNumber(@as(i32, 1234));
2197 try jw.emitNumber(1234);
21982198
21992199 try jw.objectField("array");
22002200 try jw.beginArray();
......@@ -2203,7 +2203,7 @@ test "write json then parse it" {
22032203 try jw.emitNull();
22042204
22052205 try jw.arrayElem();
2206 try jw.emitNumber(@as(f64, 12.34));
2206 try jw.emitNumber(12.34);
22072207
22082208 try jw.endArray();
22092209
lib/std/json/write_stream.zig+5-3
......@@ -148,7 +148,6 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
148148 self.popState();
149149 }
150150
151 // TODO better handling of ComptimeInt and ComptimeFloat
152151 pub fn emitNumber(
153152 self: *Self,
154153 /// 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 {
169168 return;
170169 }
171170 },
172 .Float => if (@floatCast(f64, value) == value) {
173 try self.stream.print("{}", .{value});
171 .ComptimeInt => {
172 return self.emitNumber(@as(std.math.IntFittingRange(value, value), value));
173 },
174 .Float, .ComptimeFloat => if (@floatCast(f64, value) == value) {
175 try self.stream.print("{}", .{@floatCast(f64, value)});
174176 self.popState();
175177 return;
176178 },