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 stringify returning invalid token inf #23253

Open
richard-powers opened this issue Mar 14, 2025 · 5 comments · May be fixed by #23258
Open

JSON stringify returning invalid token inf #23253

richard-powers opened this issue Mar 14, 2025 · 5 comments · May be fixed by #23258
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library.

Comments

@richard-powers
Copy link

Zig Version

0.14.0

Steps to Reproduce and Observed Behavior

Min. repro:

const std = @import("std");

test {
    const foo = .{
        .a = std.math.inf(f32),
    };
    const str = try std.json.stringifyAlloc(std.testing.allocator, foo, .{});
    defer std.testing.allocator.free(str);
    std.log.warn("{s}", .{str});
}

The output: {"a":inf}

inf is not a valid token in JSON. In my opinion, a parsing error should be thrown in cases with infinite values.

Expected Behavior

An error should be returned, instead of malformed json.

@richard-powers richard-powers added the bug Observed behavior contradicts documented or intended behavior label Mar 14, 2025
@linusg linusg added contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library. labels Mar 15, 2025
@bradcypert
Copy link

I’d be interested in attempting to fix this!

@bradcypert
Copy link

Put together a small PR to test out the issue and provide a possible solution! I hope it helps! :)

@martindrlik
Copy link

You can do the same as JSON.stringify.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

The numbers Infinity and NaN, as well as the value null, are all considered null. (But unlike the values in the previous point, they would never be omitted.)

Output would be {"a":null}.

@nektro
Copy link
Contributor

nektro commented Mar 18, 2025

JS succeed and marshals as null
Go fails and returns an error

@bradcypert
Copy link

From RFC-7159: https://datatracker.ietf.org/doc/html/rfc7159#section-6

Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants