Skip to content

json: Fix handling of NaN/Infinity #15192

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

Merged
merged 1 commit into from
Jun 26, 2014
Merged

json: Fix handling of NaN/Infinity #15192

merged 1 commit into from
Jun 26, 2014

Conversation

mrec
Copy link
Contributor

@mrec mrec commented Jun 26, 2014

The JSON spec requires that these special values be serialized as "null"; the current serialization breaks any conformant JSON parser. So encoding needs to output "null", to_json on floating-point types can return Null as well as Number values, and reading a Null value when specifically expecting a number should be interpreted as NaN. There's no way to round-trip Infinity through JSON.

This is my first attempt at both writing Rust and opening pull requests, so please dial your derp detector up to eleven when reviewing. A rustc --test lib.rs in libserialize passes all tests; a make check of the whole tree fails with the error below, but it doesn't look obviously related and the docs say that make check is known to be flaky on Windows.

---- [compile-fail] compile-fail/svh-change-significant-cfg.rs stdout ----
        task '[compile-fail] compile-fail/svh-change-significant-cfg.rs' failed at 'called `Result::
unwrap()` on an `Err` value: couldn't create file (end of file (unknown error); path=i686-pc-mingw32
\test\compile-fail\svh-a-base.err; mode=truncate; access=write)', C:\msys\home\Mike\rust\src\libcore
\result.rs:545

Incidentally, it may just be my lack of familiarity with the language and its idioms, but the duplication between Encoder/PrettyEncoder had a distinct code smell to it. The size of the file (~3500 lines) also made it a bit hard to navigate. Has there been any discussion of refactoring and/or breaking it up? I couldn't find anything in Issues except the ancient #9028.

The JSON spec requires that these special values be serialized as null; the current serialization breaks any conformant JSON parser. So encoding needs to output "null",  to_json on floating-point types can return Null as well as Number, and reading null when specifically expecting a number should be interpreted as NaN. There's no way to round-trip Infinity.
@alexcrichton
Copy link
Member

I believe that @erickt has been working on some refactorings of serialization, so he may be best to ask about the duplication.

bors added a commit that referenced this pull request Jun 26, 2014
The JSON spec requires that these special values be serialized as "null"; the current serialization breaks any conformant JSON parser. So encoding needs to output "null",  `to_json` on floating-point types can return `Null` as well as `Number` values, and reading a `Null` value when specifically expecting a number should be interpreted as NaN. There's no way to round-trip Infinity through JSON.

This is my first attempt at both writing Rust and opening pull requests, so please dial your derp detector up to eleven when reviewing. A `rustc --test lib.rs` in `libserialize` passes all tests; a `make check` of the whole tree fails with the error below, but it doesn't look obviously related and the docs say that `make check` is known to be flaky on Windows.

    ---- [compile-fail] compile-fail/svh-change-significant-cfg.rs stdout ----
            task '[compile-fail] compile-fail/svh-change-significant-cfg.rs' failed at 'called `Result::
    unwrap()` on an `Err` value: couldn't create file (end of file (unknown error); path=i686-pc-mingw32
    \test\compile-fail\svh-a-base.err; mode=truncate; access=write)', C:\msys\home\Mike\rust\src\libcore
    \result.rs:545

Incidentally, it may just be my lack of familiarity with the language and its idioms, but the duplication between `Encoder`/`PrettyEncoder` had a distinct code smell to it. The size of the file (~3500 lines) also made it a bit hard to navigate. Has there been any discussion of refactoring and/or breaking it up? I couldn't find anything in Issues except the ancient #9028.
@bors bors closed this Jun 26, 2014
@bors bors merged commit e1a9899 into rust-lang:master Jun 26, 2014
@mrec mrec deleted the json-nan branch June 30, 2014 17:40
@mrec mrec restored the json-nan branch August 18, 2019 23:11
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.

3 participants