Skip to content

Deserializing NaN #447

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

Closed
rdilipk opened this issue Mar 18, 2016 · 7 comments
Closed

Deserializing NaN #447

rdilipk opened this issue Mar 18, 2016 · 7 comments
Labels

Comments

@rdilipk
Copy link

rdilipk commented Mar 18, 2016

I am dealing with some Json that is sending down NaN quoted like this: "NaN". The Json::Value::asDouble() method fails because the type of that entity is being deserialized as string and calling asDouble() on that expectedly throws. I am using 0.7.0. Two questions:

  1. How should I handle these correctly? Should I ask upstream code to NOT quote the word NaN given that Json can't describe such entities anyway? They do send out null (no quotes) and asDouble successfully deserializes them to 0.

  2. I see something called allowSpecialFloats which looks like what I want but it is buried under some class called OurFeatures (mirroring Features). That in turn is hidden inside json_reader.cpp. Don't I have access to that setting?

( I see this has been brought up before: #209
How do I make use of that fix?)

@BillyGoto
Copy link

On Fri, Mar 18, 2016 at 11:20 AM, rdilipk [email protected] wrote:

I am dealing with some Json that is sending down NaN quoted like this:
"NaN". The Json::Value::asDouble() method fails because the type of that
entity is being deserialized as string and calling asDouble() on that
expectedly throws.

If you quote it, it's a string, not a double. Nothing can be done about
that.
Sometimes people want to send the word "NaN" as a string.
We can't have special string values. There's no NaN number in JSON
http://www.json.org/, and them's the breaks.

Two questions:

  1. How should I handle these correctly? Should I ask upstream code to NOT
    quote the word NaN given that Json can't describe such entities anyway?
    They do send out null (no quotes) and asDouble successfully deserializes
    them to 0.

This null -> 0.0 conversion is an amenity I wouldn't feel safe relying on.
You should always check isDouble() before calling asDouble().

  1. I see something called allowSpecialFloats which looks like what I want
    but it is buried under some class called OurFeatures (mirroring Features).
    That in turn is hidden inside json_reader.cpp. Don't I have access to that
    setting?

Yes. It's described in the json/reader.h header, as a public Json::Value
member (unfortunately, IMO).

ǝnɥɐuop ʎllıq

@rdilipk
Copy link
Author

rdilipk commented Mar 18, 2016

On Fri, Mar 18, 2016 at 11:29 AM, BillyGoto [email protected]
wrote:

On Fri, Mar 18, 2016 at 11:20 AM, rdilipk [email protected]
wrote:

Two questions:

  1. How should I handle these correctly? Should I ask upstream code to NOT
    quote the word NaN given that Json can't describe such entities anyway?
    They do send out null (no quotes) and asDouble successfully deserializes
    them to 0.

This null -> 0.0 conversion is an amenity I wouldn't feel safe relying on.
You should always check isDouble() before calling asDouble().

The thing is, the Json I am dealing with itself has some meta-data that advertises the
data types of values I am dealing with. It says I have to expect an array of DOUBLE
values (which is why I don't have to do isDouble()). Most values are indeed
of the right type but some are null and others are "NaN" (quoted like a string). I
guess your point is ask them to send NaN (UNQUOTED), just like null and Jsoncpp will do
something meaningful with it?

  1. I see something called allowSpecialFloats which looks like what I want
    but it is buried under some class called OurFeatures (mirroring
    Features).
    That in turn is hidden inside json_reader.cpp. Don't I have access to
    that
    setting?

Yes. It's described in the json/reader.h header, as a public Json::Value
member (unfortunately, IMO).

Sorry. I am a bit confused. I currently use Json::Reader to 'parse' in the entire Json
document as plain text. I then process it in a way I want. As far as I can see there
is no way to set allowSpecialFloats here. I must now use CharReaderBuilder to create a
CharBuilder just to operate on a fragment of Json?

@BillyDonahue
Copy link
Contributor

Yeah, Json::Reader is deprecated.
https://github.com/open-source-parsers/jsoncpp/blob/master/include/json/reader.h

\deprecated Use CharReader and CharReaderBuilder.

So:

Json::CharReaderBuilder builder;
builder["collectComments"] = false;
std::unique_ptr<Json::CharReader> reader(builder.newCharReader());

... now use this reader.

@rdilipk
Copy link
Author

rdilipk commented Mar 18, 2016

That works! Thank you very much! I somehow totally missed the deprecation notice.

@rdilipk rdilipk closed this as completed Mar 18, 2016
@cdunn2001
Copy link
Contributor

  1. I see something called allowSpecialFloats which looks like what I want
    but it is buried under some class called OurFeatures (mirroring Features).
    That in turn is hidden inside json_reader.cpp. Don't I have access to that setting?

Yes. It's described in the json/reader.h header, as a public Json::Value member (unfortunately, IMO).

allowSpecialFloats is kinda weird, but configurable parsing and dumping is one of the major advantages of JsonCpp over other libraries.

@BillyDonahue
Copy link
Contributor

On Saturday, March 19, 2016, Christopher Dunn [email protected]
wrote:

  1. I see something called allowSpecialFloats which looks like what I want
    but it is buried under some class called OurFeatures (mirroring Features).
    That in turn is hidden inside json_reader.cpp. Don't I have access to that
    setting?

Yes. It's described in the json/reader.h header, as a public Json::Value
member (unfortunately, IMO).

allowSpecialFloats is kinda weird, but configurable parsing and dumping
is one of the major advantages of JsonCpp over other libraries.

I like the configurability. I just mean the configurability mechanism is a
little loose. It's a little too freeform for me. An API to set options by
const identifiers would make it more clear what the supported operations
are, and keep the underscore-named data member out of user code. Exposing
a data member makes it hard to change the representation.

You are receiving this because you commented.
Reply to this email directly or view it on GitHub
#447 (comment)

@cdunn2001
Copy link
Contributor

True, but I don't have time to change that (if you mean to add methods to the Builder for everything). If you submit a PR, you can merge it. My main concern is easy and obvious binary compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants