-
Notifications
You must be signed in to change notification settings - Fork 94
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
Accessing parsed numbers #17
Comments
Yes I agree that we need something, I am just not sure on what it should be or what we should call it. Although I think a separate
? |
close #17 number_cast<> provides a simple way to cast a value to a number.
Give this a whirl? https://github.com/vinniefalco/json/pull/19 |
close #17 number_cast<> provides a simple way to cast a value to a number.
close #17 number_cast<> provides a simple way to cast a value to a number.
LGTM functionality-wise, though I'd prefer it to be a member function and it should also mirror the accessor functions of other value kinds: const auto jv = json::parse("42");
jv.is_number<T>(); // check if the number fits into T
jv.as_number<T>(); // checked conversion to T
jv.get_number<T>(); // unchecked conversion to T
jv.if_number<T>(); // pointer access Then we can get rid of the |
Hmm...I see some problems with this interface. First of all in generic contexts you would have to write For example this cannot be implemented:
Because a
Finally, the implementation of This is why I went with |
Currently a JSON number may be parsed as either
kind::int64
,kind::uint64
orkind::double_
depending on the actual value, e.g.1
is parsed askind::int64
,18446744073709551615
is ofkind::uint64
, and1.0
is ofkind::double_
.This makes it convoluted to parse into a specific type if a sub-range may be parsed as another type, for example, to
uint64_t
I have to write:whereas I would expect to just write:
My intuition is that there should be a
json::number
type that can hold any parsed numbers and may be converted to the user-requested type upon access as long as there is no loss of precision:Thoughts?
The text was updated successfully, but these errors were encountered: