Function value [src]
Writes the given value to the Writer writer.
See Stringify for how the given value is serialized into JSON.
The maximum nesting depth of the output JSON document is 256.
Prototype
pub fn value(v: anytype, options: Options, writer: *Writer) Error!void Parameters
options: Optionswriter: *Writer Possible Errors
Example
test value {
var out: Writer.Allocating = .init(std.testing.allocator);
const writer = &out.writer;
defer out.deinit();
const T = struct { a: i32, b: []const u8 };
try value(T{ .a = 123, .b = "xy" }, .{}, writer);
try std.testing.expectEqualSlices(u8, "{\"a\":123,\"b\":\"xy\"}", out.written());
try testStringify("9999999999999999", 9999999999999999, .{});
try testStringify("\"9999999999999999\"", 9999999999999999, .{ .emit_nonportable_numbers_as_strings = true });
try testStringify("[1,1]", @as(@Vector(2, u32), @splat(1)), .{});
try testStringify("\"AA\"", @as(@Vector(2, u8), @splat('A')), .{});
try testStringify("[65,65]", @as(@Vector(2, u8), @splat('A')), .{ .emit_strings_as_arrays = true });
// void field
try testStringify("{\"foo\":42}", struct {
foo: u32,
bar: void = {},
}{ .foo = 42 }, .{});
const Tuple = struct { []const u8, usize };
try testStringify("[\"foo\",42]", Tuple{ "foo", 42 }, .{});
comptime {
testStringify("false", false, .{}) catch unreachable;
const MyStruct = struct { foo: u32 };
testStringify("[{\"foo\":42},{\"foo\":100},{\"foo\":1000}]", [_]MyStruct{
MyStruct{ .foo = 42 },
MyStruct{ .foo = 100 },
MyStruct{ .foo = 1000 },
}, .{}) catch unreachable;
}
} Source
pub fn value(v: anytype, options: Options, writer: *Writer) Error!void {
var s: Stringify = .{ .writer = writer, .options = options };
try s.write(v);
}