mirror of
https://github.com/nlohmann/json.git
synced 2026-10-12 01:17:14 +00:00
Document and test first-wins lookups next to dump() and ==
dump() writes every member and == resolves duplicate keys as parse() does, whereas lookups find the first member of a duplicate key. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
3 files changed
+9
-2
No files matched your search
@@ -18,6 +18,8 @@ bool operator==(const BasicJsonType& lhs, const basic_json_view& rhs);
|
|||||||
Neither overload builds a `BasicJsonType` value for a view to do the comparison (see [Notes](#notes) below). Numbers
|
Neither overload builds a `BasicJsonType` value for a view to do the comparison (see [Notes](#notes) below). Numbers
|
||||||
compare by value across their types (`#!cpp 1 == 1.0`), and an object compares by its members, with duplicate keys
|
compare by value across their types (`#!cpp 1 == 1.0`), and an object compares by its members, with duplicate keys
|
||||||
resolved exactly as `parse()` resolves them -- the last value, at the position of the first occurrence of the key.
|
resolved exactly as `parse()` resolves them -- the last value, at the position of the first occurrence of the key.
|
||||||
|
This is not what [`operator[]`](operator[].md) and the other lookups return for a duplicate key: they find the *first*
|
||||||
|
member (see [Duplicate keys](../../features/json_view.md#duplicate-keys)).
|
||||||
|
|
||||||
## Parameters
|
## Parameters
|
||||||
|
|
||||||
|
|||||||
@@ -221,8 +221,8 @@ Both results are only valid as long as the view -- and, for a string with no esc
|
|||||||
[`dump()`](../api/basic_json_view/dump.md) serializes a view directly from the flat index, without ever building a
|
[`dump()`](../api/basic_json_view/dump.md) serializes a view directly from the flat index, without ever building a
|
||||||
`basic_json` value. An object's members are written in document order, not sorted by key, and *every* occurrence of a
|
`basic_json` value. An object's members are written in document order, not sorted by key, and *every* occurrence of a
|
||||||
repeated key is written, not only the last one -- the same two ways [iteration](#what-is-different) already differs
|
repeated key is written, not only the last one -- the same two ways [iteration](#what-is-different) already differs
|
||||||
from a [`materialize()`](../api/basic_json_view/materialize.md)d value, see above. `#!cpp materialize().dump()` gives
|
from a [`materialize()`](../api/basic_json_view/materialize.md)d value, see [Duplicate keys](#duplicate-keys).
|
||||||
a different result in both respects for a `json_view`.
|
`#!cpp materialize().dump()` gives a different result in both respects for a `json_view`.
|
||||||
|
|
||||||
By default, numbers are written the way [`basic_json::dump()`](../api/basic_json/dump.md) would.
|
By default, numbers are written the way [`basic_json::dump()`](../api/basic_json/dump.md) would.
|
||||||
[`number_format::source`](../api/basic_json_view/number_format.md) instead copies every number exactly as it was
|
[`number_format::source`](../api/basic_json_view/number_format.md) instead copies every number exactly as it was
|
||||||
|
|||||||
@@ -1470,6 +1470,8 @@ TEST_CASE("json_view dump")
|
|||||||
const json_document d = json_document::parse(R"({"b": 1, "a": 2, "b": 3})");
|
const json_document d = json_document::parse(R"({"b": 1, "a": 2, "b": 3})");
|
||||||
CHECK(d.root().dump() == R"({"b":1,"a":2,"b":3})");
|
CHECK(d.root().dump() == R"({"b":1,"a":2,"b":3})");
|
||||||
CHECK(d.root().dump(1) == "{\n \"b\": 1,\n \"a\": 2,\n \"b\": 3\n}");
|
CHECK(d.root().dump(1) == "{\n \"b\": 1,\n \"a\": 2,\n \"b\": 3\n}");
|
||||||
|
// dump() writes all members, though the lookup finds the first
|
||||||
|
CHECK(d.root()["b"].dump() == "1");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("deep nesting")
|
SECTION("deep nesting")
|
||||||
@@ -1592,6 +1594,9 @@ TEST_CASE("json_view comparison")
|
|||||||
const ordered_json_document dup = ordered_json_document::parse(R"({"a": 1, "b": 2, "a": 3})");
|
const ordered_json_document dup = ordered_json_document::parse(R"({"a": 1, "b": 2, "a": 3})");
|
||||||
const ordered_json_document last = ordered_json_document::parse(R"({"a": 3, "b": 2})");
|
const ordered_json_document last = ordered_json_document::parse(R"({"a": 3, "b": 2})");
|
||||||
CHECK(dup.root() == last.root());
|
CHECK(dup.root() == last.root());
|
||||||
|
// (== compares as parse() resolves duplicates, a lookup finds the first)
|
||||||
|
CHECK(dup.root()["a"].materialize() == 1);
|
||||||
|
CHECK(dup.root()["a"] != last.root()["a"]);
|
||||||
const ordered_json_document ab = ordered_json_document::parse(R"({"a": 1, "b": 2})");
|
const ordered_json_document ab = ordered_json_document::parse(R"({"a": 1, "b": 2})");
|
||||||
const ordered_json_document ba = ordered_json_document::parse(R"({"b": 2, "a": 1})");
|
const ordered_json_document ba = ordered_json_document::parse(R"({"b": 2, "a": 1})");
|
||||||
CHECK(ab.root() != ba.root());
|
CHECK(ab.root() != ba.root());
|
||||||
|
|||||||
Reference in new issue
Block a user