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

Improve JSON test to verify that code works with various fields order #274

Closed
nuald opened this issue Oct 16, 2020 · 0 comments
Closed

Improve JSON test to verify that code works with various fields order #274

nuald opened this issue Oct 16, 2020 · 0 comments

Comments

@nuald
Copy link
Collaborator

nuald commented Oct 16, 2020

Per RFC 7159:

An object is an unordered collection of zero or more name/value pairs

JSON parsers shouldn't assume the order of fields in the most of the use-cases (surely, it's possible to have the exceptions when the both JSON and its parser are in the controlled environment). As this project is not about micro-optimizations, but rather the usual code, it could be quite confusing for the average software developer that their code is broken when they parse totally valid JSON.

Therefore, it would be nice to improve tests to verify that the parser is order-insensitive. The following example demonstrates the approach:

  for (const padded_string &v : {
      "{\"coordinates\":[{\"x\":1.1,\"y\":2.2,\"z\":3.3}]}"_padded,
      "{\"coordinates\":[{\"y\":2.2,\"x\":1.1,\"z\":3.3}]}"_padded}) {
    auto left = calc(v);
    auto right = coordinate_t(1.1, 2.2, 3.3);
    if (left != right) {
      cerr << left << " != " << right << endl;
      exit(EXIT_FAILURE);
    }
  }
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

1 participant