File: get_value_or.md

package info (click to toggle)
jsoncons 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,584 kB
  • sloc: cpp: 136,382; sh: 33; makefile: 5
file content (37 lines) | stat: -rw-r--r-- 912 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
### jsoncons::basic_json::get_value_or

```cpp
template <typename T,typename U>
T get_value_or(const string_view_type& name, U&& default_value) const; 
```

Returns a value in a basic_json object
if `name` matches the name of a member, 
otherwise returns a default value.
Throws `std::domain_error` if not an object or null value.

### Type requirements

- `T` must meet the requirements of [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible) 
- `U` must be convertible to `T`

### Examples

#### Return a value if available, a default value if not 

```cpp
#include <jsoncons/json.hpp>

int main()
{
    json j(json_object_arg, {{"author","Evelyn Waugh"},{"title","Sword of Honour"}});

    std::cout << j.get_value_or<std::string>("author","unknown") << "\n";
    std::cout << j.get_value_or<std::string>("category","fiction") << "\n";
}
```
Output:
```
Evelyn Waugh
fiction
```