WhisperCom/doc/mkdocs/docs/features/element_access/default_value.md
Dominik Meyer cc002ebfbb Squashed 'libs/json/' content from commit f42a74b8
git-subtree-dir: libs/json
git-subtree-split: f42a74b8f53cc308647123d49d33d1c8122e3f42
2021-08-22 01:28:31 +02:00

978 B

Access with default value: value

Overview

In many situations such as configuration files, missing values are not exceptional, but may be treated as if a default value was present.

??? example

Consider the following JSON value:

```json
{
    "logOutput": "result.log",
    "append": true
}
```

Assume the value is parsed to a `json` variable `j`.

| expression | value |
| ---------- | ----- |
| `#!cpp j`  | `#!json {"logOutput": "result.log", "append": true}` |
| `#!cpp j.value("logOutput", "logfile.log")`  | `#!json "result.log"` |
| `#!cpp j.value("append", true)`  | `#!json true` |
| `#!cpp j.value("append", false)`  | `#!json true` |
| `#!cpp j.value("logLevel", "verbose")`  | `#!json "verbose"` |

Note

!!! failure "Exceptions"

- `value` can only be used with objects. For other types, a [`basic_json::type_error`](../../home/exceptions.md#jsonexceptiontype_error306) is thrown.