diff --git a/lib/std/json.zig b/lib/std/json.zig index 6946ba0170d9..a02c4ca733de 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -387,14 +387,17 @@ pub const StreamingParser = struct { token.* = Token.initMarker(Token.Id.ArrayBegin); }, '-' => { + p.number_is_integer = true; p.state = State.Number; p.count = 0; }, '0' => { + p.number_is_integer = true; p.state = State.NumberMaybeDotOrExponent; p.count = 0; }, '1'...'9' => { + p.number_is_integer = true; p.state = State.NumberMaybeDigitOrDotOrExponent; p.count = 0; }, @@ -453,14 +456,17 @@ pub const StreamingParser = struct { token.* = Token.initMarker(Token.Id.ArrayBegin); }, '-' => { + p.number_is_integer = true; p.state = State.Number; p.count = 0; }, '0' => { + p.number_is_integer = true; p.state = State.NumberMaybeDotOrExponent; p.count = 0; }, '1'...'9' => { + p.number_is_integer = true; p.state = State.NumberMaybeDigitOrDotOrExponent; p.count = 0; }, @@ -1374,8 +1380,21 @@ test "write json then parse it" { testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello")); } -test "parsing empty string gives appropriate error" { +fn test_parse(json_str: []const u8) !Value { var p = Parser.init(debug.global_allocator, false); - defer p.deinit(); - testing.expectError(error.UnexpectedEndOfJson, p.parse("")); + return (try p.parse(json_str)).root; +} + +test "parsing empty string gives appropriate error" { + testing.expectError(error.UnexpectedEndOfJson, test_parse("")); +} + +test "integer after float has proper type" { + const json = try test_parse( + \\{ + \\ "float": 3.14, + \\ "ints": [1, 2, 3] + \\} + ); + std.testing.expect(json.Object.getValue("ints").?.Array.at(0) == .Integer); }