File: merge_or_update.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 (59 lines) | stat: -rw-r--r-- 1,025 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
### jsoncons::basic_json::merge_or_update

```cpp
void merge_or_update(const basic_json& source); (1)
void merge_or_update(basic_json&& source); (2)
void merge_or_update(object_iterator hint, const basic_json& source); (3)
void merge_or_update(object_iterator hint, basic_json&& source); (4)
```

Inserts another json object's key-value pairs into a json object, or assigns them if they already exist.

The `merge_or_update` function performs only a one-level-deep shallow merge, not a deep merge of nested objects.

#### Parameters

<table>
  <tr>
    <td><code>source</code></td>
    <td>`json` object value</td> 
  </tr>
</table>

#### Return value

None

#### Exceptions

Throws `std::domain_error` if source or *this are not json objects.

### Examples

#### Merge or update `json`

```cpp
json j = json::parse(R"(
{
    "a" : 1,
    "b" : 2
}
)");

const json source = json::parse(R"(
{
    "a" : 2,
    "c" : 3
}
)");

j1.merge_or_update(source);

std::cout << j << endl;
```
Output:

```json
{"a":2,"b":2,"c":3}
```