jsoncons 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. checksums.yaml +7 -0
  2. data/ext/jsoncons/extconf.rb +43 -0
  3. data/ext/jsoncons/jsoncons.cpp +161 -0
  4. data/ext/jsoncons/jsoncons.h +10 -0
  5. data/jsoncons.gemspec +44 -0
  6. data/lib/jsoncons/jsoncons/examples/input/address-book.json +13 -0
  7. data/lib/jsoncons/jsoncons/examples/input/books.json +28 -0
  8. data/lib/jsoncons/jsoncons/examples/input/countries.json +7 -0
  9. data/lib/jsoncons/jsoncons/examples/input/employees.json +30 -0
  10. data/lib/jsoncons/jsoncons/examples/input/jsonschema/name.json +15 -0
  11. data/lib/jsoncons/jsoncons/examples/input/multiple-json-objects.json +3 -0
  12. data/lib/jsoncons/jsoncons/examples/input/sales.csv +6 -0
  13. data/lib/jsoncons/jsoncons/examples/input/store.json +28 -0
  14. data/lib/jsoncons/jsoncons/examples/input/tasks.csv +6 -0
  15. data/lib/jsoncons/jsoncons/include/jsoncons/allocator_holder.hpp +38 -0
  16. data/lib/jsoncons/jsoncons/include/jsoncons/basic_json.hpp +5905 -0
  17. data/lib/jsoncons/jsoncons/include/jsoncons/bigint.hpp +1611 -0
  18. data/lib/jsoncons/jsoncons/include/jsoncons/byte_string.hpp +820 -0
  19. data/lib/jsoncons/jsoncons/include/jsoncons/config/binary_config.hpp +226 -0
  20. data/lib/jsoncons/jsoncons/include/jsoncons/config/compiler_support.hpp +375 -0
  21. data/lib/jsoncons/jsoncons/include/jsoncons/config/jsoncons_config.hpp +309 -0
  22. data/lib/jsoncons/jsoncons/include/jsoncons/config/version.hpp +40 -0
  23. data/lib/jsoncons/jsoncons/include/jsoncons/conv_error.hpp +218 -0
  24. data/lib/jsoncons/jsoncons/include/jsoncons/decode_json.hpp +209 -0
  25. data/lib/jsoncons/jsoncons/include/jsoncons/decode_traits.hpp +651 -0
  26. data/lib/jsoncons/jsoncons/include/jsoncons/detail/endian.hpp +44 -0
  27. data/lib/jsoncons/jsoncons/include/jsoncons/detail/grisu3.hpp +312 -0
  28. data/lib/jsoncons/jsoncons/include/jsoncons/detail/optional.hpp +483 -0
  29. data/lib/jsoncons/jsoncons/include/jsoncons/detail/parse_number.hpp +1133 -0
  30. data/lib/jsoncons/jsoncons/include/jsoncons/detail/span.hpp +188 -0
  31. data/lib/jsoncons/jsoncons/include/jsoncons/detail/string_view.hpp +537 -0
  32. data/lib/jsoncons/jsoncons/include/jsoncons/detail/string_wrapper.hpp +370 -0
  33. data/lib/jsoncons/jsoncons/include/jsoncons/detail/write_number.hpp +567 -0
  34. data/lib/jsoncons/jsoncons/include/jsoncons/encode_json.hpp +315 -0
  35. data/lib/jsoncons/jsoncons/include/jsoncons/encode_traits.hpp +378 -0
  36. data/lib/jsoncons/jsoncons/include/jsoncons/json.hpp +18 -0
  37. data/lib/jsoncons/jsoncons/include/jsoncons/json_array.hpp +324 -0
  38. data/lib/jsoncons/jsoncons/include/jsoncons/json_content_handler.hpp +12 -0
  39. data/lib/jsoncons/jsoncons/include/jsoncons/json_cursor.hpp +448 -0
  40. data/lib/jsoncons/jsoncons/include/jsoncons/json_decoder.hpp +420 -0
  41. data/lib/jsoncons/jsoncons/include/jsoncons/json_encoder.hpp +1587 -0
  42. data/lib/jsoncons/jsoncons/include/jsoncons/json_error.hpp +156 -0
  43. data/lib/jsoncons/jsoncons/include/jsoncons/json_exception.hpp +241 -0
  44. data/lib/jsoncons/jsoncons/include/jsoncons/json_filter.hpp +653 -0
  45. data/lib/jsoncons/jsoncons/include/jsoncons/json_fwd.hpp +23 -0
  46. data/lib/jsoncons/jsoncons/include/jsoncons/json_object.hpp +1772 -0
  47. data/lib/jsoncons/jsoncons/include/jsoncons/json_options.hpp +862 -0
  48. data/lib/jsoncons/jsoncons/include/jsoncons/json_parser.hpp +2900 -0
  49. data/lib/jsoncons/jsoncons/include/jsoncons/json_reader.hpp +731 -0
  50. data/lib/jsoncons/jsoncons/include/jsoncons/json_traits_macros.hpp +1072 -0
  51. data/lib/jsoncons/jsoncons/include/jsoncons/json_traits_macros_deprecated.hpp +144 -0
  52. data/lib/jsoncons/jsoncons/include/jsoncons/json_type.hpp +206 -0
  53. data/lib/jsoncons/jsoncons/include/jsoncons/json_type_traits.hpp +1830 -0
  54. data/lib/jsoncons/jsoncons/include/jsoncons/json_visitor.hpp +1560 -0
  55. data/lib/jsoncons/jsoncons/include/jsoncons/json_visitor2.hpp +2079 -0
  56. data/lib/jsoncons/jsoncons/include/jsoncons/pretty_print.hpp +89 -0
  57. data/lib/jsoncons/jsoncons/include/jsoncons/ser_context.hpp +62 -0
  58. data/lib/jsoncons/jsoncons/include/jsoncons/sink.hpp +289 -0
  59. data/lib/jsoncons/jsoncons/include/jsoncons/source.hpp +777 -0
  60. data/lib/jsoncons/jsoncons/include/jsoncons/source_adaptor.hpp +148 -0
  61. data/lib/jsoncons/jsoncons/include/jsoncons/staj2_cursor.hpp +1189 -0
  62. data/lib/jsoncons/jsoncons/include/jsoncons/staj_cursor.hpp +1254 -0
  63. data/lib/jsoncons/jsoncons/include/jsoncons/staj_iterator.hpp +449 -0
  64. data/lib/jsoncons/jsoncons/include/jsoncons/tag_type.hpp +245 -0
  65. data/lib/jsoncons/jsoncons/include/jsoncons/text_source_adaptor.hpp +144 -0
  66. data/lib/jsoncons/jsoncons/include/jsoncons/traits_extension.hpp +884 -0
  67. data/lib/jsoncons/jsoncons/include/jsoncons/typed_array_view.hpp +250 -0
  68. data/lib/jsoncons/jsoncons/include/jsoncons/unicode_traits.hpp +1330 -0
  69. data/lib/jsoncons/jsoncons/include/jsoncons/uri.hpp +635 -0
  70. data/lib/jsoncons/jsoncons/include/jsoncons/value_converter.hpp +340 -0
  71. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson.hpp +23 -0
  72. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_cursor.hpp +320 -0
  73. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_decimal128.hpp +865 -0
  74. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_encoder.hpp +585 -0
  75. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_error.hpp +103 -0
  76. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_oid.hpp +245 -0
  77. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_options.hpp +75 -0
  78. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_parser.hpp +645 -0
  79. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_reader.hpp +92 -0
  80. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_type.hpp +44 -0
  81. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/decode_bson.hpp +201 -0
  82. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/encode_bson.hpp +144 -0
  83. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor.hpp +26 -0
  84. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_cursor.hpp +351 -0
  85. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_cursor2.hpp +265 -0
  86. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_detail.hpp +93 -0
  87. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_encoder.hpp +1766 -0
  88. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_error.hpp +105 -0
  89. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_options.hpp +113 -0
  90. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_parser.hpp +1942 -0
  91. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_reader.hpp +116 -0
  92. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/decode_cbor.hpp +203 -0
  93. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/encode_cbor.hpp +151 -0
  94. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv.hpp +17 -0
  95. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_cursor.hpp +358 -0
  96. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_encoder.hpp +954 -0
  97. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_error.hpp +85 -0
  98. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_options.hpp +973 -0
  99. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_parser.hpp +2099 -0
  100. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_reader.hpp +348 -0
  101. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_serializer.hpp +12 -0
  102. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/decode_csv.hpp +208 -0
  103. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/encode_csv.hpp +122 -0
  104. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jmespath/jmespath.hpp +5215 -0
  105. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jmespath/jmespath_error.hpp +215 -0
  106. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpatch/jsonpatch.hpp +579 -0
  107. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpatch/jsonpatch_error.hpp +121 -0
  108. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/expression.hpp +3329 -0
  109. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/flatten.hpp +432 -0
  110. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/json_location.hpp +445 -0
  111. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/json_query.hpp +115 -0
  112. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/jsonpath.hpp +13 -0
  113. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/jsonpath_error.hpp +240 -0
  114. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp +2612 -0
  115. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/jsonpath_selector.hpp +1322 -0
  116. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpointer/jsonpointer.hpp +1577 -0
  117. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpointer/jsonpointer_error.hpp +119 -0
  118. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/format_validator.hpp +968 -0
  119. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/json_validator.hpp +120 -0
  120. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/jsonschema.hpp +13 -0
  121. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/jsonschema_error.hpp +105 -0
  122. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/jsonschema_version.hpp +18 -0
  123. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/keyword_validator.hpp +1745 -0
  124. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/keyword_validator_factory.hpp +556 -0
  125. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/schema_draft7.hpp +198 -0
  126. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/schema_location.hpp +200 -0
  127. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/schema_version.hpp +35 -0
  128. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/subschema.hpp +144 -0
  129. data/lib/jsoncons/jsoncons/include/jsoncons_ext/mergepatch/mergepatch.hpp +103 -0
  130. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/decode_msgpack.hpp +202 -0
  131. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/encode_msgpack.hpp +142 -0
  132. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack.hpp +24 -0
  133. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_cursor.hpp +343 -0
  134. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_cursor2.hpp +259 -0
  135. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_encoder.hpp +753 -0
  136. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_error.hpp +94 -0
  137. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_options.hpp +74 -0
  138. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_parser.hpp +748 -0
  139. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_reader.hpp +116 -0
  140. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_type.hpp +63 -0
  141. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/decode_ubjson.hpp +201 -0
  142. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/encode_ubjson.hpp +142 -0
  143. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson.hpp +23 -0
  144. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_cursor.hpp +307 -0
  145. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_encoder.hpp +502 -0
  146. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_error.hpp +100 -0
  147. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_options.hpp +87 -0
  148. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_parser.hpp +880 -0
  149. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_reader.hpp +92 -0
  150. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_type.hpp +43 -0
  151. data/lib/jsoncons/version.rb +5 -0
  152. data/lib/jsoncons.rb +33 -0
  153. data/test/jsoncons_test.rb +108 -0
  154. data/test/test_helper.rb +7 -0
  155. metadata +268 -0
