]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/opentelemetry-cpp/third_party/nlohmann-json/doc/mkdocs/docs/api/basic_json/value.md
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / third_party / nlohmann-json / doc / mkdocs / docs / api / basic_json / value.md
CommitLineData
1e59de90
TL
1# <small>nlohmann::basic_json::</small>value
2
3```cpp
4// (1)
5template<class ValueType>
6ValueType value(const typename object_t::key_type& key,
7 const ValueType& default_value) const;
8
9// (2)
10template<class ValueType>
11ValueType value(const json_pointer& ptr,
12 const ValueType& default_value) const;
13```
14
151. Returns either a copy of an object's element at the specified key `key` or a given default value if no element with
16 key `key` exists.
17
18 The function is basically equivalent to executing
19 ```cpp
20 try {
21 return at(key);
22 } catch(out_of_range) {
23 return default_value;
24 }
25 ```
26
272. Returns either a copy of an object's element at the specified JSON pointer `ptr` or a given default value if no value
28 at `ptr` exists.
29
30 The function is basically equivalent to executing
31 ```cpp
32 try {
33 return at(ptr);
34 } catch(out_of_range) {
35 return default_value;
36 }
37 ```
38
39!!! note
40
41 - Unlike [`at`](at.md), this function does not throw if the given `key`/`ptr` was not found.
42 - Unlike [`operator[]`](operator[].md), this function does not implicitly add an element to the position defined by
43 `key`/`ptr` key. This function is furthermore also applicable to const objects.
44
45## Template parameters
46
47`ValueType`
48: type compatible to JSON values, for instance `#!cpp int` for JSON integer numbers, `#!cpp bool` for JSON booleans,
49 or `#!cpp std::vector` types for JSON arrays. Note the type of the expected value at `key`/`ptr` and the default
50 value `default_value` must be compatible.
51
52## Parameters
53
54`key` (in)
55: key of the element to access
56
57`default_value` (in)
58: the value to return if key/ptr found no value
59
60`ptr` (in)
61: a JSON pointer to the element to access
62
63## Return value
64
651. copy of the element at key `key` or `default_value` if `key` is not found
661. copy of the element at JSON Pointer `ptr` or `default_value` if no value for `ptr` is found
67
68## Exception safety
69
70Strong guarantee: if an exception is thrown, there are no
71changes to any JSON value.
72
73## Exceptions
74
751. The function can throw the following exceptions:
76 - Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) if `default_value` does not match
77 the type of the value at `key`
78 - Throws [`type_error.306`](../../home/exceptions.md#jsonexceptiontype_error306) if the JSON value is not an object;
79 in that case, using `value()` with a key makes no sense.
802. The function can throw the following exceptions:
81 - Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) if `default_value` does not match
82 the type of the value at `ptr`
83 - Throws [`type_error.306`](../../home/exceptions.md#jsonexceptiontype_error306) if the JSON value is not an object;
84 in that case, using `value()` with a key makes no sense.
85
86## Complexity
87
881. Logarithmic in the size of the container.
892. Logarithmic in the size of the container.
90
91## Examples
92
93??? example "Example (1): access specified object element with default value"
94
95 The example below shows how object elements can be queried with a default value.
96
97 ```cpp
98 --8<-- "examples/basic_json__value.cpp"
99 ```
100
101 Output:
102
103 ```json
104 --8<-- "examples/basic_json__value.output"
105 ```
106
107??? example "Example (2): access specified object element via JSON Pointer with default value"
108
109 The example below shows how object elements can be queried with a default value.
110
111 ```cpp
112 --8<-- "examples/basic_json__value_ptr.cpp"
113 ```
114
115 Output:
116
117 ```json
118 --8<-- "examples/basic_json__value_ptr.output"
119 ```
120
121## See also
122
123- see [`at`](at.md) for access by reference with range checking
124- see [`operator[]`](operator%5B%5D.md) for unchecked access by reference
125
126## Version history
127
1281. Added in version 1.0.0.
1292. Added in version 2.0.2.