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,351 @@
1
+ // Copyright 2018 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_CBOR_CBOR_CURSOR_HPP
8
+ #define JSONCONS_CBOR_CBOR_CURSOR_HPP
9
+
10
+ #include <memory> // std::allocator
11
+ #include <string>
12
+ #include <vector>
13
+ #include <stdexcept>
14
+ #include <system_error>
15
+ #include <ios>
16
+ #include <istream> // std::basic_istream
17
+ #include <jsoncons/byte_string.hpp>
18
+ #include <jsoncons/config/jsoncons_config.hpp>
19
+ #include <jsoncons/json_visitor.hpp>
20
+ #include <jsoncons/json_exception.hpp>
21
+ #include <jsoncons/staj_cursor.hpp>
22
+ #include <jsoncons/source.hpp>
23
+ #include <jsoncons_ext/cbor/cbor_parser.hpp>
24
+
25
+ namespace jsoncons {
26
+ namespace cbor {
27
+
28
+ template<class Source=jsoncons::binary_stream_source,class Allocator=std::allocator<char>>
29
+ class basic_cbor_cursor : public basic_staj_cursor<char>, private virtual ser_context
30
+ {
31
+ public:
32
+ using source_type = Source;
33
+ using char_type = char;
34
+ using allocator_type = Allocator;
35
+ private:
36
+ basic_cbor_parser<Source,Allocator> parser_;
37
+ basic_staj_visitor<char_type> cursor_visitor_;
38
+ basic_json_visitor2_to_visitor_adaptor<char_type,Allocator> cursor_handler_adaptor_;
39
+ bool eof_;
40
+
41
+ // Noncopyable and nonmoveable
42
+ basic_cbor_cursor(const basic_cbor_cursor&) = delete;
43
+ basic_cbor_cursor& operator=(const basic_cbor_cursor&) = delete;
44
+
45
+ public:
46
+ using string_view_type = string_view;
47
+
48
+ template <class Sourceable>
49
+ basic_cbor_cursor(Sourceable&& source,
50
+ const cbor_decode_options& options = cbor_decode_options(),
51
+ const Allocator& alloc = Allocator())
52
+ : parser_(std::forward<Sourceable>(source), options, alloc),
53
+ cursor_visitor_(accept_all),
54
+ cursor_handler_adaptor_(cursor_visitor_, alloc),
55
+ eof_(false)
56
+ {
57
+ if (!done())
58
+ {
59
+ next();
60
+ }
61
+ }
62
+
63
+ // Constructors that set parse error codes
64
+
65
+ template <class Sourceable>
66
+ basic_cbor_cursor(Sourceable&& source,
67
+ std::error_code& ec)
68
+ : basic_cbor_cursor(std::allocator_arg, Allocator(),
69
+ std::forward<Sourceable>(source),
70
+ cbor_decode_options(),
71
+ ec)
72
+ {
73
+ }
74
+
75
+ template <class Sourceable>
76
+ basic_cbor_cursor(Sourceable&& source,
77
+ const cbor_decode_options& options,
78
+ std::error_code& ec)
79
+ : basic_cbor_cursor(std::allocator_arg, Allocator(),
80
+ std::forward<Sourceable>(source),
81
+ options,
82
+ ec)
83
+ {
84
+ }
85
+
86
+ template <class Sourceable>
87
+ basic_cbor_cursor(std::allocator_arg_t, const Allocator& alloc,
88
+ Sourceable&& source,
89
+ const cbor_decode_options& options,
90
+ std::error_code& ec)
91
+ : parser_(std::forward<Sourceable>(source), options, alloc),
92
+ cursor_visitor_(accept_all),
93
+ cursor_handler_adaptor_(cursor_visitor_, alloc),
94
+ eof_(false)
95
+ {
96
+ if (!done())
97
+ {
98
+ next(ec);
99
+ }
100
+ }
101
+
102
+ void reset()
103
+ {
104
+ parser_.reset();
105
+ cursor_visitor_.reset();
106
+ cursor_handler_adaptor_.reset();
107
+ eof_ = false;
108
+ if (!done())
109
+ {
110
+ next();
111
+ }
112
+ }
113
+
114
+ template <class Sourceable>
115
+ void reset(Sourceable&& source)
116
+ {
117
+ parser_.reset(std::forward<Sourceable>(source));
118
+ cursor_visitor_.reset();
119
+ cursor_handler_adaptor_.reset();
120
+ eof_ = false;
121
+ if (!done())
122
+ {
123
+ next();
124
+ }
125
+ }
126
+
127
+ void reset(std::error_code& ec)
128
+ {
129
+ parser_.reset();
130
+ cursor_visitor_.reset();
131
+ cursor_handler_adaptor_.reset();
132
+ eof_ = false;
133
+ if (!done())
134
+ {
135
+ next(ec);
136
+ }
137
+ }
138
+
139
+ template <class Sourceable>
140
+ void reset(Sourceable&& source, std::error_code& ec)
141
+ {
142
+ parser_.reset(std::forward<Sourceable>(source));
143
+ cursor_visitor_.reset();
144
+ cursor_handler_adaptor_.reset();
145
+ eof_ = false;
146
+ if (!done())
147
+ {
148
+ next(ec);
149
+ }
150
+ }
151
+
152
+ bool done() const override
153
+ {
154
+ return parser_.done();
155
+ }
156
+
157
+ bool is_typed_array() const
158
+ {
159
+ return cursor_visitor_.is_typed_array();
160
+ }
161
+
162
+ const staj_event& current() const override
163
+ {
164
+ return cursor_visitor_.event();
165
+ }
166
+
167
+ void read_to(basic_json_visitor<char_type>& visitor) override
168
+ {
169
+ std::error_code ec;
170
+ read_to(visitor, ec);
171
+ if (ec)
172
+ {
173
+ JSONCONS_THROW(ser_error(ec,parser_.line(),parser_.column()));
174
+ }
175
+ }
176
+
177
+ void read_to(basic_json_visitor<char_type>& visitor,
178
+ std::error_code& ec) override
179
+ {
180
+ if (cursor_visitor_.dump(visitor, *this, ec))
181
+ {
182
+ read_next(visitor, ec);
183
+ }
184
+ }
185
+
186
+ void next() override
187
+ {
188
+ std::error_code ec;
189
+ next(ec);
190
+ if (ec)
191
+ {
192
+ JSONCONS_THROW(ser_error(ec,parser_.line(),parser_.column()));
193
+ }
194
+ }
195
+
196
+ void next(std::error_code& ec) override
197
+ {
198
+ read_next(ec);
199
+ }
200
+
201
+ const ser_context& context() const override
202
+ {
203
+ return *this;
204
+ }
205
+
206
+ bool eof() const
207
+ {
208
+ return eof_;
209
+ }
210
+
211
+ std::size_t line() const override
212
+ {
213
+ return parser_.line();
214
+ }
215
+
216
+ std::size_t column() const override
217
+ {
218
+ return parser_.column();
219
+ }
220
+
221
+ friend
222
+ staj_filter_view operator|(basic_cbor_cursor& cursor,
223
+ std::function<bool(const staj_event&, const ser_context&)> pred)
224
+ {
225
+ return staj_filter_view(cursor, pred);
226
+ }
227
+
228
+ #if !defined(JSONCONS_NO_DEPRECATED)
229
+
230
+ template <class Sourceable>
231
+ JSONCONS_DEPRECATED_MSG("Instead, use pipe syntax for filter")
232
+ basic_cbor_cursor(Sourceable&& source,
233
+ std::function<bool(const staj_event&, const ser_context&)> filter,
234
+ const cbor_decode_options& options = cbor_decode_options(),
235
+ const Allocator& alloc = Allocator())
236
+ : parser_(std::forward<Sourceable>(source), options, alloc),
237
+ cursor_visitor_(filter),
238
+ cursor_handler_adaptor_(cursor_visitor_, alloc),
239
+ eof_(false)
240
+ {
241
+ if (!done())
242
+ {
243
+ next();
244
+ }
245
+ }
246
+
247
+ template <class Sourceable>
248
+ JSONCONS_DEPRECATED_MSG("Instead, use pipe syntax for filter")
249
+ basic_cbor_cursor(Sourceable&& source,
250
+ std::function<bool(const staj_event&, const ser_context&)> filter,
251
+ std::error_code& ec)
252
+ : basic_cbor_cursor(std::allocator_arg, Allocator(),
253
+ std::forward<Sourceable>(source), filter, ec)
254
+ {
255
+ }
256
+
257
+ template <class Sourceable>
258
+ JSONCONS_DEPRECATED_MSG("Instead, use pipe syntax for filter")
259
+ basic_cbor_cursor(std::allocator_arg_t, const Allocator& alloc,
260
+ Sourceable&& source,
261
+ std::function<bool(const staj_event&, const ser_context&)> filter,
262
+ std::error_code& ec)
263
+ : parser_(std::forward<Sourceable>(source), alloc),
264
+ cursor_visitor_(filter),
265
+ cursor_handler_adaptor_(cursor_visitor_, alloc),
266
+ eof_(false)
267
+ {
268
+ if (!done())
269
+ {
270
+ next(ec);
271
+ }
272
+ }
273
+
274
+ JSONCONS_DEPRECATED_MSG("Instead, use read_to(basic_json_visitor<char_type>&)")
275
+ void read(basic_json_visitor<char_type>& visitor)
276
+ {
277
+ read_to(visitor);
278
+ }
279
+
280
+ JSONCONS_DEPRECATED_MSG("Instead, use read_to(basic_json_visitor<char_type>&, std::error_code&)")
281
+ void read(basic_json_visitor<char_type>& visitor,
282
+ std::error_code& ec)
283
+ {
284
+ read_to(visitor, ec);
285
+ }
286
+ #endif
287
+ private:
288
+ static bool accept_all(const staj_event&, const ser_context&)
289
+ {
290
+ return true;
291
+ }
292
+
293
+ void read_next(std::error_code& ec)
294
+ {
295
+ if (cursor_visitor_.in_available())
296
+ {
297
+ cursor_visitor_.send_available(ec);
298
+ }
299
+ else
300
+ {
301
+ parser_.restart();
302
+ while (!parser_.stopped())
303
+ {
304
+ parser_.parse(cursor_handler_adaptor_, ec);
305
+ if (ec) return;
306
+ }
307
+ }
308
+ }
309
+
310
+ void read_next(basic_json_visitor<char_type>& visitor, std::error_code& ec)
311
+ {
312
+ {
313
+ struct resource_wrapper
314
+ {
315
+ basic_json_visitor2_to_visitor_adaptor<char_type,Allocator>& adaptor;
316
+ basic_json_visitor<char_type>& original;
317
+
318
+ resource_wrapper(basic_json_visitor2_to_visitor_adaptor<char_type,Allocator>& adaptor,
319
+ basic_json_visitor<char_type>& visitor)
320
+ : adaptor(adaptor), original(adaptor.destination())
321
+ {
322
+ adaptor.destination(visitor);
323
+ }
324
+
325
+ ~resource_wrapper()
326
+ {
327
+ adaptor.destination(original);
328
+ }
329
+ } wrapper(cursor_handler_adaptor_, visitor);
330
+
331
+ parser_.restart();
332
+ while (!parser_.stopped())
333
+ {
334
+ parser_.parse(cursor_handler_adaptor_, ec);
335
+ if (ec)
336
+ {
337
+ return;
338
+ }
339
+ }
340
+ }
341
+ }
342
+ };
343
+
344
+ using cbor_stream_cursor = basic_cbor_cursor<jsoncons::binary_stream_source>;
345
+ using cbor_bytes_cursor = basic_cbor_cursor<jsoncons::bytes_source>;
346
+
347
+ } // namespace cbor
348
+ } // namespace jsoncons
349
+
350
+ #endif
351
+
@@ -0,0 +1,265 @@
1
+ // Copyright 2018 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_CBOR_CBOR_CURSOR2_HPP
8
+ #define JSONCONS_CBOR_CBOR_CURSOR2_HPP
9
+
10
+ #include <memory> // std::allocator
11
+ #include <string>
12
+ #include <vector>
13
+ #include <stdexcept>
14
+ #include <system_error>
15
+ #include <ios>
16
+ #include <istream> // std::basic_istream
17
+ #include <jsoncons/byte_string.hpp>
18
+ #include <jsoncons/config/jsoncons_config.hpp>
19
+ #include <jsoncons/json_visitor2.hpp>
20
+ #include <jsoncons/json_exception.hpp>
21
+ #include <jsoncons/staj2_cursor.hpp>
22
+ #include <jsoncons/source.hpp>
23
+ #include <jsoncons_ext/cbor/cbor_parser.hpp>
24
+
25
+ namespace jsoncons {
26
+ namespace cbor {
27
+
28
+ template<class Source=jsoncons::binary_stream_source,class Allocator=std::allocator<char>>
29
+ class basic_cbor_cursor2 : public basic_staj2_cursor<char>, private virtual ser_context
30
+ {
31
+ public:
32
+ using source_type = Source;
33
+ using char_type = char;
34
+ using allocator_type = Allocator;
35
+ private:
36
+ basic_cbor_parser<Source,Allocator> parser_;
37
+ basic_staj2_visitor<char_type> cursor_visitor_;
38
+ bool eof_;
39
+
40
+ // Noncopyable and nonmoveable
41
+ basic_cbor_cursor2(const basic_cbor_cursor2&) = delete;
42
+ basic_cbor_cursor2& operator=(const basic_cbor_cursor2&) = delete;
43
+
44
+ public:
45
+ using string_view_type = string_view;
46
+
47
+ template <class Sourceable>
48
+ basic_cbor_cursor2(Sourceable&& source,
49
+ const cbor_decode_options& options = cbor_decode_options(),
50
+ const Allocator& alloc = Allocator())
51
+ : parser_(std::forward<Sourceable>(source), options, alloc),
52
+ cursor_visitor_(accept_all),
53
+ eof_(false)
54
+ {
55
+ if (!done())
56
+ {
57
+ next();
58
+ }
59
+ }
60
+
61
+ // Constructors that set parse error codes
62
+
63
+ template <class Sourceable>
64
+ basic_cbor_cursor2(Sourceable&& source,
65
+ std::error_code& ec)
66
+ : basic_cbor_cursor2(std::allocator_arg, Allocator(),
67
+ std::forward<Sourceable>(source),
68
+ cbor_decode_options(),
69
+ ec)
70
+ {
71
+ }
72
+
73
+ template <class Sourceable>
74
+ basic_cbor_cursor2(Sourceable&& source,
75
+ const cbor_decode_options& options,
76
+ std::error_code& ec)
77
+ : basic_cbor_cursor2(std::allocator_arg, Allocator(),
78
+ std::forward<Sourceable>(source),
79
+ options,
80
+ ec)
81
+ {
82
+ }
83
+
84
+ template <class Sourceable>
85
+ basic_cbor_cursor2(std::allocator_arg_t, const Allocator& alloc,
86
+ Sourceable&& source,
87
+ const cbor_decode_options& options,
88
+ std::error_code& ec)
89
+ : parser_(std::forward<Sourceable>(source), options, alloc),
90
+ cursor_visitor_(accept_all),
91
+ eof_(false)
92
+ {
93
+ if (!done())
94
+ {
95
+ next(ec);
96
+ }
97
+ }
98
+
99
+ void reset()
100
+ {
101
+ parser_.reset();
102
+ cursor_visitor_.reset();
103
+ eof_ = false;
104
+ if (!done())
105
+ {
106
+ next();
107
+ }
108
+ }
109
+
110
+ template <class Sourceable>
111
+ void reset(Sourceable&& source)
112
+ {
113
+ parser_.reset(std::forward<Sourceable>(source));
114
+ cursor_visitor_.reset();
115
+ eof_ = false;
116
+ if (!done())
117
+ {
118
+ next();
119
+ }
120
+ }
121
+
122
+ void reset(std::error_code& ec)
123
+ {
124
+ parser_.reset();
125
+ cursor_visitor_.reset();
126
+ eof_ = false;
127
+ if (!done())
128
+ {
129
+ next(ec);
130
+ }
131
+ }
132
+
133
+ template <class Sourceable>
134
+ void reset(Sourceable&& source, std::error_code& ec)
135
+ {
136
+ parser_.reset(std::forward<Sourceable>(source));
137
+ cursor_visitor_.reset();
138
+ eof_ = false;
139
+ if (!done())
140
+ {
141
+ next(ec);
142
+ }
143
+ }
144
+
145
+ bool done() const override
146
+ {
147
+ return parser_.done();
148
+ }
149
+
150
+ bool is_typed_array() const
151
+ {
152
+ return cursor_visitor_.is_typed_array();
153
+ }
154
+
155
+ const staj2_event& current() const override
156
+ {
157
+ return cursor_visitor_.event();
158
+ }
159
+
160
+ void read_to(basic_json_visitor2<char_type>& visitor) override
161
+ {
162
+ std::error_code ec;
163
+ read_to(visitor, ec);
164
+ if (ec)
165
+ {
166
+ JSONCONS_THROW(ser_error(ec,parser_.line(),parser_.column()));
167
+ }
168
+ }
169
+
170
+ void read_to(basic_json_visitor2<char_type>& visitor,
171
+ std::error_code& ec) override
172
+ {
173
+ if (cursor_visitor_.dump(visitor, *this, ec))
174
+ {
175
+ read_next(visitor, ec);
176
+ }
177
+ }
178
+
179
+ void next() override
180
+ {
181
+ std::error_code ec;
182
+ next(ec);
183
+ if (ec)
184
+ {
185
+ JSONCONS_THROW(ser_error(ec,parser_.line(),parser_.column()));
186
+ }
187
+ }
188
+
189
+ void next(std::error_code& ec) override
190
+ {
191
+ read_next(ec);
192
+ }
193
+
194
+ const ser_context& context() const override
195
+ {
196
+ return *this;
197
+ }
198
+
199
+ bool eof() const
200
+ {
201
+ return eof_;
202
+ }
203
+
204
+ std::size_t line() const override
205
+ {
206
+ return parser_.line();
207
+ }
208
+
209
+ std::size_t column() const override
210
+ {
211
+ return parser_.column();
212
+ }
213
+
214
+ friend
215
+ staj2_filter_view operator|(basic_cbor_cursor2& cursor,
216
+ std::function<bool(const staj2_event&, const ser_context&)> pred)
217
+ {
218
+ return staj2_filter_view(cursor, pred);
219
+ }
220
+
221
+ private:
222
+ static bool accept_all(const staj2_event&, const ser_context&)
223
+ {
224
+ return true;
225
+ }
226
+
227
+ void read_next(std::error_code& ec)
228
+ {
229
+ if (cursor_visitor_.in_available())
230
+ {
231
+ cursor_visitor_.send_available(ec);
232
+ }
233
+ else
234
+ {
235
+ parser_.restart();
236
+ while (!parser_.stopped())
237
+ {
238
+ parser_.parse(cursor_visitor_, ec);
239
+ if (ec) return;
240
+ }
241
+ }
242
+ }
243
+
244
+ void read_next(basic_json_visitor2<char_type>& visitor, std::error_code& ec)
245
+ {
246
+ parser_.restart();
247
+ while (!parser_.stopped())
248
+ {
249
+ parser_.parse(visitor, ec);
250
+ if (ec)
251
+ {
252
+ return;
253
+ }
254
+ }
255
+ }
256
+ };
257
+
258
+ using cbor_stream_cursor2 = basic_cbor_cursor2<jsoncons::binary_stream_source>;
259
+ using cbor_bytes_cursor2 = basic_cbor_cursor2<jsoncons::bytes_source>;
260
+
261
+ } // namespace cbor
262
+ } // namespace jsoncons
263
+
264
+ #endif
265
+
@@ -0,0 +1,93 @@
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_CBOR_CBOR_DETAIL_HPP
8
+ #define JSONCONS_CBOR_CBOR_DETAIL_HPP
9
+
10
+ #include <string>
11
+ #include <vector>
12
+ #include <memory>
13
+ #include <iterator> // std::forward_iterator_tag
14
+ #include <limits> // std::numeric_limits
15
+ #include <utility> // std::move
16
+ #include <jsoncons/json.hpp>
17
+ #include <jsoncons/json_visitor.hpp>
18
+ #include <jsoncons/config/jsoncons_config.hpp>
19
+
20
+ // 0x00..0x17 (0..23)
21
+ #define JSONCONS_CBOR_0x00_0x17 \
22
+ 0x00:case 0x01:case 0x02:case 0x03:case 0x04:case 0x05:case 0x06:case 0x07:case 0x08:case 0x09:case 0x0a:case 0x0b:case 0x0c:case 0x0d:case 0x0e:case 0x0f:case 0x10:case 0x11:case 0x12:case 0x13:case 0x14:case 0x15:case 0x16:case 0x17
23
+
24
+ #define JSONCONS_CBOR_ARRAY_TAGS \
25
+ 0x40:case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:case 0x47:case 0x48:case 0x49:case 0x4a:case 0x4b:case 0x4c:case 0x4d:case 0x4e:case 0x4f:case 0x50:case 0x51:case 0x52:case 0x53:case 0x54:case 0x55:case 0x56:case 0x57
26
+
27
+ namespace jsoncons { namespace cbor { namespace detail {
28
+
29
+ //const uint8_t cbor_array_tags_010_mask = 0b11100000;
30
+ //const uint8_t cbor_array_tags_f_mask = 0b00010000;
31
+ //const uint8_t cbor_array_tags_s_mask = 0b00001000;
32
+ //const uint8_t cbor_array_tags_e_mask = 0b00000100;
33
+ //const uint8_t cbor_array_tags_ll_mask = 0b00000011;
34
+
35
+ const uint8_t cbor_array_tags_010_mask = 0xE0;
36
+ const uint8_t cbor_array_tags_f_mask = 0x10;
37
+ const uint8_t cbor_array_tags_s_mask = 0x08;
38
+ const uint8_t cbor_array_tags_e_mask = 0x04;
39
+ const uint8_t cbor_array_tags_ll_mask = 0x03;
40
+
41
+ const uint8_t cbor_array_tags_010_shift = 5;
42
+ const uint8_t cbor_array_tags_f_shift = 4;
43
+ const uint8_t cbor_array_tags_s_shift = 3;
44
+ const uint8_t cbor_array_tags_e_shift = 2;
45
+ const uint8_t cbor_array_tags_ll_shift = 0;
46
+
47
+ enum class cbor_major_type : uint8_t
48
+ {
49
+ unsigned_integer = 0x00,
50
+ negative_integer = 0x01,
51
+ byte_string = 0x02,
52
+ text_string = 0x03,
53
+ array = 0x04,
54
+ map = 0x05,
55
+ semantic_tag = 0x06,
56
+ simple = 0x7
57
+ };
58
+
59
+ namespace additional_info
60
+ {
61
+ const uint8_t indefinite_length = 0x1f;
62
+ }
63
+
64
+ inline
65
+ size_t min_length_for_stringref(uint64_t index)
66
+ {
67
+ std::size_t n;
68
+ if (index <= 23)
69
+ {
70
+ n = 3;
71
+ }
72
+ else if (index <= 255)
73
+ {
74
+ n = 4;
75
+ }
76
+ else if (index <= 65535)
77
+ {
78
+ n = 5;
79
+ }
80
+ else if (index <= 4294967295)
81
+ {
82
+ n = 7;
83
+ }
84
+ else
85
+ {
86
+ n = 11;
87
+ }
88
+ return n;
89
+ }
90
+
91
+ }}}
92
+
93
+ #endif