File: parse.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 (268 lines) | stat: -rw-r--r-- 6,960 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
### jsoncons::basic_json::parse

```cpp
template <typename Source>
static basic_json parse(const Source& source, 
    const basic_json_decode_options<char_type>& options = basic_json_decode_options<CharT>());   (1) 

static basic_json parse(const char_type* str, 
    const basic_json_decode_options<char_type>& options = basic_json_decode_options<CharT>());   (2) 

static basic_json parse(const char_type* str, std::size_t length, 
    const basic_json_decode_options<char_type>& options = basic_json_decode_options<CharT>());   (3) 

static basic_json parse(std::basic_istream<char_type>& is, 
    const basic_json_decode_options<char_type>& options = basic_json_decode_options<CharT>());   (4) 

template <typename InputIt>
static basic_json parse(InputIt first, InputIt last, 
    const basic_json_decode_options<char_type>& options = basic_json_decode_options<CharT>());   (5) 

template <typename Source,typename TempAllocator>
static basic_json parse(const allocator_set<allocator_type,TempAllocator>& alloc_set, 
    const Source& source, 
    const basic_json_decode_options<char_type>& options = basic_json_decode_options<CharT>());   (6) (since 0.171.0)

template <typename TempAllocator>
static basic_json parse(const allocator_set<allocator_type,TempAllocator>& alloc_set,
    const char_type* str, 
    const basic_json_decode_options<char_type>& options = basic_json_decode_options<CharT>());   (7) (since 0.171.0)

template <typename TempAllocator>
static basic_json parse(const allocator_set<allocator_type,TempAllocator>& alloc_set,
    const char_type* str, std::size_t length, 
    const basic_json_decode_options<char_type>& options = basic_json_decode_options<CharT>());   (8) (since 0.177.0)

template <typename TempAllocator>
static basic_json parse(const allocator_set<allocator_type,TempAllocator>& alloc_set, 
    std::basic_istream<char_type>& is, 
    const basic_json_decode_options<char_type>& options = basic_json_decode_options<CharT>());   (9) (since 0.171.0)

template <typename InputIt,typename TempAllocator>
static basic_json parse(const allocator_set<allocator_type,TempAllocator>& alloc_set,
    InputIt first, InputIt last, 
    const basic_json_decode_options<char_type>& options = basic_json_decode_options<CharT>());   (10) (since 0.171.0)
```
(1) Parses JSON data from a contiguous character sequence provided by `source` and returns a `basic_json` value. 
Throws a [ser_error](../ser_error.md) if parsing fails.

(2) Parses JSON data from a null-terminated character string and returns a `basic_json` value. 
Throws a [ser_error](../ser_error.md) if parsing fails.

(3) Parses JSON data from a string and length and returns a `basic_json` value. 
Throws a [ser_error](../ser_error.md) if parsing fails.

(4) Parses JSON data from an input stream and returns a `basic_json` value. 
Throws a [ser_error](../ser_error.md) if parsing fails.

(5) Parses JSON data from the range [`first`,`last`) and returns a `basic_json` value. 
Throws a [ser_error](../ser_error.md) if parsing fails.

(6)-(10) Same as (1)-(5), except they accept an [allocator_set](allocator_set.md) argument.

#### Parameters

`source` = a contigugous character source, such as a `std::string` or `std::string_view`

`str` - a character string  

`length` - the length of a character string

`is` - an input stream  

`first`, `last` - pair of [LegacyInputIterators](https://en.cppreference.com/w/cpp/named_req/InputIterator) that specify a character sequence  

`options` - a [basic_json_options](../basic_json_options.md)  

`err_handler` - an error handler. Since 0.171.0, an error handler may be provided as a member of a [basic_json_options](../basic_json_options.md).  

### Examples

#### Parse from string

```cpp
try 
{
    json val = json::parse("[1,2,3,4,]");
} 
catch(const jsoncons::ser_error& e) 
{
    std::cout << e.what() << '\n';
}
```
Output:
```
Extra comma at line 1 and column 10
```

#### Parse from string with options

```cpp
std::string s = R"({"field1":"NaN","field2":"PositiveInfinity","field3":"NegativeInfinity"})";

auto options = json_options{}
    .nan_to_str("NaN")
    .inf_to_str("PositiveInfinity")
    .neginf_to_str("NegativeInfinity");

json j = json::parse(s,options);

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

std::cout << "\n(2)\n" << pretty_print(j,options) << '\n';
```
Output:
```
(1)
{
    "field1": null,
    "field2": null,
    "field3": null
}

(2)
{
    "field1": "NaN",
    "field2": "PositiveInfinity",
    "field3": "NegativeInfinity"
}
```

#### Parse from stream

Input JSON file `example.json`:

```json
{"File Format Options":{"Color Spaces":["sRGB","AdobeRGB","ProPhoto RGB"]}}
```

```cpp
std::ifstream is("example.json");
json j = json::parse(is);

std::cout << pretty_print(j) << '\n';
```

Output:

```json
{
    "File Format Options": {
        "Color Spaces": ["sRGB","AdobeRGB","ProPhoto RGB"]
    }
}
```

#### Parse from pair of input iterators

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

class MyIterator
{
    const char* p_;
public:
    using iterator_category = std::input_iterator_tag;
    using value_type = char;
    using difference_type = std::ptrdiff_t;
    using pointer = const char*; 
    using reference = const char&;

    MyIterator(const char* p)
        : p_(p)
    {
    }

    reference operator*() const
    {
        return *p_;
    }

    pointer operator->() const 
    {
        return p_;
    }

    MyIterator& operator++()
    {
        ++p_;
        return *this;
    }

    MyIterator operator++(int) 
    {
        MyIterator temp(*this);
        ++*this;
        return temp;
    }

    bool operator!=(const MyIterator& rhs) const
    {
        return p_ != rhs.p_;
    }
};

int main()
{
    char source[] = {'[','\"', 'f','o','o','\"',',','\"', 'b','a','r','\"',']'};

    MyIterator first(source);
    MyIterator last(source + sizeof(source));

    json j = json::parse(first, last);

    std::cout << j << "\n\n";
}
```

Output:
```json
["foo","bar"]
```

#### Parse a JSON text using a `std::pmr::polymorphic_allocator` allocator (since 0.171.0)

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

using namespace jsoncons; // for convenience

using pmr_json = jsoncons::pmr::json;

int main()
{
    char buffer[1024] = {}; // a small buffer on the stack
    std::pmr::monotonic_buffer_resource pool{std::data(buffer), std::size(buffer)};
    std::pmr::polymorphic_allocator<char> alloc(&pool);

    std::string json_text = R"(
    {
        "street_number" : "100",
        "street_name" : "Queen St W",
        "city" : "Toronto",
        "country" : "Canada"
    }
    )";

    try
    {
        auto doc = pmr_json::parse(combine_allocators(alloc), json_text);
        std::cout << pretty_print(doc) << "\n\n";
    }
    catch (const std::exception& ex)
    {
        std::cerr << ex.what() << '\n';
    }
}
```

Output:

```json
{
    "city": "Toronto",
    "country": "Canada",
    "street_name": "Queen St W",
    "street_number": "100"
}
```