-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathwrite_stream.zig
300 lines (262 loc) · 9.83 KB
/
write_stream.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const std = @import("std");
const assert = std.debug.assert;
const maxInt = std.math.maxInt;
const StringifyOptions = @import("./stringify.zig").StringifyOptions;
const jsonStringify = @import("./stringify.zig").stringify;
const Value = @import("./dynamic.zig").Value;
const State = enum {
complete,
value,
array_start,
array,
object_start,
object,
};
/// Writes JSON ([RFC8259](https://tools.ietf.org/html/rfc8259)) formatted data
/// to a stream. `max_depth` is a comptime-known upper bound on the nesting depth.
/// TODO A future iteration of this API will allow passing `null` for this value,
/// and disable safety checks in release builds.
pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
return struct {
const Self = @This();
pub const Stream = OutStream;
whitespace: StringifyOptions.Whitespace = StringifyOptions.Whitespace{
.indent_level = 0,
.indent = .{ .space = 1 },
},
stream: OutStream,
state_index: usize,
state: [max_depth]State,
pub fn init(stream: OutStream) Self {
var self = Self{
.stream = stream,
.state_index = 1,
.state = undefined,
};
self.state[0] = .complete;
self.state[1] = .value;
return self;
}
pub fn beginArray(self: *Self) !void {
assert(self.state[self.state_index] == State.value); // need to call arrayElem or objectField
try self.stream.writeByte('[');
self.state[self.state_index] = State.array_start;
self.whitespace.indent_level += 1;
}
pub fn beginObject(self: *Self) !void {
assert(self.state[self.state_index] == State.value); // need to call arrayElem or objectField
try self.stream.writeByte('{');
self.state[self.state_index] = State.object_start;
self.whitespace.indent_level += 1;
}
pub fn arrayElem(self: *Self) !void {
const state = self.state[self.state_index];
switch (state) {
.complete => unreachable,
.value => unreachable,
.object_start => unreachable,
.object => unreachable,
.array, .array_start => {
if (state == .array) {
try self.stream.writeByte(',');
}
self.state[self.state_index] = .array;
self.pushState(.value);
try self.indent();
},
}
}
pub fn objectField(self: *Self, name: []const u8) !void {
const state = self.state[self.state_index];
switch (state) {
.complete => unreachable,
.value => unreachable,
.array_start => unreachable,
.array => unreachable,
.object, .object_start => {
if (state == .object) {
try self.stream.writeByte(',');
}
self.state[self.state_index] = .object;
self.pushState(.value);
try self.indent();
try self.writeEscapedString(name);
try self.stream.writeByte(':');
if (self.whitespace.separator) {
try self.stream.writeByte(' ');
}
},
}
}
pub fn endArray(self: *Self) !void {
switch (self.state[self.state_index]) {
.complete => unreachable,
.value => unreachable,
.object_start => unreachable,
.object => unreachable,
.array_start => {
self.whitespace.indent_level -= 1;
try self.stream.writeByte(']');
self.popState();
},
.array => {
self.whitespace.indent_level -= 1;
try self.indent();
self.popState();
try self.stream.writeByte(']');
},
}
}
pub fn endObject(self: *Self) !void {
switch (self.state[self.state_index]) {
.complete => unreachable,
.value => unreachable,
.array_start => unreachable,
.array => unreachable,
.object_start => {
self.whitespace.indent_level -= 1;
try self.stream.writeByte('}');
self.popState();
},
.object => {
self.whitespace.indent_level -= 1;
try self.indent();
self.popState();
try self.stream.writeByte('}');
},
}
}
pub fn emitNull(self: *Self) !void {
assert(self.state[self.state_index] == State.value);
try self.stringify(null);
self.popState();
}
pub fn emitBool(self: *Self, value: bool) !void {
assert(self.state[self.state_index] == State.value);
try self.stringify(value);
self.popState();
}
pub fn emitNumber(
self: *Self,
/// An integer, float, or `std.math.BigInt`. Emitted as a bare number if it fits losslessly
/// in a IEEE 754 double float, otherwise emitted as a string to the full precision.
value: anytype,
) !void {
assert(self.state[self.state_index] == State.value);
switch (@typeInfo(@TypeOf(value))) {
.Int => |info| {
if (info.bits < 53) {
try self.stream.print("{}", .{value});
self.popState();
return;
}
if (value < 4503599627370496 and (info.signedness == .unsigned or value > -4503599627370496)) {
try self.stream.print("{}", .{value});
self.popState();
return;
}
},
.ComptimeInt => {
return self.emitNumber(@as(std.math.IntFittingRange(value, value), value));
},
.Float, .ComptimeFloat => if (@as(f64, @floatCast(value)) == value) {
try self.stream.print("{}", .{@as(f64, @floatCast(value))});
self.popState();
return;
},
else => {},
}
try self.stream.print("\"{}\"", .{value});
self.popState();
}
pub fn emitString(self: *Self, string: []const u8) !void {
assert(self.state[self.state_index] == State.value);
try self.writeEscapedString(string);
self.popState();
}
fn writeEscapedString(self: *Self, string: []const u8) !void {
assert(std.unicode.utf8ValidateSlice(string));
try self.stringify(string);
}
/// Writes the complete json into the output stream
pub fn emitJson(self: *Self, value: Value) Stream.Error!void {
assert(self.state[self.state_index] == State.value);
try self.stringify(value);
self.popState();
}
fn indent(self: *Self) !void {
assert(self.state_index >= 1);
try self.whitespace.outputIndent(self.stream);
}
fn pushState(self: *Self, state: State) void {
self.state_index += 1;
self.state[self.state_index] = state;
}
fn popState(self: *Self) void {
self.state_index -= 1;
}
fn stringify(self: *Self, value: anytype) !void {
try jsonStringify(value, StringifyOptions{
.whitespace = self.whitespace,
}, self.stream);
}
};
}
pub fn writeStream(
out_stream: anytype,
comptime max_depth: usize,
) WriteStream(@TypeOf(out_stream), max_depth) {
return WriteStream(@TypeOf(out_stream), max_depth).init(out_stream);
}
const ObjectMap = @import("./dynamic.zig").ObjectMap;
test "json write stream" {
var out_buf: [1024]u8 = undefined;
var slice_stream = std.io.fixedBufferStream(&out_buf);
const out = slice_stream.writer();
var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_allocator.deinit();
var w = writeStream(out, 10);
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 =
\\{
\\ "object": {
\\ "one": 1,
\\ "two": 2.0e+00
\\ },
\\ "string": "This is a string",
\\ "array": [
\\ "Another string",
\\ 1,
\\ 3.5e+00
\\ ],
\\ "int": 10,
\\ "float": 3.5e+00
\\}
;
try std.testing.expect(std.mem.eql(u8, expected, result));
}
fn getJsonObject(allocator: std.mem.Allocator) !Value {
var value = Value{ .object = ObjectMap.init(allocator) };
try value.object.put("one", Value{ .integer = @as(i64, @intCast(1)) });
try value.object.put("two", Value{ .float = 2.0 });
return value;
}