File: json_decoder.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 (186 lines) | stat: -rw-r--r-- 5,145 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
### jsoncons::json_decoder

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

template <typename Json,typename TempAllocator>
json_decoder
```

![json_decoder](./diagrams/json_decoder.png)

#### Member types

Member type                         |Definition
------------------------------------|------------------------------
`result_allocator_type`|Json::allocator_type (until 0.171.0)
`allocator_type`|Json::allocator_type (since 0.171.0)
`temp_allocator_type`|TempAllocator

#### Constructors

    json_decoder(result_allocator_arg_t, 
        const result_allocator_type& alloc);                            (1)   (until 0.171.0)

    json_decoder(result_allocator_arg_t, 
        const result_allocator_type& alloc, 
        const temp_allocator_type& temp_alloc);                         (1)   (until 0.171.0)

    json_decoder(const allocator_type& alloc = allocator_type(),         
        const temp_allocator_type& temp_alloc = temp_allocator_type()); (1)   (since 0.171.0)

    json_decoder(const temp_allocator_type& temp_alloc = 
        temp_allocator_type());                                         (2)   (until 0.171.0)

    json_decoder(temp_allocator_arg_t, 
        const temp_allocator_type& temp_alloc);                         (2)   (since 0.171.0)

#### Member functions

    allocator_type get_allocator() const
Returns the allocator associated with the json value.

    bool is_valid() const
Checks if the decoder contains a valid `basic_json` value. 
Initially `is_valid()` is false, becomes `true` when a `basic_json`
value has been received, and becomes false when `get_result()` is called.

    Json get_result()
Returns the json value `v` stored in the decoder as `std::move(v)`.
If before calling this function `is_valid()` is false, an `assertion_error` is thrown.
After `get_result()` is called, 'is_valid()' becomes false.
Once the result has been retrieved, `get_result` cannot be called again until
another `basic_json` value has been received.

### Examples

#### Decode a JSON text using stateful result and work allocators

Input JSON file `book_catalog.json`:

```json
[ 
  { 
      "author" : "Haruki Murakami",
      "title" : "Hard-Boiled Wonderland and the End of the World",
      "isbn" : "0679743464",
      "publisher" : "Vintage",
      "date" : "1993-03-02",
      "price": 18.90
  },
  { 
      "author" : "Graham Greene",
      "title" : "The Comedians",
      "isbn" : "0099478374",
      "publisher" : "Vintage Classics",
      "date" : "2005-09-21",
      "price": 15.74
  }
]
```

```cpp
int main()
{
    // Given allocator my_alloc with a single-argument constructor my_alloc(int),
    // use my_alloc(1) to allocate basic_json memory, my_alloc(2) to allocate
    // working memory used by json_decoder, and my_alloc(3) to allocate
    // working memory used by basic_json_reader. 

    using my_json = basic_json<char,sorted_policy,my_alloc>;

    std::ifstream is("book_catalog.json");
    json_decoder<my_json,my_alloc> decoder(my_alloc(1),my_alloc(2));
    basic_json_reader<char,stream_source<char>,my_alloc> reader(is, decoder, my_alloc(3));
    reader.read();

    my_json j = decoder.get_result();
    std::cout << pretty_print(j) << "\n";
}
```
Output:
```
[
    {
        "author": "Haruki Murakami",
        "date": "1993-03-02",
        "isbn": "0679743464",
        "price": 18.9,
        "publisher": "Vintage",
        "title": "Hard-Boiled Wonderland and the End of the World"
    },
    {
        "author": "Graham Greene",
        "date": "2005-09-21",
        "isbn": "0099478374",
        "price": 15.74,
        "publisher": "Vintage Classics",
        "title": "The Comedians"
    }
]
```

#### Decode a CSV text using stateful result and work allocators

Input CSV file `bond_yields.csv`:

```
Date,1Y,2Y,3Y,5Y
2017-01-09,0.0062,0.0075,0.0083,0.011
2017-01-08,0.0063,0.0076,0.0084,0.0112
2017-01-08,0.0063,0.0076,0.0084,0.0112
```

```cpp
int main()
{
    // Given allocator my_alloc with a single-argument constructor my_alloc(int),
    // use my_alloc(1) to allocate basic_json memory, my_alloc(2) to allocate
    // working memory used by json_decoder, and my_alloc(3) to allocate
    // working memory used by basic_csv_reader. 

    using my_json = basic_json<char,sorted_policy,my_alloc>;

    auto options = csv::csv_options{}
        .assume_header(true);

    std::ifstream is("bond_yields.csv");
    json_decoder<my_json,my_alloc> decoder(my_alloc(1),my_alloc(2));
    csv::basic_csv_reader<char,stream_source<char>,free_list_allocator<char>> reader(is, decoder, options, my_alloc(3));
    reader.read();

    my_json j = decoder.get_result();
    std::cout << pretty_print(j) << "\n";
}
```
Output:
```
[
    {
        "1Y": 0.0062,
        "2Y": 0.0075,
        "3Y": 0.0083,
        "5Y": 0.011,
        "Date": "2017-01-09"
    },
    {
        "1Y": 0.0063,
        "2Y": 0.0076,
        "3Y": 0.0084,
        "5Y": 0.0112,
        "Date": "2017-01-08"
    },
    {
        "1Y": 0.0063,
        "2Y": 0.0076,
        "3Y": 0.0084,
        "5Y": 0.0112,
        "Date": "2017-01-08"
    }
]
```

### See also

[basic_json_visitor](basic_json_visitor.md)