mirror of
https://github.com/nlohmann/json.git
synced 2025-07-28 12:02:00 +03:00
Consolidate documentation (#3071)
* 🔥 consolidate documentation * ♻️ overwork std specializations * 🚚 move images files to mkdocs * ♻️ fix URLs * 🔧 tweak MkDocs configuration * 🔧 add namespaces * 📝 document deprecations * 📝 document documentation generation * 🚸 improve search * 🚸 add examples * 🚧 start adding documentation for macros * 📝 add note for https://github.com/nlohmann/json/issues/874#issuecomment-1001699139 * 📝 overwork example handling * 📝 fix Markdown tables
This commit is contained in:
@ -1,12 +1,17 @@
|
||||
# basic_json::contains
|
||||
# <small>nlohmann::basic_json::</small>contains
|
||||
|
||||
```cpp
|
||||
// (1)
|
||||
template<typename KeyT>
|
||||
bool contains(KeyT && key) const;
|
||||
|
||||
// (2)
|
||||
bool contains(const json_pointer& ptr) const;
|
||||
```
|
||||
|
||||
Check whether an element exists in a JSON object with key equivalent to `key`. If the element is not found or the JSON
|
||||
value is not an object, `#!cpp false` is returned.
|
||||
1. Check whether an element exists in a JSON object with key equivalent to `key`. If the element is not found or the
|
||||
JSON value is not an object, `#!cpp false` is returned.
|
||||
2. Check whether the given JSON pointer `ptr` can be resolved in the current JSON value.
|
||||
|
||||
## Template parameters
|
||||
|
||||
@ -17,27 +22,45 @@ value is not an object, `#!cpp false` is returned.
|
||||
|
||||
`key` (in)
|
||||
: key value to check its existence.
|
||||
|
||||
|
||||
`ptr` (in)
|
||||
: JSON pointer to check its existence.
|
||||
|
||||
## Return value
|
||||
|
||||
`#!cpp true` if an element with specified `key` exists. If no such element with such key is found or the JSON value is
|
||||
not an object, `#!cpp false` is returned.
|
||||
1. `#!cpp true` if an element with specified `key` exists. If no such element with such key is found or the JSON value
|
||||
is not an object, `#!cpp false` is returned.
|
||||
2. `#!cpp true` if the JSON pointer can be resolved to a stored value, `#!cpp false` otherwise.
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong exception safety: if an exception occurs, the original value stays intact.
|
||||
|
||||
## Exceptions
|
||||
|
||||
1. The function does not throw exceptions.
|
||||
2. The function can throw the following exceptions:
|
||||
- Throws [`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) if an array index begins with
|
||||
`0`.
|
||||
- Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) if an array index was not a
|
||||
number.
|
||||
|
||||
## Complexity
|
||||
|
||||
Logarithmic in the size of the JSON object.
|
||||
|
||||
## Notes
|
||||
|
||||
This method always returns `#!cpp false` when executed on a JSON type that is not an object.
|
||||
1. This method always returns `#!cpp false` when executed on a JSON type that is not an object.
|
||||
2. This method can be executed on any JSON value type.
|
||||
|
||||
## Example
|
||||
!!! info "Postconditions"
|
||||
|
||||
??? example
|
||||
If `#!cpp j.contains(x)` returns `#!c true` for a key or JSON pointer `x`, then it is safe to call `j[x]`.
|
||||
|
||||
## Examples
|
||||
|
||||
??? example "Example (1) check with key"
|
||||
|
||||
The example shows how `contains()` is used.
|
||||
|
||||
@ -51,6 +74,21 @@ This method always returns `#!cpp false` when executed on a JSON type that is no
|
||||
--8<-- "examples/contains.output"
|
||||
```
|
||||
|
||||
??? example "Example (1) check with JSON pointer"
|
||||
|
||||
The example shows how `contains()` is used.
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/contains_json_pointer.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/contains_json_pointer.output"
|
||||
```
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.6.0.
|
||||
1. Added in version 3.6.0.
|
||||
2. Added in version 3.7.0.
|
||||
|
Reference in New Issue
Block a user