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

std.json: Document opEquals #8975

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions std/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,22 @@ struct JSONValue
assert(j["author"].str == "Walter");
}

///
/**
* Compare two JSONValues for equality
*
* JSON arrays and objects are compared deeply. The order of object keys does not matter.
*
* Floating point numbers are compared for exact equality, not approximal equality.
*
* Different number types (unsigned, signed, and floating) will be compared by converting
* them to a common type, in the same way that comparison of built-in D `int`, `uint` and
* `float` works.
*
* Other than that, types must match exactly.
* Empty arrays are not equal to empty objects, and booleans are never equal to integers.
*
* Returns: whether this `JSONValue` is equal to `rhs`
*/
bool opEquals(const JSONValue rhs) const @nogc nothrow pure @safe
{
return opEquals(rhs);
Expand Down Expand Up @@ -871,9 +886,13 @@ struct JSONValue
///
@safe unittest
{
assert(JSONValue(0u) == JSONValue(0));
assert(JSONValue(0u) == JSONValue(0.0));
assert(JSONValue(0) == JSONValue(0.0));
assert(JSONValue(10).opEquals(JSONValue(10.0)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I use == here, the generator will convert it to:

writeln(JSONValue(10)); // JSONValue(10.0)

Hence I'm calling opEquals explicitly here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall such awkward situations coming up before. Maybe it's time to add some kind of // no-writeln syntax to https://github.com/dlang/dlang.org/blob/master/ddoc/source/assert_writeln_magic.d ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or it could exempt documented unittests under opEquals

assert(JSONValue(10) != (JSONValue(10.5)));

assert(JSONValue(1) != JSONValue(true));
assert(JSONValue.emptyArray != JSONValue.emptyObject);

assert(parseJSON(`{"a": 1, "b": 2}`).opEquals(parseJSON(`{"b": 2, "a": 1}`)));
}

/// Implements the foreach `opApply` interface for json arrays.
Expand Down
Loading