-
Notifications
You must be signed in to change notification settings - Fork 274
JSON equality operator #4912
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
JSON equality operator #4912
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ Author: Daniel Kroening, [email protected] | |
#include "json.h" | ||
|
||
#include <ostream> | ||
#include <algorithm> | ||
|
||
const jsont jsont::null_json_object(jsont::kindt::J_NULL); | ||
|
||
|
@@ -162,3 +163,44 @@ void jsont::swap(jsont &other) | |
other.object.swap(object); | ||
other.value.swap(value); | ||
} | ||
|
||
bool operator==(const jsont &left, const jsont &right) | ||
{ | ||
if(left.kind != right.kind) | ||
return false; | ||
switch(left.kind) | ||
{ | ||
case jsont::kindt::J_NULL: | ||
return true; | ||
case jsont::kindt::J_TRUE: | ||
return true; | ||
case jsont::kindt::J_FALSE: | ||
return true; | ||
case jsont::kindt::J_NUMBER: | ||
return left.value == right.value; | ||
case jsont::kindt::J_STRING: | ||
return left.value == right.value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overall design question: shouldn't we do this more in a visitor-style where each call deriving from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, except There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course, hence me saying "visitor-style." But also my comment was intended as a thought, not any kind of hard requirement. |
||
case jsont::kindt::J_ARRAY: | ||
{ | ||
const auto &left_array = static_cast<const json_arrayt &>(left); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ️ There is a function called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const auto &right_array = static_cast<const json_arrayt &>(right); | ||
return left_array.size() == right_array.size() && | ||
std::equal( | ||
left_array.begin(), left_array.end(), right_array.begin()); | ||
} | ||
case jsont::kindt::J_OBJECT: | ||
{ | ||
const auto &left_object = static_cast<const json_objectt &>(left); | ||
const auto &right_object = static_cast<const json_objectt &>(right); | ||
if(left_object.size() != left_object.size()) | ||
return false; | ||
return std::all_of( | ||
left_object.begin(), | ||
left_object.end(), | ||
[&](const std::pair<std::string, jsont> &pair) { | ||
return right_object[pair.first] == pair.second; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's an unnecessary lookup when instead you could just use two iterators over the two containers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, but if someone decides to swap There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do wonder which containers would exhibit differences in strict equality testing? But I suppose it is conceivable that two There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I still favour
|
||
}); | ||
} | ||
} | ||
UNREACHABLE; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe removing this is the right thing to do, but then we should remove all the code computing
leading_text
,trailing_text
above. Or maybe you should just useNOLINTNEXTLINE(whitespace/braces)
in the test.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clang-format
already checks for positions and spacing of curly braces and parentheses. The problem stems from the fact this isn't aware of initializer lists/uniform initialization.In the days of C++98, the above wasn't a valid syntax. Curly braces would appear only after
if
/for
statements, as block scopes or surround function bodies. The linter expects this to still be the case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, in which case the linter should be cleaned properly so as not to leave in place unused code.