File: constructor.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 (328 lines) | stat: -rw-r--r-- 10,518 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
### `jsoncons::basic_json::basic_json`

```cpp
basic_json(); (1)

basic_json(const basic_json& other); (2)

basic_json(const basic_json& other, const allocator_type& alloc); (3)

basic_json(basic_json&& other) noexcept; (4)

basic_json(basic_json&& other, const allocator_type& alloc) noexcept; (5)

template <typename T>
basic_json(const T& val); 
                                                                  (6)
template <typename T>
basic_json(const T& val, const Allocator& alloc = Allocator()); 

template <typename Unsigned>
basic_json(Unsigned val, semantic_tag tag); (7)

template <typename Signed>
basic_json(Signed val, semantic_tag tag); (8)

basic_json(half_arg_t, uint16_t value, semantic_tag tag = semantic_tag::none); (9)

basic_json(double val, semantic_tag tag); (10)

explicit basic_json(json_array_arg_t, 
    const Allocator& alloc = Allocator()); (11)

basic_json(json_array_arg_t, 
    semantic_tag tag, 
    const Allocator& alloc = Allocator()); (12)

basic_json(json_array_arg_t, 
    std::size_t count, const basic_json& value, 
    semantic_tag tag = semantic_tag::none, 
    const Allocator& alloc = Allocator());                         (13) (since 0.178.0)  

template <typename InputIt>
basic_json(json_array_arg_t, 
    InputIt first, InputIt last, semantic_tag tag = semantic_tag::none, 
    const Allocator& alloc = Allocator());                         (14) 

basic_json(json_array_arg_t, 
    std::initializer_list<basic_json> init, semantic_tag tag = semantic_tag::none, 
    const Allocator& alloc = Allocator());                         (15)

explicit basic_json(json_object_arg_t, 
    const Allocator& alloc = Allocator());                         (16) 

basic_json(json_object_arg_t, 
    semantic_tag tag, 
    const Allocator& alloc = Allocator());                         (17) 

template <typename InputIt>
basic_json(json_object_arg_t, 
    InputIt first, InputIt last, 
    semantic_tag tag = semantic_tag::none,
    const Allocator& alloc = Allocator());                         (18) 

basic_json(json_object_arg_t, 
    std::initializer_list<std::pair<std::basic_string<char_type>,basic_json>> init, 
    semantic_tag tag = semantic_tag::none, 
    const Allocator& alloc = Allocator());                         (19)

template <typename T>
basic_json(const T& val); 
                                                                   (20)
template <typename T>
basic_json(const T& val, const allocator_type& alloc); 

basic_json(const char_type* val); (21)

basic_json(const char_type* val, const allocator_type& alloc);     (22)

template <typename Source>
basic_json(byte_string_arg_t, 
    const Source& source, semantic_tag tag = semantic_tag::none,
    const Allocator& alloc = Allocator());                         (23)

template <typename Source>
basic_json(byte_string_arg_t, 
    const Source& source, uint64_t ext_tag,
    const Allocator& alloc = Allocator());                         (24) 

basic_json(json_const_pointer_arg, const basic_json* ptr);         (25) (since 0.156.0)

basic_json(json_pointer_arg, basic_json* ptr);                     (26) (since 1.0.0)
```

(1) Constructs an empty json object. 

(2) Constructs a copy of val

(3) Copy with allocator

(4) Acquires the contents of val, leaving val a `null` value

(5) Move with allocator

(6) Constructs a `basic_json` value for types supported in [json_type_traits](json_type_traits.md).

(7) Constructs a `basic_json` value from an unsigned integer and a [semantic_tag](../semantic_tag.md). This overload only participates in overload resolution if `Unsigned` is an unsigned integral type.

(8) Constructs a `basic_json` value from a signed integer and a [semantic_tag](../semantic_tag.md). This overload only participates in overload resolution if `Signed` is a signed integral type.

(9) Constructs a `basic_json` value for a half precision floating point number.
Uses [half_arg_t](../half_arg_t.md) as first argument to disambiguate overloads that construct half precision floating point numbers.

(10) Constructs a `basic_json` value from a double and a [semantic_tag](../semantic_tag.md).

(11)-(15) use [json_array_arg_t](../json_aray_arg_t.md) as first argument to disambiguate overloads that construct json arrays.

(11) Constructs a json array with the provided allocator.

(12) Constructs a json array with the provided [semantic_tag](../semantic_tag.md) and allocator.

(13) Constructs a json array with `count` copies of elements with value `value`.

(14) Constructs a json array with the contents of the range `[first,last]`.
`std::iterator_traits<InputIt>::value_type` must be convertible to `basic_json`. 

(15) Constructs a json array with the contents of the initializer list `init`.

(16)-(19) use [json_object_arg_t](../json_object_arg_t.md) as first argument to disambiguate overloads that construct json objects.

(16) Constructs a json object with the provided allocator.

(17) Constructs a json object with the provided [semantic_tag](../semantic_tag.md) and allocator.

(18) Constructs a json object with the contents of the range `[first,last]`.

(19) Constructs a json object with the contents of the initializer list `init`.

(20) Constructs a `basic_json` value for types supported in [json_type_traits](json_type_traits.md) with allocator.

(21) Constructs a `basic_json` value from a text string.

(22) Constructs a `basic_json` value from a text string with supplied allocator.

