Comments (7)
A corner case where the Boost.Json comparison behavior could be problematic is when a Javascript peer accidentally serializes a semantically floating point value into an integer without a decimal point, because the number just happens to be one that's exactly representable as an integer.
{"voltage": 3.10104}
-> document.at("voltage") == 3.0
-> false
// OK
{"voltage": 3.0}
-> document.at("voltage") == 3.0
-> true
// OK
{"voltage": 3}
-> document.at("voltage") == 3.0
-> false
// Oops
Where "voltage" is a fluctuating value that could coincidentally be exactly 3 (e.g. a 12-bit analog-to-digital converter).
from json.
I just realized in my example above that nobody should do such a floating-point equality comparsion without a +/-epsilon.
from json.
I guess a good rationale for treating signed/unsigned integers as the same kind in comparisons is when using literals on the right hand side:
{"id": 42}
-> 42
parsed as unsigned integer
document.at("id") == 42
// Literal 42
is signed!
from json.
The rationale is that the stored values are different and thus certain member functions would have different behaviours. It would be strange to have equal values behave differently.
If the user wants a more lax comparison, it can be acheived: https://godbolt.org/z/4vz9h63MG. If you can use C++ concepts, than here is a slightly cleaner version: https://godbolt.org/z/TahMGMG8o.
BTW, 42
would parse as int64_t
. And "42"
would parse as json::string
.
from json.
The rationale is that the stored values are different and thus certain member functions would have different behaviours.
But int64_t
and uint64_t
are stored differently, yet they are treated as the same kind in comparisons. This does not adequately explain why double
is treated as a different kind in comparisons.
It would be strange to have equal values behave differently.
It is currently "strange" that assert(json::value(42) == json::value(42.0)
fails.
BTW, 42 would parse as int64_t. And "42" would parse as json::string.
I incorrectly assumed 42 would parse as uint64_t
because that's what the current JSON library I'm using does (only negative integers get parsed as int64_t
). The quoted "42" in my example was a brain fart. :-)
from json.
But
int64_t
anduint64_t
are stored differently, yet they are treated as the same kind in comparisons. This does not adequately explain whydouble
is treated as a different kind in comparisons.
You got me there. I guess, it's for the convenience of value(1u) == 1
.
from json.
BTW, 42 would parse as int64_t. And "42" would parse as json::string.
I incorrectly assumed 42 would parse as uint64_t because that's what the current JSON library I'm using does (only negative integers get parsed as int64_t). The quoted "42" in my example was a brain fart. :-)
Ah, I think I've stumbled upon the rationale right there. Treating signed/unsigned integers as the same kind in comparisons eliminates the guesswork and messiness in dealing with how the library parses positive/negative integers into uint64_t
or int64_t
.
from json.
Related Issues (20)
- Can the value_from function add iterator parameters, such as value_from (Iterator begin, Iterator end) HOT 5
- Regression: serializing vector<> of objects HOT 4
- Linux gcc compilation failures with --pedantic-errors flag: error: extra ‘;’ HOT 1
- Value Iteration HOT 5
- Question: how to determine the required size for temp buffers? HOT 7
- If an unsigned integer is serialized, deserializing back to the unsigned integer fails HOT 8
- Comment Only: Logo HOT 3
- Stack exaustion while value_from(std::filesystem::path) HOT 5
- Small help for non eagle eyed people HOT 2
- Documentation inconsistency about exceptions HOT 2
- Add a convenience member function to call value_to HOT 5
- boost::json::value::is_uint64() behaves unintuitively HOT 6
- Add support to universal tag_invoke for class as struct with macro BOOST_DESCRIBE_CLASS HOT 10
- Unable to extract boolean value in numberic format HOT 1
- Add a shorthand to masquerade a type parsing/serializing HOT 1
- Allow unknown described class members with a parse option HOT 5
- Null-dereference warning in value_to HOT 2
- Error in parse_into() Error: unknown name or Error: incomplete JSON HOT 2
- Backwards compatibility? (new structure definitions but old json serialization) HOT 17
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 json.