mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 22:35:24 +00:00
Merge pull request #5118 from xackus/fix-json-writestream
fix json.WriteStream.emitJson
This commit is contained in:
commit
80d0c2f166
@ -2194,7 +2194,7 @@ test "write json then parse it" {
|
||||
try jw.emitBool(true);
|
||||
|
||||
try jw.objectField("int");
|
||||
try jw.emitNumber(@as(i32, 1234));
|
||||
try jw.emitNumber(1234);
|
||||
|
||||
try jw.objectField("array");
|
||||
try jw.beginArray();
|
||||
@ -2203,7 +2203,7 @@ test "write json then parse it" {
|
||||
try jw.emitNull();
|
||||
|
||||
try jw.arrayElem();
|
||||
try jw.emitNumber(@as(f64, 12.34));
|
||||
try jw.emitNumber(12.34);
|
||||
|
||||
try jw.endArray();
|
||||
|
||||
|
||||
@ -168,8 +168,11 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
|
||||
return;
|
||||
}
|
||||
},
|
||||
.Float => if (@floatCast(f64, value) == value) {
|
||||
try self.stream.print("{}", .{value});
|
||||
.ComptimeInt => {
|
||||
return self.emitNumber(@as(std.math.IntFittingRange(value, value), value));
|
||||
},
|
||||
.Float, .ComptimeFloat => if (@floatCast(f64, value) == value) {
|
||||
try self.stream.print("{}", .{@floatCast(f64, value)});
|
||||
self.popState();
|
||||
return;
|
||||
},
|
||||
@ -180,6 +183,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 +195,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 +239,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 +277,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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user