Commit ebbd137a0e
Changed files (1)
lib
std
json
lib/std/json/write_stream.zig
@@ -148,6 +148,7 @@ 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
@@ -180,6 +181,7 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
}
pub fn emitString(self: *Self, string: []const u8) !void {
+ assert(self.state[self.state_index] == State.Value);
try self.writeEscapedString(string);
self.popState();
}
@@ -191,7 +193,9 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
/// Writes the complete json into the output stream
pub fn emitJson(self: *Self, json: std.json.Value) Stream.Error!void {
+ assert(self.state[self.state_index] == State.Value);
try self.stringify(json);
+ self.popState();
}
fn indent(self: *Self) !void {
@@ -233,7 +237,32 @@ test "json write stream" {
defer arena_allocator.deinit();
var w = std.json.writeStream(out, 10);
- try w.emitJson(try getJson(&arena_allocator.allocator));
+
+ try w.beginObject();
+
+ try w.objectField("object");
+ try w.emitJson(try getJsonObject(&arena_allocator.allocator));
+
+ try w.objectField("string");
+ try w.emitString("This is a string");
+
+ try w.objectField("array");
+ try w.beginArray();
+ try w.arrayElem();
+ try w.emitString("Another string");
+ try w.arrayElem();
+ try w.emitNumber(@as(i32, 1));
+ try w.arrayElem();
+ try w.emitNumber(@as(f32, 3.5));
+ try w.endArray();
+
+ try w.objectField("int");
+ try w.emitNumber(@as(i32, 10));
+
+ try w.objectField("float");
+ try w.emitNumber(@as(f32, 3.5));
+
+ try w.endObject();
const result = slice_stream.getWritten();
const expected =
@@ -246,38 +275,18 @@ test "json write stream" {
\\ "array": [
\\ "Another string",
\\ 1,
- \\ 3.14e+00
+ \\ 3.5e+00
\\ ],
\\ "int": 10,
- \\ "float": 3.14e+00
+ \\ "float": 3.5e+00
\\}
;
std.testing.expect(std.mem.eql(u8, expected, result));
}
-fn getJson(allocator: *std.mem.Allocator) !std.json.Value {
- var value = std.json.Value{ .Object = std.json.ObjectMap.init(allocator) };
- _ = try value.Object.put("string", std.json.Value{ .String = "This is a string" });
- _ = try value.Object.put("int", std.json.Value{ .Integer = @intCast(i64, 10) });
- _ = try value.Object.put("float", std.json.Value{ .Float = 3.14 });
- _ = try value.Object.put("array", try getJsonArray(allocator));
- _ = try value.Object.put("object", try getJsonObject(allocator));
- return value;
-}
-
fn getJsonObject(allocator: *std.mem.Allocator) !std.json.Value {
var value = std.json.Value{ .Object = std.json.ObjectMap.init(allocator) };
_ = try value.Object.put("one", std.json.Value{ .Integer = @intCast(i64, 1) });
_ = try value.Object.put("two", std.json.Value{ .Float = 2.0 });
return value;
}
-
-fn getJsonArray(allocator: *std.mem.Allocator) !std.json.Value {
- var value = std.json.Value{ .Array = std.json.Array.init(allocator) };
- var array = &value.Array;
- _ = try array.append(std.json.Value{ .String = "Another string" });
- _ = try array.append(std.json.Value{ .Integer = @intCast(i64, 1) });
- _ = try array.append(std.json.Value{ .Float = 3.14 });
-
- return value;
-}