mirror of
https://github.com/ziglang/zig.git
synced 2024-12-02 18:12:34 +00:00
std: add json.stringifyAlloc
This commit is contained in:
parent
f72a0a2907
commit
0d0f277954
@ -3333,3 +3333,23 @@ test "stringify null optional fields" {
|
|||||||
.{ .allocator = std.testing.allocator },
|
.{ .allocator = std.testing.allocator },
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Same as `stringify` but accepts an Allocator and stores result in dynamically allocated memory instead of using a Writer.
|
||||||
|
// Caller owns returned memory.
|
||||||
|
pub fn stringifyAlloc(allocator: std.mem.Allocator, value: anytype, options: StringifyOptions) ![]const u8 {
|
||||||
|
var list = std.ArrayList(u8).init(allocator);
|
||||||
|
errdefer list.deinit();
|
||||||
|
try stringify(value, options, list.writer());
|
||||||
|
return list.toOwnedSlice();
|
||||||
|
}
|
||||||
|
|
||||||
|
test "stringify alloc" {
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
const expected =
|
||||||
|
\\{"foo":"bar","answer":42,"my_friend":"sammy"}
|
||||||
|
;
|
||||||
|
const actual = try stringifyAlloc(allocator, .{ .foo = "bar", .answer = 42, .my_friend = "sammy" }, .{});
|
||||||
|
defer allocator.free(actual);
|
||||||
|
|
||||||
|
try std.testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user