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

SwiftyJSON generates invalid JSON for inf/nan numbers #1096

Open
kengruven opened this issue Feb 27, 2021 · 1 comment
Open

SwiftyJSON generates invalid JSON for inf/nan numbers #1096

kengruven opened this issue Feb 27, 2021 · 1 comment

Comments

@kengruven
Copy link

What did you do?

JSON(Double.nan).rawString()

What did you expect to happen?

There's no way to serialize NaN (or Inf) in JSON, and rawString() returns an optional, so I expected to get nil.

What actually happened instead?

I got the Swift string "nan" (which is not valid JSON).

Environment

List the software versions you're using:

  • SwiftyJSON: 5.0.0
  • Xcode Version: 12.0.1 (12A7300)
  • Swift Version: 5.3

Please also mention which package manager you used and its version.
other package managers in this list:

  • Swift Package Manager Swift Package Manager - Swift 5.3.0

Project that demonstrates the issue

(It's literally one line of code, and it's shown above in full. If you really need a complete project, let me know and I'll see what I can do.)

@kengruven
Copy link
Author

It looks like SwiftyJSON is simply returning the stringValue from NSNumber here, and that property obviously doesn't know anything about JSON syntax.

For comparison, when Foundation.JSONSerialization sees a Double.nan, it throws an NSInvalidArgumentException, "Invalid number value (NaN) in JSON write".

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

1 participant