authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-04-21 10:25:09+02:00
committergravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-04-23 22:14:23+02:00
logebbd137a0e90a8e78655d2a5f74cf87120b5a5a1
tree9c5db94755d862da4ad7cbf0c1d3b1c990932d9b
parenta9eb4a6740e0bb00e1984de3768a7de6001c25dd

fix json.WriteStream.emitJson


1 files changed, 32 insertions(+), 23 deletions(-)

lib/std/json/write_stream.zig+32-23
...@@ -148,6 +148,7 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {...@@ -148,6 +148,7 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
148 self.popState();148 self.popState();
149 }149 }
150150
151 // TODO better handling of ComptimeInt and ComptimeFloat
151 pub fn emitNumber(152 pub fn emitNumber(
152 self: *Self,153 self: *Self,
153 /// An integer, float, or `std.math.BigInt`. Emitted as a bare number if it fits losslessly154 /// An integer, float, or `std.math.BigInt`. Emitted as a bare number if it fits losslessly
...@@ -180,6 +181,7 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {...@@ -180,6 +181,7 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
180 }181 }
181182
182 pub fn emitString(self: *Self, string: []const u8) !void {183 pub fn emitString(self: *Self, string: []const u8) !void {
184 assert(self.state[self.state_index] == State.Value);
183 try self.writeEscapedString(string);185 try self.writeEscapedString(string);
184 self.popState();186 self.popState();
185 }187 }
...@@ -191,7 +193,9 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {...@@ -191,7 +193,9 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
191193
192 /// Writes the complete json into the output stream194 /// Writes the complete json into the output stream
193 pub fn emitJson(self: *Self, json: std.json.Value) Stream.Error!void {195 pub fn emitJson(self: *Self, json: std.json.Value) Stream.Error!void {
196 assert(self.state[self.state_index] == State.Value);
194 try self.stringify(json);197 try self.stringify(json);
198 self.popState();
195 }199 }
196200
197 fn indent(self: *Self) !void {201 fn indent(self: *Self) !void {
...@@ -233,7 +237,32 @@ test "json write stream" {...@@ -233,7 +237,32 @@ test "json write stream" {
233 defer arena_allocator.deinit();237 defer arena_allocator.deinit();
234238
235 var w = std.json.writeStream(out, 10);239 var w = std.json.writeStream(out, 10);
236 try w.emitJson(try getJson(&arena_allocator.allocator));240
241 try w.beginObject();
242
243 try w.objectField("object");
244 try w.emitJson(try getJsonObject(&arena_allocator.allocator));
245
246 try w.objectField("string");
247 try w.emitString("This is a string");
248
249 try w.objectField("array");
250 try w.beginArray();
251 try w.arrayElem();
252 try w.emitString("Another string");
253 try w.arrayElem();
254 try w.emitNumber(@as(i32, 1));
255 try w.arrayElem();
256 try w.emitNumber(@as(f32, 3.5));
257 try w.endArray();
258
259 try w.objectField("int");
260 try w.emitNumber(@as(i32, 10));
261
262 try w.objectField("float");
263 try w.emitNumber(@as(f32, 3.5));
264
265 try w.endObject();
237266
238 const result = slice_stream.getWritten();267 const result = slice_stream.getWritten();
239 const expected =268 const expected =
...@@ -246,38 +275,18 @@ test "json write stream" {...@@ -246,38 +275,18 @@ test "json write stream" {
246 \\ "array": [275 \\ "array": [
247 \\ "Another string",276 \\ "Another string",
248 \\ 1,277 \\ 1,
249 \\ 3.14e+00278 \\ 3.5e+00
250 \\ ],279 \\ ],
251 \\ "int": 10,280 \\ "int": 10,
252 \\ "float": 3.14e+00281 \\ "float": 3.5e+00
253 \\}282 \\}
254 ;283 ;
255 std.testing.expect(std.mem.eql(u8, expected, result));284 std.testing.expect(std.mem.eql(u8, expected, result));
256}285}
257286
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 {287fn getJsonObject(allocator: *std.mem.Allocator) !std.json.Value {
269 var value = std.json.Value{ .Object = std.json.ObjectMap.init(allocator) };288 var value = std.json.Value{ .Object = std.json.ObjectMap.init(allocator) };
270 _ = try value.Object.put("one", std.json.Value{ .Integer = @intCast(i64, 1) });289 _ = try value.Object.put("one", std.json.Value{ .Integer = @intCast(i64, 1) });
271 _ = try value.Object.put("two", std.json.Value{ .Float = 2.0 });290 _ = try value.Object.put("two", std.json.Value{ .Float = 2.0 });
272 return value;291 return value;
273}292}
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}