authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-05-12 17:44:06+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-12 17:44:06+03:00
log80d0c2f166311c82ddb48c093df508c063973d02
treefcb4d53bfe4d7066c35dfd76af278b995c841142
parent08e2e690d75f9c91faf97e3281b95fa9a0aad47d
parentb1ebaba40811a6131152b9fc2eb8fb533e9fe5f3
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5118 from xackus/fix-json-writestream

fix json.WriteStream.emitJson

2 files changed, 38 insertions(+), 27 deletions(-)

lib/std/json.zig+2-2
...@@ -2194,7 +2194,7 @@ test "write json then parse it" {...@@ -2194,7 +2194,7 @@ test "write json then parse it" {
2194 try jw.emitBool(true);2194 try jw.emitBool(true);
21952195
2196 try jw.objectField("int");2196 try jw.objectField("int");
2197 try jw.emitNumber(@as(i32, 1234));2197 try jw.emitNumber(1234);
21982198
2199 try jw.objectField("array");2199 try jw.objectField("array");
2200 try jw.beginArray();2200 try jw.beginArray();
...@@ -2203,7 +2203,7 @@ test "write json then parse it" {...@@ -2203,7 +2203,7 @@ test "write json then parse it" {
2203 try jw.emitNull();2203 try jw.emitNull();
22042204
2205 try jw.arrayElem();2205 try jw.arrayElem();
2206 try jw.emitNumber(@as(f64, 12.34));2206 try jw.emitNumber(12.34);
22072207
2208 try jw.endArray();2208 try jw.endArray();
22092209
lib/std/json/write_stream.zig+36-25
...@@ -168,8 +168,11 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {...@@ -168,8 +168,11 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
168 return;168 return;
169 }169 }
170 },170 },
171 .Float => if (@floatCast(f64, value) == value) {171 .ComptimeInt => {
172 try self.stream.print("{}", .{value});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)});
173 self.popState();176 self.popState();
174 return;177 return;
175 },178 },
...@@ -180,6 +183,7 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {...@@ -180,6 +183,7 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
180 }183 }
181184
182 pub fn emitString(self: *Self, string: []const u8) !void {185 pub fn emitString(self: *Self, string: []const u8) !void {
186 assert(self.state[self.state_index] == State.Value);
183 try self.writeEscapedString(string);187 try self.writeEscapedString(string);
184 self.popState();188 self.popState();
185 }189 }
...@@ -191,7 +195,9 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {...@@ -191,7 +195,9 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
191195
192 /// Writes the complete json into the output stream196 /// Writes the complete json into the output stream
193 pub fn emitJson(self: *Self, json: std.json.Value) Stream.Error!void {197 pub fn emitJson(self: *Self, json: std.json.Value) Stream.Error!void {
198 assert(self.state[self.state_index] == State.Value);
194 try self.stringify(json);199 try self.stringify(json);
200 self.popState();
195 }201 }
196202
197 fn indent(self: *Self) !void {203 fn indent(self: *Self) !void {
...@@ -233,7 +239,32 @@ test "json write stream" {...@@ -233,7 +239,32 @@ test "json write stream" {
233 defer arena_allocator.deinit();239 defer arena_allocator.deinit();
234240
235 var w = std.json.writeStream(out, 10);241 var w = std.json.writeStream(out, 10);
236 try w.emitJson(try getJson(&arena_allocator.allocator));242
243 try w.beginObject();
244
245 try w.objectField("object");
246 try w.emitJson(try getJsonObject(&arena_allocator.allocator));
247
248 try w.objectField("string");
249 try w.emitString("This is a string");
250
251 try w.objectField("array");
252 try w.beginArray();
253 try w.arrayElem();
254 try w.emitString("Another string");
255 try w.arrayElem();
256 try w.emitNumber(@as(i32, 1));
257 try w.arrayElem();
258 try w.emitNumber(@as(f32, 3.5));
259 try w.endArray();
260
261 try w.objectField("int");
262 try w.emitNumber(@as(i32, 10));
263
264 try w.objectField("float");
265 try w.emitNumber(@as(f32, 3.5));
266
267 try w.endObject();
237268
238 const result = slice_stream.getWritten();269 const result = slice_stream.getWritten();
239 const expected =270 const expected =
...@@ -246,38 +277,18 @@ test "json write stream" {...@@ -246,38 +277,18 @@ test "json write stream" {
246 \\ "array": [277 \\ "array": [
247 \\ "Another string",278 \\ "Another string",
248 \\ 1,279 \\ 1,
249 \\ 3.14e+00280 \\ 3.5e+00
250 \\ ],281 \\ ],
251 \\ "int": 10,282 \\ "int": 10,
252 \\ "float": 3.14e+00283 \\ "float": 3.5e+00
253 \\}284 \\}
254 ;285 ;
255 std.testing.expect(std.mem.eql(u8, expected, result));286 std.testing.expect(std.mem.eql(u8, expected, result));
256}287}
257288
258fn getJson(allocator: *std.mem.Allocator) !std.json.Value {
259 var value = std.json.Value{ .Object = std.json.ObjectMap.init(allocator) };
260 _ = try value.Object.put("string", std.json.Value{ .String = "This is a string" });
261 _ = try value.Object.put("int", std.json.Value{ .Integer = @intCast(i64, 10) });
262 _ = try value.Object.put("float", std.json.Value{ .Float = 3.14 });
263 _ = try value.Object.put("array", try getJsonArray(allocator));
264 _ = try value.Object.put("object", try getJsonObject(allocator));
265 return value;
266}
267
268fn getJsonObject(allocator: *std.mem.Allocator) !std.json.Value {289fn getJsonObject(allocator: *std.mem.Allocator) !std.json.Value {
269 var value = std.json.Value{ .Object = std.json.ObjectMap.init(allocator) };290 var value = std.json.Value{ .Object = std.json.ObjectMap.init(allocator) };
270 _ = try value.Object.put("one", std.json.Value{ .Integer = @intCast(i64, 1) });291 _ = try value.Object.put("one", std.json.Value{ .Integer = @intCast(i64, 1) });
271 _ = try value.Object.put("two", std.json.Value{ .Float = 2.0 });292 _ = try value.Object.put("two", std.json.Value{ .Float = 2.0 });
272 return value;293 return value;
273}294}
274
275fn getJsonArray(allocator: *std.mem.Allocator) !std.json.Value {
276 var value = std.json.Value{ .Array = std.json.Array.init(allocator) };
277 var array = &value.Array;
278 _ = try array.append(std.json.Value{ .String = "Another string" });
279 _ = try array.append(std.json.Value{ .Integer = @intCast(i64, 1) });
280 _ = try array.append(std.json.Value{ .Float = 3.14 });
281
282 return value;
283}