Skip to content
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

Equality operator fails #931

Closed
axx opened this issue Jan 22, 2018 · 4 comments
Closed

Equality operator fails #931

axx opened this issue Jan 22, 2018 · 4 comments

Comments

@axx
Copy link

axx commented Jan 22, 2018

Bug Report

  • What is the issue you have?
    The equality operator returns false even though 2 JSON objects have the same content, just different order.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

#include < iostream >
#include "json.hpp"

int main(int argc, char** argv)
{
auto j1 = R"#(
{
"alertResponses": [
{"profileId": "CEMAV1",
"errors": [{"code": 1666, "title": "Not Allowed"}]
},
{"profileId": "CEMAV2",
"errors": [{"code": 1666, "title": "Not Allowed"}]
}]
}
)#"_json;

auto j2 = R"#(
{
"alertResponses": [
{"profileId": "CEMAV2",
"errors": [{"code": 1666, "title": "Not Allowed"}]
},
{"profileId": "CEMAV1",
"errors": [{"code": 1666, "title": "Not Allowed"}]
}]
}
)#"_json;

if (j1 == j2) std::cout << "The same!" << std::endl;
else std::cout << "Not the same!" << std::endl;

return 0;
}

  • What is the expected behavior?

"The same!" should be printed out.

  • And what is the actual behavior instead?

"Not the same!" is printed out.

g++ 4.9.3 on SUSE Linux
g++ Apple LLVM version 9.0.0 (clang-900.0.39.2) on Mac OS X

  • Did you use a released version of the library or the version from the develop branch?

It's from the "develop" branch version 2.1.1.
Same problem is found in version 3.0.1

No compilation error

Feature Request

  • Describe the feature in as much detail as possible.

  • Include sample usage where appropriate.

@gregmarr
Copy link
Contributor

I would expect two arrays having different orders to result in the JSON being different. Objects have unordered keys, but arrays are ordered. Why would you expect them to be the same?

@gregmarr
Copy link
Contributor

www.json.org

JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

@gregmarr
Copy link
Contributor

These two would be the same, for example:

int main(int argc, char** argv)
{
    auto j1 = R"#({
        "alertResponses": {
            "CEMAV1": [{"code": 1666, "title": "Not Allowed"}],
            "CEMAV2": [{"code": 1666, "title": "Not Allowed"}]
        }
    })#"_json;

    auto j2 = R"#({
        "alertResponses": {
            "CEMAV2": [{"code": 1666, "title": "Not Allowed"}],
            "CEMAV1": [{"code": 1666, "title": "Not Allowed"}]
        }
    })#"_json;

@axx
Copy link
Author

axx commented Jan 22, 2018

OK, fair enough. I missed the part about the ordered list. Thanks for the clarification.

@axx axx closed this as completed Jan 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants