Function valueAlloc [src]

Calls value and stores the result in dynamically allocated memory instead of taking a writer. Caller owns returned memory.

Prototype

pub fn valueAlloc(gpa: Allocator, v: anytype, options: Options) error{OutOfMemory}![]u8

Parameters

gpa: Allocatoroptions: Options

Possible Errors

OutOfMemory

Example

test valueAlloc { const allocator = std.testing.allocator; const expected = \\{"foo":"bar","answer":42,"my_friend":"sammy"} ; const actual = try valueAlloc(allocator, .{ .foo = "bar", .answer = 42, .my_friend = "sammy" }, .{}); defer allocator.free(actual); try std.testing.expectEqualStrings(expected, actual); }

Source

pub fn valueAlloc(gpa: Allocator, v: anytype, options: Options) error{OutOfMemory}![]u8 { var aw: Writer.Allocating = .init(gpa); defer aw.deinit(); value(v, options, &aw.writer) catch return error.OutOfMemory; return aw.toOwnedSlice(); }