Comments (12)
It's not about the JSON spec (which doesn't say anything about numeric sizes) but about compatibility with all major implementations, including those such as JavaScript which treat all numbers as doubles.
But JavaScript will implicity convert to double! Ignoring the spec and lowering precision deliberately because one implementation has a funky numerical typing system is really a frustrating resolution to this ticket. Further, in the future JS is expected to have 64 bit ints & uints (https://github.com/tc39/ecmascript_simd &c)! Exercising this nuclear option now will could significantly harm the usability of this library in that future.
uint64_t support would be really really really nice, and it should totally happen. JavaScript will work just fine if json11 does this, it'll just lose precision, like JavaScript does when you are > MAX_SAFE_INTEGER. Which is fine and well known and JS developers (raise hand) know they have to cope with this. Please don't limit this lib artificially.
This would be very useful for implementing PowerDNS/pdns#4915 for example.
from json11.
I understand json11
is limited to 53-bit integers because it might round-trip them through double
- but that is a limitation of the implementation, not of the JSON specification. (If I am mistaken here and missed something in the JSON spec, please let me know. Otherwise...)
I think a proper implementation would use intmax_t
to store integer values and separate integer storage from double
. Support for implicit casting between intmax_t
and double
should still be available, with the implied understanding that there would be cases of loss in precision (which is already the case, only going from double
-> int
.)
from json11.
I believe that the reasoning here is that JSON can't actually support anything larger that 53 bit integers. If your use case is timestamps, double has plenty of precision as clarified in this comment
Lines 24 to 28 in 7103522
Storing as a 32 bit integer (as either signed or unsigned) will be fine, since doubles have more than enough precision to represent this.
uint32_t value = static_cast<uint32_t>( json_thing.number_value() )
will always be safe, but casting to a 64 bit integer type will cause issues with values larger than 53 bits.
So, in my opinion, 64 bit types are a non-goal of this library, and 32 bits types are simply syntactic sugar (which would be argued for either way).
from json11.
Alright, that all makes sense, thanks for clarifying. The syntactic sugar for unsigned 32 bit could still be nice though?
from json11.
I would probably agree, but I'm not the library maintainer :)
from json11.
@skabbes and @lehni made a pull request #61
(also signed the SLA).
from json11.
Yes, I store uint64 session_id, but can not store with the precise. I am wondering whether there are any other methods to improve this.
string str1 = R"({"message_id":105308320612483198,"msg_type":3,"order":0,"ques_id":0,"session_id":105308187502051928,"site_id":122062,"text":"XXXXXXX","timestamp":1471140271,"visitor_id":9941658010949867158,"worker_id":133746})";
string err;
auto json = json11::Json::parse(str1, err);
cout << (unsigned long long) (json["message_id"].number_value()) << endl;
the value is 105308320612483200
maybe I can ignore some bits....
from json11.
If you need fidelity for a full 64 bits, your best bet is to encode your IDs as strings rather than numbers. Or modify your own fork with something like #61 if you can accept the cross-language compatibility concerns discussed there.
from json11.
@artwyman thanks.
I checked, the #61 implement may have some problem, It can not reach int64_max, int64_min, uint64_max limits. I forked and enhanced at https://github.com/taozhijiang/json11
With out the consider like javascript, I think int64_t and uint_64 are very important at most cases. I suggest create another branch may a better solution. ;-)
from json11.
Maintaining a separate branch would add some maintenance overhead, though admittedly less confusingly than multiple forks. It might be better to provide both options controlled by ifdefs, or as distinct types in the same library, though I'd be interested to hear @j4cbo's opinion on the matter.
from json11.
@fosterbrereton That's an intentional limitation for compatibility, as described here:
Lines 16 to 22 in 7103522
It's not about the JSON spec (which doesn't say anything about numeric sizes) but about compatibility with all major implementations, including those such as JavaScript which treat all numbers as doubles. We designed json11 for network traffic, and want it to produce and consume JSON which can be exchanged with any other implementation.
from json11.
If everything has to be limited to what can round-trip through a double
, then, why add all the extra code to support int
? The library would be cleaner and more correct with respect to the intent of supporting double
for JavaScript's sake.
Separating storage for int
and double
implies they should be treated differently to take advantage of each's benefits (which would be great). It's no surprise, then, that library users might misinterpret the library's intent and file bug reports and/or feature requests in kind.
from json11.
Related Issues (20)
- is it possible to add fields to the json? HOT 1
- in json11.cpp line 161 , why use static_cast, what if a json object is a runtime parsed object HOT 1
- clang 6.0 compilation error HOT 2
- Check if a json object key exists HOT 2
- differentiate between "number" and "integer" as per json schema HOT 1
- Tag Versioning HOT 6
- Number size issue.
- Can't parse wstring?
- auto casting HOT 4
- has_shape() always returns true when used with json11::Json::NUL HOT 1
- [Feature] conan package manager support HOT 1
- Order serealization Json::object HOT 2
- ASAN reports runtime error HOT 1
- swig4.0.0 and mingw compile the test.cpp(main modified to be a function) :error: 'final' is not a member of 'json11' HOT 3
- can't be loaded as a dll in low version c++? HOT 2
- Warning '-Werror=overloaded-virtual' with gcc-5.1.0 --std=c++14 HOT 1
- Making object_items(), array_items() not const or alternatively support mutable_object_items() HOT 1
- Super stuff. HOT 1
- No API to add Json (f.e. array) to Json::object outside of ctor. HOT 1
- Buliding for windows? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json11.