(23) Constructs a `basic_json` value for a byte string from a contiguous byte sequence provided by `source`
with a generic tag.
Type `Source` must be a contiguous container that has member functions `data()` and `size()`, and member type `value_type` 
with width of exactly 8 bits (since 0.152.0.)
Any of the values types `int8_t`, `uint8_t`, `char`, `unsigned char` and `std::byte` (since C++17) are allowed.

Uses [byte_string_arg_t](../byte_string_arg_t.md) as first argument to disambiguate overloads that construct byte strings.

(24) Constructs a `basic_json` value for a byte string from a contiguous byte sequence provided by `source`
with a format specific tag.
Type `Source` must be a contiguous container that has member functions `data()` and `size()`, and member type `value_type` 
with width of exactly 8 bits (since 0.152.0.)
Any of the values types `int8_t`, `uint8_t`, `char`, `unsigned char` and `std::byte` (since C++17) are allowed.

Uses [byte_string_arg_t](../byte_string_arg_t.md) as first argument to disambiguate overloads that construct byte strings.

(25) Constructs a `basic_json` value that provides a non-owning view of
another `basic_json` value. If second argument `ptr` is null,
constructs a `null` value.

(26) Constructs a `basic_json` value that provides a non-owning view of
another `basic_json` value. If second argument `ptr` is null,
constructs a `null` value. 

### Helpers

Helper                |Definition
--------------------|------------------------------
[json_object_arg][../json_object_arg.md] |    
[json_object_arg_t][../json_object_arg_t.md] | json object construction tag
[json_array_arg][../json_array_arg.md] |
[json_array_arg_t][../json_array_arg_t.md] | json array construction tag
[byte_string_arg][../byte_string_arg.md] |
[byte_string_arg_t][../byte_string_arg_t.md] | byte string construction tag
[half_arg][../half_arg.md] |
[half_arg_t][../half_arg_t.md] | half precision floating point number construction tag

### Examples

#### Basic examples

```cpp
#include <stdexcept>
#include <string>
#include <vector>
#include <map>
#include <jsoncons/json.hpp>

using namespace jsoncons;
int main()
{
    json j1; // An empty object
    std::cout << "(1) " << j1 << '\n';

    json j2(json_object_arg, {{"baz", "qux"}, {"foo", "bar"}}); // An object 
    std::cout << "(2) " << j2 << '\n';

    json j3(json_array_arg, {"bar", "baz"}); // An array 
    std::cout << "(3) " << j3 << '\n';
  
    json j4(json::null()); // A null value
    std::cout << "(4) " << j4 << '\n';
    
    json j5(true); // A boolean value
    std::cout << "(5) " << j5 << '\n';

    double x = 1.0/7.0;

    json j6(x); // A double value
    std::cout << "(6) " << j6 << '\n';

    json j8("Hello"); // A text string
    std::cout << "(8) " << j8 << '\n';

    std::vector<int> v = {10,20,30};
    json j9 = v; // From a sequence container
    std::cout << "(9) " << j9 << '\n';

    std::map<std::string, int> m{ {"one", 1}, {"two", 2}, {"three", 3} };
    json j10 = m; // From an associative container
    std::cout << "(10) " << j10 << '\n';

    std::vector<uint8_t> bytes = {'H','e','l','l','o'};
    json j11(byte_string_arg, bytes); // A byte string
    std::cout << "(11) " << j11 << '\n';

    json j12(half_arg, 0x3bff);
    std::cout << "(12) " << j12.as_double() << '\n';
}
```
Output:
```
(1) {}
(2) {"baz":"qux","foo":"bar"}
(3) ["bar","baz"]
(4) null
(5) true
(6) 0.14285714285714285
(8) "Hello"
(9) [10,20,30]
(10) {"one":1,"three":3,"two":2}
(11) "SGVsbG8"
(12) 0.999512
```

#### A json array that contains non-owning references to other json values

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

int main()
{
    std::string input = R"(
    {
      "machines": [
        {"id": 1, "state": "running"},
        {"id": 2, "state": "stopped"},
        {"id": 3, "state": "running"}
      ]
    }        
    )";

    json j = json::parse(input);

    json j_v(json_array_arg);
    for (const auto& item : j.at("machines").array_range())
    {
        if (item.at("state").as<std::string>() == "running")
        {
            j_v.emplace_back(json_const_pointer_arg, &item);
        }
    }

    std::cout << "\n(1)\n" << pretty_print(j_v) << "\n\n";

    for (const auto& item : j_v.array_range())
    {
        std::cout << "json type: " << item.type() << ", storage kind: " << item.storage() << "\n";
    }

    json j2 = deep_copy(j_v);

    std::cout << "\n(2)\n" << pretty_print(j2) << "\n\n";

    for (const auto& item : j2.array_range())
    {
        std::cout << "json type: " << item.type() << ", storage kind: " << item.storage() << "\n";
    }
}

```
Output:
```json
(1)
[
    {
        "id": 1,
        "state": "running"
    },
    {
        "id": 3,
        "state": "running"
    }
]

json type: object, storage kind: json const pointer
json type: object, storage kind: json const pointer

(2)
[
    {
        "id": 1,
        "state": "running"
    },
    {
        "id": 3,
        "state": "running"
    }
]

json type: object, storage kind: object
json type: object, storage kind: object
```