mirror of
https://github.com/nlohmann/json.git
synced 2026-10-12 01:17:14 +00:00
Test discarded views with is_discarded() in the operator[] docs and examples
The explicit conversion to bool of basic_json_view is gone. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
3 files changed
+8
-5
No files matched your search
@@ -93,7 +93,7 @@ None of these exceptions carry a [`JSON_DIAGNOSTICS`](../macros/json_diagnostics
|
|||||||
Unlike `BasicJsonType::operator[]`, which is undefined behavior (guarded by a
|
Unlike `BasicJsonType::operator[]`, which is undefined behavior (guarded by a
|
||||||
[runtime assertion](../../features/assertions.md)) for a missing key on a **const** value, this operator returns a
|
[runtime assertion](../../features/assertions.md)) for a missing key on a **const** value, this operator returns a
|
||||||
safe, testable result for a missing key or an index out of range: a [discarded](is_discarded.md) view, which is
|
safe, testable result for a missing key or an index out of range: a [discarded](is_discarded.md) view, which is
|
||||||
`#!cpp false` in a boolean context.
|
tested with [`is_discarded`](is_discarded.md).
|
||||||
There is also no non-const overload that inserts a missing key or extends an array -- a view never modifies the
|
There is also no non-const overload that inserts a missing key or extends an array -- a view never modifies the
|
||||||
document.
|
document.
|
||||||
|
|
||||||
|
|||||||
@@ -22,17 +22,19 @@ int main()
|
|||||||
std::cout << user["name"].materialize().dump();
|
std::cout << user["name"].materialize().dump();
|
||||||
|
|
||||||
// operator[] on a missing object key gives a discarded view -- test
|
// operator[] on a missing object key gives a discarded view -- test
|
||||||
// it with a plain "if". The const overload of json::operator[]
|
// it with is_discarded(). The const overload of json::operator[]
|
||||||
// would instead be undefined behavior (guarded by an assertion) for
|
// would instead be undefined behavior (guarded by an assertion) for
|
||||||
// a missing key
|
// a missing key
|
||||||
if (const auto email = user["email"])
|
const auto email = user["email"];
|
||||||
|
if (!email.is_discarded())
|
||||||
{
|
{
|
||||||
std::cout << " <" << email.materialize().dump() << ">";
|
std::cout << " <" << email.materialize().dump() << ">";
|
||||||
}
|
}
|
||||||
|
|
||||||
// the same holds for an array index past the end: a discarded view,
|
// the same holds for an array index past the end: a discarded view,
|
||||||
// not undefined behavior
|
// not undefined behavior
|
||||||
if (const auto first_tag = user["tags"][0])
|
const auto first_tag = user["tags"][0];
|
||||||
|
if (!first_tag.is_discarded())
|
||||||
{
|
{
|
||||||
std::cout << " #" << first_tag.materialize().dump();
|
std::cout << " #" << first_tag.materialize().dump();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,8 @@ int main()
|
|||||||
// a missing key or an out-of-range index along the path gives a
|
// a missing key or an out-of-range index along the path gives a
|
||||||
// discarded view, exactly where const json::operator[] would be
|
// discarded view, exactly where const json::operator[] would be
|
||||||
// undefined behavior for the same pointer
|
// undefined behavior for the same pointer
|
||||||
if (const auto missing = root[json_pointer("/region/servers/5/metrics/cpu")])
|
const auto missing = root[json_pointer("/region/servers/5/metrics/cpu")];
|
||||||
|
if (!missing.is_discarded())
|
||||||
{
|
{
|
||||||
std::cout << missing.materialize().dump() << '\n';
|
std::cout << missing.materialize().dump() << '\n';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user