diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-03-19 18:06:16 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-19 18:06:16 -0400 |
| commit | dc04e97098010f590d109e6e70d4afe79cd8f01b (patch) | |
| tree | bed11818fd80fe7b4557f4253c8d5562de773624 /lib/std/json.zig | |
| parent | 555a2c03286507ffe4bd3bea2154dbfb719ebef1 (diff) | |
| parent | 160367e0ddcb36b6957e603d869507b9d7542edc (diff) | |
| download | zig-dc04e97098010f590d109e6e70d4afe79cd8f01b.tar.gz zig-dc04e97098010f590d109e6e70d4afe79cd8f01b.zip | |
Merge pull request #4752 from ziglang/slice-array
slicing with comptime start and end indexes results in pointer-to-array
Diffstat (limited to 'lib/std/json.zig')
| -rw-r--r-- | lib/std/json.zig | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/lib/std/json.zig b/lib/std/json.zig index f5a72d86da..3dd8088785 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -2249,11 +2249,16 @@ pub const StringifyOptions = struct { // TODO: allow picking if []u8 is string or array? }; +pub const StringifyError = error{ + TooMuchData, + DifferentData, +}; + pub fn stringify( value: var, options: StringifyOptions, out_stream: var, -) !void { +) StringifyError!void { const T = @TypeOf(value); switch (@typeInfo(T)) { .Float, .ComptimeFloat => { @@ -2320,9 +2325,15 @@ pub fn stringify( return; }, .Pointer => |ptr_info| switch (ptr_info.size) { - .One => { - // TODO: avoid loops? - return try stringify(value.*, options, out_stream); + .One => switch (@typeInfo(ptr_info.child)) { + .Array => { + const Slice = []const std.meta.Elem(ptr_info.child); + return stringify(@as(Slice, value), options, out_stream); + }, + else => { + // TODO: avoid loops? + return stringify(value.*, options, out_stream); + }, }, // TODO: .Many when there is a sentinel (waiting for https://github.com/ziglang/zig/pull/3972) .Slice => { @@ -2381,9 +2392,7 @@ pub fn stringify( }, else => @compileError("Unable to stringify type '" ++ @typeName(T) ++ "'"), }, - .Array => |info| { - return try stringify(value[0..], options, out_stream); - }, + .Array => return stringify(&value, options, out_stream), else => @compileError("Unable to stringify type '" ++ @typeName(T) ++ "'"), } unreachable; |
