From 4d45408a407aa4057878aa6db91d8e9b2ef2e76f Mon Sep 17 00:00:00 2001 From: Dennis Korpel Date: Mon, 8 Apr 2024 12:17:49 +0200 Subject: [PATCH] std.json: Document `opEquals` --- std/json.d | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/std/json.d b/std/json.d index 6e94a5d71ce..22249a8f66b 100644 --- a/std/json.d +++ b/std/json.d @@ -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); @@ -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))); + 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.