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

Invalid JSON serialization when NaN values are present #1816

Open
9cvele3 opened this issue Jan 5, 2024 · 2 comments
Open

Invalid JSON serialization when NaN values are present #1816

9cvele3 opened this issue Jan 5, 2024 · 2 comments

Comments

@9cvele3
Copy link

9cvele3 commented Jan 5, 2024

When using klv and serialize arrows to serialize klv metadata to json,
sometimes I get invalid JSON that can not be parsed properly
due to the following key value pair:
"value": NaN

NaN is not valid in JSON, it should be null instead:

"value": null

Short code snippet:

            std::stringstream ss;                                                                                                           
            kwiver::arrows::serialize::json::metadata_map_io_klv map_to_json;                                                               
            map_to_json.save(ss, vital_metadata_map);    
            auto str = ss.str();
@aramSofthenge
Copy link
Contributor

Hi @chetnieter

I am encountering an "undefined reference" error with an example project that includes <arrows/serialize/json/klv/metadata_map_io.h> header, however kwiver build succeeds with arrows component enabled.

Any ideas on what I'm doing wrong?

@Erotemic
Copy link
Member

2cents: even though NaN is not valid JSON according to RFC 8259, it is not equivalent to null, and blindly converting it can cause downstream errors.

To illustrate this point, consider this Python list:

[1, 2, float('nan'), None]

This list is distinct from and should not be considered the same as [1, 2, None, None].

In fact many json parsers support (including Python's internal one and ultrajson) these non-standard symbols such as NaN and Infinity, which parse to valid floats as represented by IEEE 754, but are simply unrepresentable in "authoritative JSON". Other libraries like ijson do take the opposite approach and explicitly disallow them, which IMO is unfortunate.

If your desire is to replace NaN with null, I think that should be an application specific decision and done via pre or post processing. Or just use one of the many json parsers that recognize the weakness of RFC 8259 and support NaN and Infinity.

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

No branches or pull requests

3 participants