diff options
| -rw-r--r-- | doc/langref.html.in | 9 | ||||
| -rw-r--r-- | lib/std/json.zig | 23 |
2 files changed, 22 insertions, 10 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in index 618ea08cdb..3eb30c3058 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2778,6 +2778,7 @@ test "simple union" { This turns the union into a <em>tagged</em> union, which makes it eligible to use with {#link|switch#} expressions. One can use {#link|@TagType#} to obtain the enum type from the union type. + Tagged unions implicitly cast to their enum {#link|Implicit Cast: unions and enums#} </p> {#code_begin|test#} const std = @import("std"); @@ -2805,6 +2806,14 @@ test "switch on tagged union" { test "@TagType" { assert(@TagType(ComplexType) == ComplexTypeTag); } + +test "implicit cast to enum" { + const c1 = ComplexType{ .Ok = 42 }; + const c2 = ComplexType.NotOk; + + assert(c1 == .Ok); + assert(c2 == .NotOk); +} {#code_end#} <p>In order to modify the payload of a tagged union in a switch expression, place a {#syntax#}*{#endsyntax#} before the variable name to make it a pointer: diff --git a/lib/std/json.zig b/lib/std/json.zig index 025cf28a70..f385205a24 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -867,6 +867,8 @@ pub const TokenStream = struct { parser: StreamingParser, token: ?Token, + pub const Error = StreamingParser.Error || error{UnexpectedEndOfJson}; + pub fn init(slice: []const u8) TokenStream { return TokenStream{ .i = 0, @@ -876,7 +878,7 @@ pub const TokenStream = struct { }; } - pub fn next(self: *TokenStream) !?Token { + pub fn next(self: *TokenStream) Error!?Token { if (self.token) |token| { const copy = token; self.token = null; @@ -896,16 +898,11 @@ pub const TokenStream = struct { } } - if (self.i > self.slice.len) { - try self.parser.feed(' ', &t1, &t2); - self.i += 1; - - if (t1) |token| { - return token; - } + if(self.parser.complete){ + return null; + } else { + return error.UnexpectedEndOfJson; } - - return null; } }; @@ -1456,3 +1453,9 @@ test "write json then parse it" { testing.expect(tree.root.Object.get("array").?.value.Array.at(1).Float == 12.34); testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello")); } + +test "parsing empty string gives appropriate error" { + var p = Parser.init(debug.global_allocator, false); + defer p.deinit(); + testing.expectError(error.UnexpectedEndOfJson, p.parse("")); +} |
