Skip to content

Commit

Permalink
fix integers parsed as floats
Browse files Browse the repository at this point in the history
  • Loading branch information
xackus authored and andrewrk committed Dec 10, 2019
1 parent 3388c77 commit c27d065
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/std/json.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down Expand Up @@ -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;
},
Expand Down Expand Up @@ -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);
}

0 comments on commit c27d065

Please sign in to comment.