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" {
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+36-25
......@@ -168,8 +168,11 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
168168 return;
169169 }
170170 },
171 .Float => if (@floatCast(f64, value) == value) {
172 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)});
173176 self.popState();
174177 return;
175178 },
......@@ -180,6 +183,7 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
180183 }
181184
182185 pub fn emitString(self: *Self, string: []const u8) !void {
186 assert(self.state[self.state_index] == State.Value);
183187 try self.writeEscapedString(string);
184188 self.popState();
185189 }
......@@ -191,7 +195,9 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
191195
192196 /// Writes the complete json into the output stream
193197 pub fn emitJson(self: *Self, json: std.json.Value) Stream.Error!void {
198 assert(self.state[self.state_index] == State.Value);
194199 try self.stringify(json);
200 self.popState();
195201 }
196202
197203 fn indent(self: *Self) !void {
......@@ -233,7 +239,32 @@ test "json write stream" {
233239 defer arena_allocator.deinit();
234240
235241 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
238269 const result = slice_stream.getWritten();
239270 const expected =
......@@ -246,38 +277,18 @@ test "json write stream" {
246277 \\ "array": [
247278 \\ "Another string",
248279 \\ 1,
249 \\ 3.14e+00
280 \\ 3.5e+00
250281 \\ ],
251282 \\ "int": 10,
252 \\ "float": 3.14e+00
283 \\ "float": 3.5e+00
253284 \\}
254285 ;
255286 std.testing.expect(std.mem.eql(u8, expected, result));
256287}
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
268289fn getJsonObject(allocator: *std.mem.Allocator) !std.json.Value {
269290 var value = std.json.Value{ .Object = std.json.ObjectMap.init(allocator) };
270291 _ = try value.Object.put("one", std.json.Value{ .Integer = @intCast(i64, 1) });
271292 _ = try value.Object.put("two", std.json.Value{ .Float = 2.0 });
272293 return value;
273294}
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}