@@ -0,0 +1,880 @@
1
+ // Copyright 2017 Daniel Parker
2
+ // Distributed under the Boost license, Version 1.0.
3
+ // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4
+
5
+ // See https://github.com/danielaparker/jsoncons for latest version
6
+
7
+ #ifndef JSONCONS_UBJSON_UBJSON_PARSER_HPP
8
+ #define JSONCONS_UBJSON_UBJSON_PARSER_HPP
9
+
10
+ #include <string>
11
+ #include <memory>
12
+ #include <utility> // std::move
13
+ #include <jsoncons/json.hpp>
14
+ #include <jsoncons/source.hpp>
15
+ #include <jsoncons/json_visitor.hpp>
16
+ #include <jsoncons/config/jsoncons_config.hpp>
17
+ #include <jsoncons_ext/ubjson/ubjson_type.hpp>
18
+ #include <jsoncons_ext/ubjson/ubjson_error.hpp>
19
+ #include <jsoncons_ext/ubjson/ubjson_options.hpp>
20
+
21
+ namespace jsoncons { namespace ubjson {
22
+
23
+ enum class parse_mode {root,accept,array,indefinite_array,strongly_typed_array,map_key,map_value,strongly_typed_map_key,strongly_typed_map_value,indefinite_map_key,indefinite_map_value};
24
+
25
+ struct parse_state
26
+ {
27
+ parse_mode mode;
28
+ std::size_t length;
29
+ uint8_t type;
30
+ std::size_t index;
31
+
32
+ parse_state(parse_mode mode, std::size_t length, uint8_t type = 0) noexcept
33
+ : mode(mode), length(length), type(type), index(0)
34
+ {
35
+ }
36
+
37
+ parse_state(const parse_state&) = default;
38
+ parse_state(parse_state&&) = default;
39
+ };
40
+
41
+ template <class Source,class Allocator=std::allocator<char>>
42
+ class basic_ubjson_parser : public ser_context
43
+ {
44
+ using char_type = char;
45
+ using char_traits_type = std::char_traits<char>;
46
+ using temp_allocator_type = Allocator;
47
+ using char_allocator_type = typename std::allocator_traits<temp_allocator_type>:: template rebind_alloc<char_type>;
48
+ using byte_allocator_type = typename std::allocator_traits<temp_allocator_type>:: template rebind_alloc<uint8_t>;
49
+ using parse_state_allocator_type = typename std::allocator_traits<temp_allocator_type>:: template rebind_alloc<parse_state>;
50
+
51
+ Source source_;
52
+ ubjson_decode_options options_;
53
+ bool more_;
54
+ bool done_;
55
+ std::basic_string<char,std::char_traits<char>,char_allocator_type> text_buffer_;
56
+ std::vector<parse_state,parse_state_allocator_type> state_stack_;
57
+ int nesting_depth_;
58
+ public:
59
+ template <class Sourceable>
60
+ basic_ubjson_parser(Sourceable&& source,
61
+ const ubjson_decode_options& options = ubjson_decode_options(),
62
+ const Allocator alloc = Allocator())
63
+ : source_(std::forward<Sourceable>(source)),
64
+ options_(options),
65
+ more_(true),
66
+ done_(false),
67
+ text_buffer_(alloc),
68
+ state_stack_(alloc),
69
+ nesting_depth_(0)
70
+ {
71
+ state_stack_.emplace_back(parse_mode::root,0);
72
+ }
73
+
74
+ void restart()
75
+ {
76
+ more_ = true;
77
+ }
78
+
79
+ void reset()
80
+ {
81
+ more_ = true;
82
+ done_ = false;
83
+ text_buffer_.clear();
84
+ state_stack_.clear();
85
+ state_stack_.emplace_back(parse_mode::root,0,uint8_t(0));
86
+ nesting_depth_ = 0;
87
+ }
88
+
89
+ template <class Sourceable>
90
+ void reset(Sourceable&& source)
91
+ {
92
+ source_ = std::forward<Sourceable>(source);
93
+ reset();
94
+ }
95
+
96
+ bool done() const
97
+ {
98
+ return done_;
99
+ }
100
+
101
+ bool stopped() const
102
+ {
103
+ return !more_;
104
+ }
105
+
106
+ std::size_t line() const override
107
+ {
108
+ return 0;
109
+ }
110
+
111
+ std::size_t column() const override
112
+ {
113
+ return source_.position();
114
+ }
115
+
116
+ void parse(json_visitor& visitor, std::error_code& ec)
117
+ {
118
+ while (!done_ && more_)
119
+ {
120
+ switch (state_stack_.back().mode)
121
+ {
122
+ case parse_mode::array:
123
+ {
124
+ if (state_stack_.back().index < state_stack_.back().length)
125
+ {
126
+ ++state_stack_.back().index;
127
+ read_type_and_value(visitor, ec);
128
+ if (ec)
129
+ {
130
+ return;
131
+ }
132
+ }
133
+ else
134
+ {
135
+ end_array(visitor, ec);
136
+ }
137
+ break;
138
+ }
139
+ case parse_mode::strongly_typed_array:
140
+ {
141
+ if (state_stack_.back().index < state_stack_.back().length)
142
+ {
143
+ ++state_stack_.back().index;
144
+ read_value(visitor, state_stack_.back().type, ec);
145
+ if (ec)
146
+ {
147
+ return;
148
+ }
149
+ }
150
+ else
151
+ {
152
+ end_array(visitor, ec);
153
+ }
154
+ break;
155
+ }
156
+ case parse_mode::indefinite_array:
157
+ {
158
+ auto c = source_.peek();
159
+ if (c.eof)
160
+ {
161
+ ec = ubjson_errc::unexpected_eof;
162
+ more_ = false;
163
+ return;
164
+ }
165
+ if (c.value == jsoncons::ubjson::ubjson_type::end_array_marker)
166
+ {
167
+ source_.ignore(1);
168
+ end_array(visitor, ec);
169
+ if (ec)
170
+ {
171
+ return;
172
+ }
173
+ }
174
+ else
175
+ {
176
+ if (++state_stack_.back().index > options_.max_items())
177
+ {
178
+ ec = ubjson_errc::max_items_exceeded;
179
+ more_ = false;
180
+ return;
181
+ }
182
+ read_type_and_value(visitor, ec);
183
+ if (ec)
184
+ {
185
+ return;
186
+ }
187
+ }
188
+ break;
189
+ }
190
+ case parse_mode::map_key:
191
+ {
192
+ if (state_stack_.back().index < state_stack_.back().length)
193
+ {
194
+ ++state_stack_.back().index;
195
+ read_key(visitor, ec);
196
+ if (ec)
197
+ {
198
+ return;
199
+ }
200
+ state_stack_.back().mode = parse_mode::map_value;
201
+ }
202
+ else
203
+ {
204
+ end_object(visitor, ec);
205
+ }
206
+ break;
207
+ }
208
+ case parse_mode::map_value:
209
+ {
210
+ state_stack_.back().mode = parse_mode::map_key;
211
+ read_type_and_value(visitor, ec);
212
+ if (ec)
213
+ {
214
+ return;
215
+ }
216
+ break;
217
+ }
218
+ case parse_mode::strongly_typed_map_key:
219
+ {
220
+ if (state_stack_.back().index < state_stack_.back().length)
221
+ {
222
+ ++state_stack_.back().index;
223
+ read_key(visitor, ec);
224
+ if (ec)
225
+ {
226
+ return;
227
+ }
228
+ state_stack_.back().mode = parse_mode::strongly_typed_map_value;
229
+ }
230
+ else
231
+ {
232
+ end_object(visitor, ec);
233
+ }
234
+ break;
235
+ }
236
+ case parse_mode::strongly_typed_map_value:
237
+ {
238
+ state_stack_.back().mode = parse_mode::strongly_typed_map_key;
239
+ read_value(visitor, state_stack_.back().type, ec);
240
+ if (ec)
241
+ {
242
+ return;
243
+ }
244
+ break;
245
+ }
246
+ case parse_mode::indefinite_map_key:
247
+ {
248
+ auto c = source_.peek();
249
+ if (c.eof)
250
+ {
251
+ ec = ubjson_errc::unexpected_eof;
252
+ more_ = false;
253
+ return;
254
+ }
255
+ if (c.value == jsoncons::ubjson::ubjson_type::end_object_marker)
256
+ {
257
+ source_.ignore(1);
258
+ end_object(visitor, ec);
259
+ if (ec)
260
+ {
261
+ return;
262
+ }
263
+ }
264
+ else
265
+ {
266
+ if (++state_stack_.back().index > options_.max_items())
267
+ {
268
+ ec = ubjson_errc::max_items_exceeded;
269
+ more_ = false;
270
+ return;
271
+ }
272
+ read_key(visitor, ec);
273
+ if (ec)
274
+ {
275
+ return;
276
+ }
277
+ state_stack_.back().mode = parse_mode::indefinite_map_value;
278
+ }
279
+ break;
280
+ }
281
+ case parse_mode::indefinite_map_value:
282
+ {
283
+ state_stack_.back().mode = parse_mode::indefinite_map_key;
284
+ read_type_and_value(visitor, ec);
285
+ if (ec)
286
+ {
287
+ return;
288
+ }
289
+ break;
290
+ }
291
+ case parse_mode::root:
292
+ {
293
+ state_stack_.back().mode = parse_mode::accept;
294
+ read_type_and_value(visitor, ec);
295
+ if (ec)
296
+ {
297
+ return;
298
+ }
299
+ break;
300
+ }
301
+ case parse_mode::accept:
302
+ {
303
+ JSONCONS_ASSERT(state_stack_.size() == 1);
304
+ state_stack_.clear();
305
+ more_ = false;
306
+ done_ = true;
307
+ visitor.flush();
308
+ break;
309
+ }
310
+ }
311
+ }
312
+ }
313
+ private:
314
+ void read_type_and_value(json_visitor& visitor, std::error_code& ec)
315
+ {
316
+ if (source_.is_error())
317
+ {
318
+ ec = ubjson_errc::source_error;
319
+ more_ = false;
320
+ return;
321
+ }
322
+
323
+ uint8_t b;
324
+ if (source_.read(&b, 1) == 0)
325
+ {
326
+ ec = ubjson_errc::unexpected_eof;
327
+ more_ = false;
328
+ return;
329
+ }
330
+ read_value(visitor, b, ec);
331
+ }
332
+
333
+ void read_value(json_visitor& visitor, uint8_t type, std::error_code& ec)
334
+ {
335
+ switch (type)
336
+ {
337
+ case jsoncons::ubjson::ubjson_type::null_type:
338
+ {
339
+ more_ = visitor.null_value(semantic_tag::none, *this, ec);
340
+ break;
341
+ }
342
+ case jsoncons::ubjson::ubjson_type::no_op_type:
343
+ {
344
+ break;
345
+ }
346
+ case jsoncons::ubjson::ubjson_type::true_type:
347
+ {
348
+ more_ = visitor.bool_value(true, semantic_tag::none, *this, ec);
349
+ break;
350
+ }
351
+ case jsoncons::ubjson::ubjson_type::false_type:
352
+ {
353
+ more_ = visitor.bool_value(false, semantic_tag::none, *this, ec);
354
+ break;
355
+ }
356
+ case jsoncons::ubjson::ubjson_type::int8_type:
357
+ {
358
+ uint8_t buf[sizeof(int8_t)];
359
+ if (source_.read(buf, sizeof(int8_t)) != sizeof(int8_t))
360
+ {
361
+ ec = ubjson_errc::unexpected_eof;
362
+ more_ = false;
363
+ return;
364
+ }
365
+ int8_t val = binary::big_to_native<int8_t>(buf, sizeof(buf));
366
+ more_ = visitor.int64_value(val, semantic_tag::none, *this, ec);
367
+ break;
368
+ }
369
+ case jsoncons::ubjson::ubjson_type::uint8_type:
370
+ {
371
+ uint8_t b;
372
+ if (source_.read(&b, 1) == 0)
373
+ {
374
+ ec = ubjson_errc::unexpected_eof;
375
+ more_ = false;
376
+ return;
377
+ }
378
+ more_ = visitor.uint64_value(b, semantic_tag::none, *this, ec);
379
+ break;
380
+ }
381
+ case jsoncons::ubjson::ubjson_type::int16_type:
382
+ {
383
+ uint8_t buf[sizeof(int16_t)];
384
+ if (source_.read(buf, sizeof(int16_t)) != sizeof(int16_t))
385
+ {
386
+ ec = ubjson_errc::unexpected_eof;
387
+ more_ = false;
388
+ return;
389
+ }
390
+ int16_t val = binary::big_to_native<int16_t>(buf, sizeof(buf));
391
+ more_ = visitor.int64_value(val, semantic_tag::none, *this, ec);
392
+ break;
393
+ }
394
+ case jsoncons::ubjson::ubjson_type::int32_type:
395
+ {
396
+ uint8_t buf[sizeof(int32_t)];
397
+ if (source_.read(buf, sizeof(int32_t)) != sizeof(int32_t))
398
+ {
399
+ ec = ubjson_errc::unexpected_eof;
400
+ more_ = false;
401
+ return;
402
+ }
403
+ int32_t val = binary::big_to_native<int32_t>(buf, sizeof(buf));
404
+ more_ = visitor.int64_value(val, semantic_tag::none, *this, ec);
405
+ break;
406
+ }
407
+ case jsoncons::ubjson::ubjson_type::int64_type:
408
+ {
409
+ uint8_t buf[sizeof(int64_t)];
410
+ if (source_.read(buf, sizeof(int64_t)) != sizeof(int64_t))
411
+ {
412
+ ec = ubjson_errc::unexpected_eof;
413
+ more_ = false;
414
+ return;
415
+ }
416
+ int64_t val = binary::big_to_native<int64_t>(buf, sizeof(buf));
417
+ more_ = visitor.int64_value(val, semantic_tag::none, *this, ec);
418
+ break;
419
+ }
420
+ case jsoncons::ubjson::ubjson_type::float32_type:
421
+ {
422
+ uint8_t buf[sizeof(float)];
423
+ if (source_.read(buf, sizeof(float)) != sizeof(float))
424
+ {
425
+ ec = ubjson_errc::unexpected_eof;
426
+ more_ = false;
427
+ return;
428
+ }
429
+ float val = binary::big_to_native<float>(buf, sizeof(buf));
430
+ more_ = visitor.double_value(val, semantic_tag::none, *this, ec);
431
+ break;
432
+ }
433
+ case jsoncons::ubjson::ubjson_type::float64_type:
434
+ {
435
+ uint8_t buf[sizeof(double)];
436
+ if (source_.read(buf, sizeof(double)) != sizeof(double))
437
+ {
438
+ ec = ubjson_errc::unexpected_eof;
439
+ more_ = false;
440
+ return;
441
+ }
442
+ double val = binary::big_to_native<double>(buf, sizeof(buf));
443
+ more_ = visitor.double_value(val, semantic_tag::none, *this, ec);
444
+ break;
445
+ }
446
+ case jsoncons::ubjson::ubjson_type::char_type:
447
+ {
448
+ text_buffer_.clear();
449
+ if (source_reader<Source>::read(source_,text_buffer_,1) != 1)
450
+ {
451
+ ec = ubjson_errc::unexpected_eof;
452
+ more_ = false;
453
+ return;
454
+ }
455
+ auto result = unicode_traits::validate(text_buffer_.data(),text_buffer_.size());
456
+ if (result.ec != unicode_traits::conv_errc())
457
+ {
458
+ ec = ubjson_errc::invalid_utf8_text_string;
459
+ more_ = false;
460
+ return;
461
+ }
462
+ more_ = visitor.string_value(text_buffer_, semantic_tag::none, *this, ec);
463
+ break;
464
+ }
465
+ case jsoncons::ubjson::ubjson_type::string_type:
466
+ {
467
+ std::size_t length = get_length(ec);
468
+ if (ec)
469
+ {
470
+ return;
471
+ }
472
+ text_buffer_.clear();
473
+ if (source_reader<Source>::read(source_,text_buffer_,length) != length)
474
+ {
475
+ ec = ubjson_errc::unexpected_eof;
476
+ more_ = false;
477
+ return;
478
+ }
479
+ auto result = unicode_traits::validate(text_buffer_.data(),text_buffer_.size());
480
+ if (result.ec != unicode_traits::conv_errc())
481
+ {
482
+ ec = ubjson_errc::invalid_utf8_text_string;
483
+ more_ = false;
484
+ return;
485
+ }
486
+ more_ = visitor.string_value(jsoncons::basic_string_view<char>(text_buffer_.data(),text_buffer_.length()), semantic_tag::none, *this, ec);
487
+ break;
488
+ }
489
+ case jsoncons::ubjson::ubjson_type::high_precision_number_type:
490
+ {
491
+ std::size_t length = get_length(ec);
492
+ if (ec)
493
+ {
494
+ return;
495
+ }
496
+ text_buffer_.clear();
497
+ if (source_reader<Source>::read(source_,text_buffer_,length) != length)
498
+ {
499
+ ec = ubjson_errc::unexpected_eof;
500
+ more_ = false;
501
+ return;
502
+ }
503
+ if (jsoncons::detail::is_base10(text_buffer_.data(),text_buffer_.length()))
504
+ {
505
+ more_ = visitor.string_value(jsoncons::basic_string_view<char>(text_buffer_.data(),text_buffer_.length()), semantic_tag::bigint, *this, ec);
506
+ }
507
+ else
508
+ {
509
+ more_ = visitor.string_value(jsoncons::basic_string_view<char>(text_buffer_.data(),text_buffer_.length()), semantic_tag::bigdec, *this, ec);
510
+ }
511
+ break;
512
+ }
513
+ case jsoncons::ubjson::ubjson_type::start_array_marker:
514
+ {
515
+ begin_array(visitor,ec);
516
+ break;
517
+ }
518
+ case jsoncons::ubjson::ubjson_type::start_object_marker:
519
+ {
520
+ begin_object(visitor, ec);
521
+ break;
522
+ }
523
+ default:
524
+ {
525
+ ec = ubjson_errc::unknown_type;
526
+ break;
527
+ }
528
+ }
529
+ if (ec)
530
+ {
531
+ more_ = false;
532
+ }
533
+ }
534
+
535
+ void begin_array(json_visitor& visitor, std::error_code& ec)
536
+ {
537
+ if (JSONCONS_UNLIKELY(++nesting_depth_ > options_.max_nesting_depth()))
538
+ {
539
+ ec = ubjson_errc::max_nesting_depth_exceeded;
540
+ more_ = false;
541
+ return;
542
+ }
543
+
544
+ auto c = source_.peek();
545
+ if (c.eof)
546
+ {
547
+ ec = ubjson_errc::unexpected_eof;
548
+ more_ = false;
549
+ return;
550
+ }
551
+ if (c.value == jsoncons::ubjson::ubjson_type::type_marker)
552
+ {
553
+ source_.ignore(1);
554
+ uint8_t b;
555
+ if (source_.read(&b, 1) == 0)
556
+ {
557
+ ec = ubjson_errc::unexpected_eof;
558
+ more_ = false;
559
+ return;
560
+ }
561
+ c = source_.peek();
562
+ if (c.eof)
563
+ {
564
+ ec = ubjson_errc::unexpected_eof;
565
+ more_ = false;
566
+ return;
567
+ }
568
+ if (c.value == jsoncons::ubjson::ubjson_type::count_marker)
569
+ {
570
+ source_.ignore(1);
571
+ std::size_t length = get_length(ec);
572
+ if (ec)
573
+ {
574
+ return;
575
+ }
576
+ if (length > options_.max_items())
577
+ {
578
+ ec = ubjson_errc::max_items_exceeded;
579
+ more_ = false;
580
+ return;
581
+ }
582
+ state_stack_.emplace_back(parse_mode::strongly_typed_array,length,b);
583
+ more_ = visitor.begin_array(length, semantic_tag::none, *this, ec);
584
+ }
585
+ else
586
+ {
587
+ ec = ubjson_errc::count_required_after_type;
588
+ more_ = false;
589
+ return;
590
+ }
591
+ }
592
+ else if (c.value == jsoncons::ubjson::ubjson_type::count_marker)
593
+ {
594
+ source_.ignore(1);
595
+ std::size_t length = get_length(ec);
596
+ if (ec)
597
+ {
598
+ return;
599
+ }
600
+ if (length > options_.max_items())
601
+ {
602
+ ec = ubjson_errc::max_items_exceeded;
603
+ more_ = false;
604
+ return;
605
+ }
606
+ state_stack_.emplace_back(parse_mode::array,length);
607
+ more_ = visitor.begin_array(length, semantic_tag::none, *this, ec);
608
+ }
609
+ else
610
+ {
611
+ state_stack_.emplace_back(parse_mode::indefinite_array,0);
612
+ more_ = visitor.begin_array(semantic_tag::none, *this, ec);
613
+ }
614
+ }
615
+
616
+ void end_array(json_visitor& visitor, std::error_code& ec)
617
+ {
618
+ --nesting_depth_;
619
+
620
+ more_ = visitor.end_array(*this, ec);
621
+ state_stack_.pop_back();
622
+ }
623
+
624
+ void begin_object(json_visitor& visitor, std::error_code& ec)
625
+ {
626
+ if (JSONCONS_UNLIKELY(++nesting_depth_ > options_.max_nesting_depth()))
627
+ {
628
+ ec = ubjson_errc::max_nesting_depth_exceeded;
629
+ more_ = false;
630
+ return;
631
+ }
632
+
633
+ auto c = source_.peek();
634
+ if (c.eof)
635
+ {
636
+ ec = ubjson_errc::unexpected_eof;
637
+ more_ = false;
638
+ return;
639
+ }
640
+ if (c.value == jsoncons::ubjson::ubjson_type::type_marker)
641
+ {
642
+ source_.ignore(1);
643
+ uint8_t b;
644
+ if (source_.read(&b, 1) == 0)
645
+ {
646
+ ec = ubjson_errc::unexpected_eof;
647
+ more_ = false;
648
+ return;
649
+ }
650
+ c = source_.peek();
651
+ if (c.eof)
652
+ {
653
+ ec = ubjson_errc::unexpected_eof;
654
+ more_ = false;
655
+ return;
656
+ }
657
+ if (c.value == jsoncons::ubjson::ubjson_type::count_marker)
658
+ {
659
+ source_.ignore(1);
660
+ std::size_t length = get_length(ec);
661
+ if (ec)
662
+ {
663
+ return;
664
+ }
665
+ if (length > options_.max_items())
666
+ {
667
+ ec = ubjson_errc::max_items_exceeded;
668
+ more_ = false;
669
+ return;
670
+ }
671
+ state_stack_.emplace_back(parse_mode::strongly_typed_map_key,length,b);
672
+ more_ = visitor.begin_object(length, semantic_tag::none, *this, ec);
673
+ }
674
+ else
675
+ {
676
+ ec = ubjson_errc::count_required_after_type;
677
+ more_ = false;
678
+ return;
679
+ }
680
+ }
681
+ else
682
+ {
683
+ c = source_.peek();
684
+ if (c.eof)
685
+ {
686
+ ec = ubjson_errc::unexpected_eof;
687
+ more_ = false;
688
+ return;
689
+ }
690
+ if (c.value == jsoncons::ubjson::ubjson_type::count_marker)
691
+ {
692
+ source_.ignore(1);
693
+ std::size_t length = get_length(ec);
694
+ if (ec)
695
+ {
696
+ return;
697
+ }
698
+ if (length > options_.max_items())
699
+ {
700
+ ec = ubjson_errc::max_items_exceeded;
701
+ more_ = false;
702
+ return;
703
+ }
704
+ state_stack_.emplace_back(parse_mode::map_key,length);
705
+ more_ = visitor.begin_object(length, semantic_tag::none, *this, ec);
706
+ }
707
+ else
708
+ {
709
+ state_stack_.emplace_back(parse_mode::indefinite_map_key,0);
710
+ more_ = visitor.begin_object(semantic_tag::none, *this, ec);
711
+ }
712
+ }
713
+ }
714
+
715
+ void end_object(json_visitor& visitor, std::error_code& ec)
716
+ {
717
+ --nesting_depth_;
718
+ more_ = visitor.end_object(*this, ec);
719
+ state_stack_.pop_back();
720
+ }
721
+
722
+ std::size_t get_length(std::error_code& ec)
723
+ {
724
+ std::size_t length = 0;
725
+ uint8_t type;
726
+ if (source_.read(&type, 1) == 0)
727
+ {
728
+ ec = ubjson_errc::unexpected_eof;
729
+ more_ = false;
730
+ return length;
731
+ }
732
+ switch (type)
733
+ {
734
+ case jsoncons::ubjson::ubjson_type::int8_type:
735
+ {
736
+ uint8_t buf[sizeof(int8_t)];
737
+ if (source_.read(buf, sizeof(int8_t)) != sizeof(int8_t))
738
+ {
739
+ ec = ubjson_errc::unexpected_eof;
740
+ more_ = false;
741
+ return length;
742
+ }
743
+ int8_t val = binary::big_to_native<int8_t>(buf, sizeof(buf));
744
+ if (val >= 0)
745
+ {
746
+ length = val;
747
+ }
748
+ else
749
+ {
750
+ ec = ubjson_errc::length_is_negative;
751
+ more_ = false;
752
+ return length;
753
+ }
754
+ break;
755
+ }
756
+ case jsoncons::ubjson::ubjson_type::uint8_type:
757
+ {
758
+ uint8_t b;
759
+ if (source_.read(&b, 1) == 0)
760
+ {
761
+ ec = ubjson_errc::unexpected_eof;
762
+ more_ = false;
763
+ return length;
764
+ }
765
+ length = b;
766
+ break;
767
+ }
768
+ case jsoncons::ubjson::ubjson_type::int16_type:
769
+ {
770
+ uint8_t buf[sizeof(int16_t)];
771
+ if (source_.read(buf, sizeof(int16_t)) != sizeof(int16_t))
772
+ {
773
+ ec = ubjson_errc::unexpected_eof;
774
+ more_ = false;
775
+ return length;
776
+ }
777
+ int16_t val = binary::big_to_native<int16_t>(buf, sizeof(buf));
778
+ if (val >= 0)
779
+ {
780
+ length = val;
781
+ }
782
+ else
783
+ {
784
+ ec = ubjson_errc::length_is_negative;
785
+ more_ = false;
786
+ return length;
787
+ }
788
+ break;
789
+ }
790
+ case jsoncons::ubjson::ubjson_type::int32_type:
791
+ {
792
+ uint8_t buf[sizeof(int32_t)];
793
+ if (source_.read(buf, sizeof(int32_t)) != sizeof(int32_t))
794
+ {
795
+ ec = ubjson_errc::unexpected_eof;
796
+ more_ = false;
797
+ return length;
798
+ }
799
+ int32_t val = binary::big_to_native<int32_t>(buf, sizeof(buf));
800
+ if (val >= 0)
801
+ {
802
+ length = static_cast<std::size_t>(val);
803
+ }
804
+ else
805
+ {
806
+ ec = ubjson_errc::length_is_negative;
807
+ more_ = false;
808
+ return length;
809
+ }
810
+ break;
811
+ }
812
+ case jsoncons::ubjson::ubjson_type::int64_type:
813
+ {
814
+ uint8_t buf[sizeof(int64_t)];
815
+ if (source_.read(buf, sizeof(int64_t)) != sizeof(int64_t))
816
+ {
817
+ ec = ubjson_errc::unexpected_eof;
818
+ more_ = false;
819
+ return length;
820
+ }
821
+ int64_t val = binary::big_to_native<int64_t>(buf, sizeof(buf));
822
+ if (val >= 0)
823
+ {
824
+ length = (std::size_t)val;
825
+ if (length != (uint64_t)val)
826
+ {
827
+ ec = ubjson_errc::number_too_large;
828
+ more_ = false;
829
+ return length;
830
+ }
831
+ }
832
+ else
833
+ {
834
+ ec = ubjson_errc::length_is_negative;
835
+ more_ = false;
836
+ return length;
837
+ }
838
+ break;
839
+ }
840
+ default:
841
+ {
842
+ ec = ubjson_errc::length_must_be_integer;
843
+ more_ = false;
844
+ return length;
845
+ }
846
+ }
847
+ return length;
848
+ }
849
+
850
+ void read_key(json_visitor& visitor, std::error_code& ec)
851
+ {
852
+ std::size_t length = get_length(ec);
853
+ if (ec)
854
+ {
855
+ ec = ubjson_errc::key_expected;
856
+ more_ = false;
857
+ return;
858
+ }
859
+ text_buffer_.clear();
860
+ if (source_reader<Source>::read(source_,text_buffer_,length) != length)
861
+ {
862
+ ec = ubjson_errc::unexpected_eof;
863
+ more_ = false;
864
+ return;
865
+ }
866
+
867
+ auto result = unicode_traits::validate(text_buffer_.data(),text_buffer_.size());
868
+ if (result.ec != unicode_traits::conv_errc())
869
+ {
870
+ ec = ubjson_errc::invalid_utf8_text_string;
871
+ more_ = false;
872
+ return;
873
+ }
874
+ more_ = visitor.key(jsoncons::basic_string_view<char>(text_buffer_.data(),text_buffer_.length()), *this, ec);
875
+ }
876
+ };
877
+
878
+ }}
879
+
880
+ #endif