Skip to content

Comments change integer parsing behavior #17

@prk3

Description

@prk3

Detailed description

I noticed that whether a big integer is parsed as int or string depends on the presence of JSON5-specific elements in the input string. Example:

var_dump(json_decode('{
    "hello": 8819896480406998907
}', true, 512, JSON_BIGINT_AS_STRING));

var_dump(json5_decode('{
    "hello": 8819896480406998907
}', true, 512, JSON_BIGINT_AS_STRING));

var_dump(json5_decode('{
    // comment
    "hello": 8819896480406998907
}', true, 512, JSON_BIGINT_AS_STRING));
array(1) {
  ["hello"]=>
  int(8819896480406998907)
}
array(1) {
  ["hello"]=>
  int(8819896480406998907)
}
array(1) {
  ["hello"]=>
  string(19) "8819896480406998907"
}

The only difference between the last two snippets is the presence of comments, and yet one parsed the number as int, while the other parsed the number a string. Maybe JSON_BIGINT_AS_STRING isn't interpreted correctly?

In this case json_decode should behave the same as the native json_decode: it should return an int.

Environment

  • Library version used (e.g. 1.0): 2.2.1
  • PHP version used (e.g. PHP 5.6, PHP 7.0): 7.3.25
  • Operating system and version (e.g. Ubuntu 16.04, Windows 7): Linux Fedora 35

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions