-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
ordered_json vs json types comparison #3443
Comments
Interesting. I'm working on some significant changes to the comparison code for C++20. I'll take a look at comparing different There're some issues with your proposed solution. But thanks for pointing this out. I'll try to find a workable solution. |
Taking a second look, your type aliases threw me off.
|
Sorry, I just used |
So, I've just tested comparing different In summary: Two If you insist on comparing different |
This is not right! |
Don't know what to tell you, it's right there: https://json.nlohmann.me/api/basic_json/operator_eq/ Sorry, 🤷. Compares two JSON values for equality according to the following rules:
|
Then |
That would be
I also disagree that users universally expect an If that's not deemed too niche, we could add an order-agnostic comparison users can opt into as a compromise? |
I think it’s too niche. Maybe we should just add another note/example to the documentation. |
This seems like the most straightforward method. Is there anything that |
Yes, I can do, but I would prefer this would be a part of the library. Not natural to have this in consumer project. |
Except how straightforward is it to implement in a way that is actually more efficient than converting to The way I imagine it might work is to keep a second vector of indices around. Then implement a custom iterator for sorting the indices. That iterator has to return the values stored in the map, but when swapped, has to swap the indices and not the values. A boolean flag is set after a sort and cleared whenever the map is modified. The actual comparison would then compare values in the order dictated by the indices vector, which would be the same for (content-wise) identical maps. Something like that (with a few blanks left to fill in). Not difficult, but maybe also not straightforward. Maybe we need a |
I don’t think all this is required. If you care about the order, then comparison should respect the order as well. If you don’t, then don’t use ordered_json. |
I need the order for output and pretty print, for comparison using order is ridiculous! JSON is json and objects should match regardless to the order. |
If this is your specific usecase, then you need to implement your specific comparison. |
* 📝 add documentation for #3443 * Apply suggestions from code review Co-authored-by: Florian Albrechtskirchinger <falbrechtskirchinger@gmail.com>
nlohman compares jsons as strings so comparing 2 equal
nlohmann::ordered_json
objects could produce wrong results:So I would change
operator==
to this:and add operator to compare
json
andnlohmann::ordered_json
:and vice versa:
What do you think?
Compiler and operating system
gcc 9.4.0, Ubuntu 20.04.4 LTS
Library version
3.10.5
Validation
develop
branch is used.The text was updated successfully, but these errors were encountered: