Changes
1 changed files (+45/-5)
-
-
@@ -1279,6 +1279,9 @@/// Body data. Do not access after `deinit()`. value: *const T, /// Used for encoding. stringifyOpts: std.json.StringifyOptions = .{}, pub const ParseError = error{ MissingContentType, MissingContentLength,
-
@@ -1339,8 +1342,8 @@ }} /// Creates and returns JSON encodable body. pub fn init(value: *const T) @This() { return .{ .value = value }; pub fn init(value: *const T, stringifyOpts: std.json.StringifyOptions) @This() { return .{ .value = value, .stringifyOpts = stringifyOpts }; } /// Returns how many bytes required for encoding.
-
@@ -1380,7 +1383,7 @@ /// Use dynamic buffer (e.g. `std.ArrayList(u8)`) if the serialization cost/// matters. pub fn getEncodeSize(self: *const @This()) usize { var cw = std.io.countingWriter(std.io.null_writer); std.json.stringify(self.value, .{}, cw.writer()) catch return 0; std.json.stringify(self.value, self.stringifyOpts, cw.writer()) catch return 0; return cw.bytes_written; }
-
@@ -1388,7 +1391,7 @@ /// Serializes `value` to JSON text and writes it to `writer`./// /// `writer` must be `std.io.GenericWriter`. pub fn encode(self: *const @This(), writer: anytype) @TypeOf(writer).Error!void { try std.json.stringify(self.value, .{}, writer); try std.json.stringify(self.value, self.stringifyOpts, writer); } /// Get minimal header for the message.
-
@@ -1576,7 +1579,44 @@ .verb = "GET",.service = "user/account", }; const body = JsonBody(UserInfo).init(&UserInfo{ .id = "alice" }); const body = JsonBody(UserInfo).init(&UserInfo{ .id = "alice" }, .{}); const header = body.getHeader(1); const message_size = meta.getEncodeSize() + header.getEncodeSize() + body.getEncodeSize(); const buf = try std.testing.allocator.alloc(u8, message_size); defer std.testing.allocator.free(buf); var fbs = std.io.fixedBufferStream(buf); const writer = fbs.writer(); try encode(writer, meta, header, body); try std.testing.expectEqualStrings( \\MOO/1 GET user/account \\Content-Type: application/json \\Content-Length: 14 \\Request-Id: 1 \\ \\{"id":"alice"} , buf); } test "JSON stringify options" { const UserInfo = struct { id: []const u8, email: ?[]const u8 = null, }; const meta = Metadata{ .version = 1, .verb = "GET", .service = "user/account", }; const body = JsonBody(UserInfo).init(&UserInfo{ .id = "alice" }, .{ .emit_null_optional_fields = false, }); const header = body.getHeader(1);
-