Skip to content

Commit d5780aa

Browse files
dkorpelDennis Korpel
and
Dennis Korpel
authored
std.json: Document opEquals (#8975)
Co-authored-by: Dennis Korpel <[email protected]>
1 parent 82b745d commit d5780aa

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

std/json.d

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,22 @@ struct JSONValue
804804
assert(j["author"].str == "Walter");
805805
}
806806

807-
///
807+
/**
808+
* Compare two JSONValues for equality
809+
*
810+
* JSON arrays and objects are compared deeply. The order of object keys does not matter.
811+
*
812+
* Floating point numbers are compared for exact equality, not approximal equality.
813+
*
814+
* Different number types (unsigned, signed, and floating) will be compared by converting
815+
* them to a common type, in the same way that comparison of built-in D `int`, `uint` and
816+
* `float` works.
817+
*
818+
* Other than that, types must match exactly.
819+
* Empty arrays are not equal to empty objects, and booleans are never equal to integers.
820+
*
821+
* Returns: whether this `JSONValue` is equal to `rhs`
822+
*/
808823
bool opEquals(const JSONValue rhs) const @nogc nothrow pure @safe
809824
{
810825
return opEquals(rhs);
@@ -871,9 +886,13 @@ struct JSONValue
871886
///
872887
@safe unittest
873888
{
874-
assert(JSONValue(0u) == JSONValue(0));
875-
assert(JSONValue(0u) == JSONValue(0.0));
876-
assert(JSONValue(0) == JSONValue(0.0));
889+
assert(JSONValue(10).opEquals(JSONValue(10.0)));
890+
assert(JSONValue(10) != (JSONValue(10.5)));
891+
892+
assert(JSONValue(1) != JSONValue(true));
893+
assert(JSONValue.emptyArray != JSONValue.emptyObject);
894+
895+
assert(parseJSON(`{"a": 1, "b": 2}`).opEquals(parseJSON(`{"b": 2, "a": 1}`)));
877896
}
878897

879898
/// Implements the foreach `opApply` interface for json arrays.

0 commit comments

Comments
 (0)