2
2
3
3
## Overview
4
4
5
- In many situations such as configuration files, missing values are not exceptional, but may be treated as if a default value was present.
5
+ In many situations such as configuration files, missing values are not exceptional, but may be treated as if a default
6
+ value was present. For this case, use [ ` value(key, default_value) ` ] ( ../../api/basic_json/value.md ) which takes the key
7
+ you want to access and a default value in case there is no value stored with that key.
8
+
9
+ ## Example
6
10
7
11
??? example
8
12
@@ -17,16 +21,43 @@ In many situations such as configuration files, missing values are not exception
17
21
18
22
Assume the value is parsed to a `json` variable `j`.
19
23
20
- | expression | value |
21
- | ---------- | ----- |
22
- | `#!cpp j` | `#!json {"logOutput": "result.log", "append": true}` |
23
- | `#!cpp j.value("logOutput", "logfile.log")` | `#!json "result.log"` |
24
- | `#!cpp j.value("append", true)` | `#!json true` |
25
- | `#!cpp j.value("append", false)` | `#!json true` |
26
- | `#!cpp j.value("logLevel", "verbose")` | `#!json "verbose"` |
24
+ | expression | value |
25
+ |---------------------------------------------| ------------------------------------------------------ |
26
+ | `#!cpp j` | `#!json {"logOutput": "result.log", "append": true}` |
27
+ | `#!cpp j.value("logOutput", "logfile.log")` | `#!json "result.log"` |
28
+ | `#!cpp j.value("append", true)` | `#!json true` |
29
+ | `#!cpp j.value("append", false)` | `#!json true` |
30
+ | `#!cpp j.value("logLevel", "verbose")` | `#!json "verbose"` |
27
31
28
- ## Note
32
+ ## Notes
29
33
30
34
!!! failure "Exceptions"
31
35
32
36
- `value` can only be used with objects. For other types, a [`basic_json::type_error`](../../home/exceptions.md#jsonexceptiontype_error306) is thrown.
37
+
38
+ !!! warning "Return type"
39
+
40
+ The value function is a template, and the return type of the function is determined by the type of the provided
41
+ default value unless otherwise specified. This can have unexpected effects. In the example below, we store a 64-bit
42
+ unsigned integer. We get exactly that value when using [`operator[]`](../../api/basic_json/operator[].md). However,
43
+ when we call `value` and provide `#!c 0` as default value, then `#!c -1` is returned. The occurs, because `#!c 0`
44
+ has type `#!c int` which overflows when handling the value `#!c 18446744073709551615`.
45
+
46
+ To address this issue, either provide a correctly typed default value or use the template parameter to specify the
47
+ desired return type. Note that this issue occurs even when a value is stored at the provided key, and the default
48
+ value is not used as the return value.
49
+
50
+ ```cpp
51
+ --8<-- "examples/value__return_type.cpp"
52
+ ```
53
+
54
+ Output:
55
+
56
+ ```json
57
+ --8<-- "examples/value__return_type.output"
58
+ ```
59
+
60
+ ## See also
61
+
62
+ - [ ` value ` ] ( ../../api/basic_json/value.md ) for access with default value
63
+ - documentation on [ checked access] ( checked_access.md )
0 commit comments