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

Parse nan in JSON as NaN instead of triggering a parse error #2712

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/jv_aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ jv jv_has(jv t, jv k) {
} else if (jv_get_kind(t) == JV_KIND_ARRAY &&
jv_get_kind(k) == JV_KIND_NUMBER) {
if (jvp_number_is_nan(k)) {
jv_free(t);
ret = jv_false();
} else {
jv elem = jv_array_get(t, (int)jv_number_value(k));
Expand Down
6 changes: 5 additions & 1 deletion src/jv_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,11 @@ static pfunc check_literal(struct jv_parser* p) {
switch (p->tokenbuf[0]) {
case 't': pattern = "true"; plen = 4; v = jv_true(); break;
case 'f': pattern = "false"; plen = 5; v = jv_false(); break;
case 'n': pattern = "null"; plen = 4; v = jv_null(); break;
case 'n':
// if it starts with 'n', it could be a literal "nan"
if (p->tokenpos != 3) {
pattern = "null"; plen = 4; v = jv_null();
}
}
if (pattern) {
if (p->tokenpos != plen) return "Invalid literal";
Expand Down
12 changes: 12 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,18 @@ reduce .[] as $then (4 as $else | $else; . as $elif | . + $then * $elif)


# { $__loc__ } works

{ a, $__loc__, c }
{"a":[1,2,3],"b":"foo","c":{"hi":"hey"}}
{"a":[1,2,3],"__loc__":{"file":"<top-level>","line":1},"c":{"hi":"hey"}}


# nan is parsed as a valid NaN value from JSON

fromjson | isnan
"nan"
true

tojson | fromjson
{"a":nan}
{"a":null}