Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json: stricter number parsing #21962

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

D-Berg
Copy link
Contributor

@D-Berg D-Berg commented Nov 11, 2024

This PR makes zigs json.parseFromSlice return error UnexpectedToken when trying to parse a 'numbered string' when expecting an integer. For example the below test succeeds and parses "2134071" to an i32, while I believe that it should fail since the type of "number" is a json string.

test "parse json" {
    const Request = struct {
        method: []const u8,
        number: i32,
    };

    const allocator = std.testing.allocator;
    const req = 
        \\{"method":"isPrime","number":"2134071"}
    ;

    const parsed = try std.json.parseFromSlice(Request, allocator, req, .{});
    defer parsed.deinit();
}

Both Rust's Serde and Go's json are stricter and fail when trying to parse json 'numbered strings' to numbers and this would make zigs json lib conform to the expected behaviour of those other languages libs and make it up to the user to then parse the string to a number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant