jsoncons 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/jsoncons/extconf.rb +43 -0
- data/ext/jsoncons/jsoncons.cpp +161 -0
- data/ext/jsoncons/jsoncons.h +10 -0
- data/jsoncons.gemspec +44 -0
- data/lib/jsoncons/jsoncons/examples/input/address-book.json +13 -0
- data/lib/jsoncons/jsoncons/examples/input/books.json +28 -0
- data/lib/jsoncons/jsoncons/examples/input/countries.json +7 -0
- data/lib/jsoncons/jsoncons/examples/input/employees.json +30 -0
- data/lib/jsoncons/jsoncons/examples/input/jsonschema/name.json +15 -0
- data/lib/jsoncons/jsoncons/examples/input/multiple-json-objects.json +3 -0
- data/lib/jsoncons/jsoncons/examples/input/sales.csv +6 -0
- data/lib/jsoncons/jsoncons/examples/input/store.json +28 -0
- data/lib/jsoncons/jsoncons/examples/input/tasks.csv +6 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/allocator_holder.hpp +38 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/basic_json.hpp +5905 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/bigint.hpp +1611 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/byte_string.hpp +820 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/config/binary_config.hpp +226 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/config/compiler_support.hpp +375 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/config/jsoncons_config.hpp +309 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/config/version.hpp +40 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/conv_error.hpp +218 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/decode_json.hpp +209 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/decode_traits.hpp +651 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/detail/endian.hpp +44 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/detail/grisu3.hpp +312 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/detail/optional.hpp +483 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/detail/parse_number.hpp +1133 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/detail/span.hpp +188 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/detail/string_view.hpp +537 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/detail/string_wrapper.hpp +370 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/detail/write_number.hpp +567 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/encode_json.hpp +315 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/encode_traits.hpp +378 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json.hpp +18 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_array.hpp +324 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_content_handler.hpp +12 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_cursor.hpp +448 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_decoder.hpp +420 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_encoder.hpp +1587 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_error.hpp +156 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_exception.hpp +241 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_filter.hpp +653 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_fwd.hpp +23 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_object.hpp +1772 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_options.hpp +862 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_parser.hpp +2900 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_reader.hpp +731 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_traits_macros.hpp +1072 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_traits_macros_deprecated.hpp +144 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_type.hpp +206 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_type_traits.hpp +1830 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_visitor.hpp +1560 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/json_visitor2.hpp +2079 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/pretty_print.hpp +89 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/ser_context.hpp +62 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/sink.hpp +289 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/source.hpp +777 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/source_adaptor.hpp +148 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/staj2_cursor.hpp +1189 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/staj_cursor.hpp +1254 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/staj_iterator.hpp +449 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/tag_type.hpp +245 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/text_source_adaptor.hpp +144 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/traits_extension.hpp +884 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/typed_array_view.hpp +250 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/unicode_traits.hpp +1330 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/uri.hpp +635 -0
- data/lib/jsoncons/jsoncons/include/jsoncons/value_converter.hpp +340 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson.hpp +23 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_cursor.hpp +320 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_decimal128.hpp +865 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_encoder.hpp +585 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_error.hpp +103 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_oid.hpp +245 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_options.hpp +75 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_parser.hpp +645 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_reader.hpp +92 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_type.hpp +44 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/decode_bson.hpp +201 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/encode_bson.hpp +144 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor.hpp +26 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_cursor.hpp +351 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_cursor2.hpp +265 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_detail.hpp +93 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_encoder.hpp +1766 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_error.hpp +105 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_options.hpp +113 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_parser.hpp +1942 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_reader.hpp +116 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/decode_cbor.hpp +203 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/encode_cbor.hpp +151 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv.hpp +17 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_cursor.hpp +358 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_encoder.hpp +954 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_error.hpp +85 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_options.hpp +973 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_parser.hpp +2099 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_reader.hpp +348 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_serializer.hpp +12 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/decode_csv.hpp +208 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/encode_csv.hpp +122 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jmespath/jmespath.hpp +5215 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jmespath/jmespath_error.hpp +215 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpatch/jsonpatch.hpp +579 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpatch/jsonpatch_error.hpp +121 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/expression.hpp +3329 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/flatten.hpp +432 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/json_location.hpp +445 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/json_query.hpp +115 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/jsonpath.hpp +13 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/jsonpath_error.hpp +240 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp +2612 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/jsonpath_selector.hpp +1322 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpointer/jsonpointer.hpp +1577 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpointer/jsonpointer_error.hpp +119 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/format_validator.hpp +968 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/json_validator.hpp +120 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/jsonschema.hpp +13 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/jsonschema_error.hpp +105 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/jsonschema_version.hpp +18 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/keyword_validator.hpp +1745 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/keyword_validator_factory.hpp +556 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/schema_draft7.hpp +198 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/schema_location.hpp +200 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/schema_version.hpp +35 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/subschema.hpp +144 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/mergepatch/mergepatch.hpp +103 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/decode_msgpack.hpp +202 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/encode_msgpack.hpp +142 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack.hpp +24 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_cursor.hpp +343 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_cursor2.hpp +259 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_encoder.hpp +753 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_error.hpp +94 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_options.hpp +74 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_parser.hpp +748 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_reader.hpp +116 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_type.hpp +63 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/decode_ubjson.hpp +201 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/encode_ubjson.hpp +142 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson.hpp +23 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_cursor.hpp +307 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_encoder.hpp +502 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_error.hpp +100 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_options.hpp +87 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_parser.hpp +880 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_reader.hpp +92 -0
- data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_type.hpp +43 -0
- data/lib/jsoncons/version.rb +5 -0
- data/lib/jsoncons.rb +33 -0
- data/test/jsoncons_test.rb +108 -0
- data/test/test_helper.rb +7 -0
- metadata +268 -0
@@ -0,0 +1,340 @@
|
|
1
|
+
// Copyright 2020 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_CONVERTER_HPP
|
8
|
+
#define JSONCONS_CONVERTER_HPP
|
9
|
+
|
10
|
+
#include <system_error> // std::error_code
|
11
|
+
#include <jsoncons/traits_extension.hpp>
|
12
|
+
#include <jsoncons/byte_string.hpp>
|
13
|
+
#include <jsoncons/json_type.hpp>
|
14
|
+
#include <jsoncons/conv_error.hpp>
|
15
|
+
#include <jsoncons/detail/write_number.hpp> // from_integer
|
16
|
+
#include <jsoncons/tag_type.hpp>
|
17
|
+
|
18
|
+
namespace jsoncons {
|
19
|
+
|
20
|
+
template <class From, class Into, class Enable = void>
|
21
|
+
class value_converter
|
22
|
+
{
|
23
|
+
};
|
24
|
+
|
25
|
+
template <class Into>
|
26
|
+
class value_converter_base
|
27
|
+
{
|
28
|
+
public:
|
29
|
+
using allocator_type = typename std::conditional<traits_extension::has_allocator_type<Into>::value, typename Into::allocator_type, std::allocator<char>>::type;
|
30
|
+
private:
|
31
|
+
allocator_type alloc_;
|
32
|
+
|
33
|
+
public:
|
34
|
+
value_converter_base(const allocator_type& alloc = allocator_type())
|
35
|
+
: alloc_(alloc)
|
36
|
+
{
|
37
|
+
}
|
38
|
+
|
39
|
+
allocator_type get_allocator() const noexcept
|
40
|
+
{
|
41
|
+
return alloc_;
|
42
|
+
}
|
43
|
+
};
|
44
|
+
|
45
|
+
// From any byte sequence, Into string
|
46
|
+
template <class From, class Into>
|
47
|
+
class value_converter<From, Into,
|
48
|
+
typename std::enable_if<traits_extension::is_byte_sequence<From>::value && !traits_extension::is_string_or_string_view<From>::value &&
|
49
|
+
traits_extension::is_string<Into>::value>::type> : value_converter_base<Into>
|
50
|
+
{
|
51
|
+
public:
|
52
|
+
using allocator_type = typename value_converter_base<Into>::allocator_type;
|
53
|
+
|
54
|
+
template <class CharT = typename Into::value_type>
|
55
|
+
typename std::enable_if<traits_extension::is_narrow_character<CharT>::value,Into>::type
|
56
|
+
convert(const From& value, semantic_tag tag, std::error_code&)
|
57
|
+
{
|
58
|
+
Into s(this->get_allocator());
|
59
|
+
switch (tag)
|
60
|
+
{
|
61
|
+
case semantic_tag::base64:
|
62
|
+
encode_base64(value.begin(), value.end(), s);
|
63
|
+
break;
|
64
|
+
case semantic_tag::base16:
|
65
|
+
encode_base16(value.begin(), value.end(), s);
|
66
|
+
break;
|
67
|
+
default:
|
68
|
+
encode_base64url(value.begin(), value.end(), s);
|
69
|
+
break;
|
70
|
+
}
|
71
|
+
return s;
|
72
|
+
}
|
73
|
+
template <class CharT = typename Into::value_type>
|
74
|
+
typename std::enable_if<traits_extension::is_wide_character<CharT>::value,Into>::type
|
75
|
+
convert(const From& value, semantic_tag tag, std::error_code& ec)
|
76
|
+
{
|
77
|
+
std::string s;
|
78
|
+
switch (tag)
|
79
|
+
{
|
80
|
+
case semantic_tag::base64:
|
81
|
+
encode_base64(value.begin(), value.end(), s);
|
82
|
+
break;
|
83
|
+
case semantic_tag::base16:
|
84
|
+
encode_base16(value.begin(), value.end(), s);
|
85
|
+
break;
|
86
|
+
default:
|
87
|
+
encode_base64url(value.begin(), value.end(), s);
|
88
|
+
break;
|
89
|
+
}
|
90
|
+
|
91
|
+
Into ws(this->get_allocator());
|
92
|
+
auto retval = unicode_traits::convert(s.data(), s.size(), ws);
|
93
|
+
if (retval.ec != unicode_traits::conv_errc())
|
94
|
+
{
|
95
|
+
ec = conv_errc::not_wide_char;
|
96
|
+
}
|
97
|
+
|
98
|
+
return ws;
|
99
|
+
}
|
100
|
+
};
|
101
|
+
|
102
|
+
// From byte string, Into byte string
|
103
|
+
template <class From, class Into>
|
104
|
+
class value_converter<From, Into,
|
105
|
+
typename std::enable_if<traits_extension::is_byte_sequence<From>::value &&
|
106
|
+
!traits_extension::is_string_or_string_view<From>::value &&
|
107
|
+
!traits_extension::is_string_or_string_view<Into>::value &&
|
108
|
+
traits_extension::is_back_insertable_byte_container<Into>::value>::type> : value_converter_base<Into>
|
109
|
+
{
|
110
|
+
public:
|
111
|
+
using allocator_type = typename value_converter_base<Into>::allocator_type;
|
112
|
+
|
113
|
+
Into convert(const From& value, semantic_tag, std::error_code&)
|
114
|
+
{
|
115
|
+
Into s(value.begin(),value.end(),this->get_allocator());
|
116
|
+
return s;
|
117
|
+
}
|
118
|
+
};
|
119
|
+
|
120
|
+
// From string or string_view, Into string, same character type
|
121
|
+
template <class From, class Into>
|
122
|
+
class value_converter<From, Into,
|
123
|
+
typename std::enable_if<traits_extension::is_string_or_string_view<From>::value &&
|
124
|
+
traits_extension::is_string<Into>::value &&
|
125
|
+
std::is_same<typename From::value_type,typename Into::value_type>::value>::type> : value_converter_base<Into>
|
126
|
+
{
|
127
|
+
public:
|
128
|
+
using allocator_type = typename value_converter_base<Into>::allocator_type;
|
129
|
+
|
130
|
+
Into convert(const From& value, semantic_tag, std::error_code&)
|
131
|
+
{
|
132
|
+
return Into(value.begin(),value.end(),this->get_allocator());
|
133
|
+
}
|
134
|
+
};
|
135
|
+
|
136
|
+
// From string or string_view, Into string, different character type
|
137
|
+
template <class From, class Into>
|
138
|
+
class value_converter<From, Into,
|
139
|
+
typename std::enable_if<traits_extension::is_string_or_string_view<From>::value &&
|
140
|
+
traits_extension::is_string<Into>::value &&
|
141
|
+
!std::is_same<typename From::value_type,typename Into::value_type>::value>::type> : value_converter_base<Into>
|
142
|
+
{
|
143
|
+
public:
|
144
|
+
using allocator_type = typename value_converter_base<Into>::allocator_type;
|
145
|
+
|
146
|
+
Into convert(const From& value, semantic_tag, std::error_code& ec)
|
147
|
+
{
|
148
|
+
Into ws(this->get_allocator());
|
149
|
+
auto retval = unicode_traits::convert(value.data(), value.size(), ws);
|
150
|
+
if (retval.ec != unicode_traits::conv_errc())
|
151
|
+
{
|
152
|
+
ec = conv_errc::not_wide_char;
|
153
|
+
}
|
154
|
+
|
155
|
+
return ws;
|
156
|
+
}
|
157
|
+
};
|
158
|
+
|
159
|
+
// From string, Into byte_string
|
160
|
+
template <class From, class Into>
|
161
|
+
class value_converter<From, Into,
|
162
|
+
typename std::enable_if<traits_extension::is_char_sequence<From>::value &&
|
163
|
+
!traits_extension::is_string_or_string_view<Into>::value &&
|
164
|
+
traits_extension::is_back_insertable_byte_container<Into>::value>::type> : value_converter_base<Into>
|
165
|
+
{
|
166
|
+
public:
|
167
|
+
using allocator_type = typename value_converter_base<Into>::allocator_type;
|
168
|
+
|
169
|
+
template <class CharT = typename From::value_type>
|
170
|
+
typename std::enable_if<traits_extension::is_narrow_character<CharT>::value,Into>::type
|
171
|
+
convert(const From& value, semantic_tag tag, std::error_code& ec)
|
172
|
+
{
|
173
|
+
Into bytes(this->get_allocator());
|
174
|
+
switch (tag)
|
175
|
+
{
|
176
|
+
case semantic_tag::base16:
|
177
|
+
{
|
178
|
+
auto res = decode_base16(value.begin(), value.end(), bytes);
|
179
|
+
if (res.ec != conv_errc::success)
|
180
|
+
{
|
181
|
+
ec = conv_errc::not_byte_string;
|
182
|
+
}
|
183
|
+
break;
|
184
|
+
}
|
185
|
+
case semantic_tag::base64:
|
186
|
+
{
|
187
|
+
decode_base64(value.begin(), value.end(), bytes);
|
188
|
+
break;
|
189
|
+
}
|
190
|
+
case semantic_tag::base64url:
|
191
|
+
{
|
192
|
+
decode_base64url(value.begin(), value.end(), bytes);
|
193
|
+
break;
|
194
|
+
}
|
195
|
+
default:
|
196
|
+
{
|
197
|
+
ec = conv_errc::not_byte_string;
|
198
|
+
break;
|
199
|
+
}
|
200
|
+
}
|
201
|
+
return bytes;
|
202
|
+
}
|
203
|
+
|
204
|
+
template <class CharT = typename From::value_type>
|
205
|
+
typename std::enable_if<traits_extension::is_wide_character<CharT>::value,Into>::type
|
206
|
+
convert(const From& value, semantic_tag tag, std::error_code& ec)
|
207
|
+
{
|
208
|
+
Into bytes(this->get_allocator());
|
209
|
+
|
210
|
+
std::string s(this->get_allocator());
|
211
|
+
auto retval = unicode_traits::convert(value.data(), value.size(), s);
|
212
|
+
if (retval.ec != unicode_traits::conv_errc())
|
213
|
+
{
|
214
|
+
ec = conv_errc::not_wide_char;
|
215
|
+
}
|
216
|
+
switch (tag)
|
217
|
+
{
|
218
|
+
case semantic_tag::base16:
|
219
|
+
{
|
220
|
+
auto res = decode_base16(s.begin(), s.end(), bytes);
|
221
|
+
if (res.ec != conv_errc::success)
|
222
|
+
{
|
223
|
+
ec = conv_errc::not_byte_string;
|
224
|
+
}
|
225
|
+
break;
|
226
|
+
}
|
227
|
+
case semantic_tag::base64:
|
228
|
+
{
|
229
|
+
decode_base64(s.begin(), s.end(), bytes);
|
230
|
+
break;
|
231
|
+
}
|
232
|
+
case semantic_tag::base64url:
|
233
|
+
{
|
234
|
+
decode_base64url(s.begin(), s.end(), bytes);
|
235
|
+
break;
|
236
|
+
}
|
237
|
+
default:
|
238
|
+
{
|
239
|
+
ec = conv_errc::not_byte_string;
|
240
|
+
break;
|
241
|
+
}
|
242
|
+
}
|
243
|
+
return bytes;
|
244
|
+
}
|
245
|
+
};
|
246
|
+
|
247
|
+
// From integer, Into string
|
248
|
+
template <class From, class Into>
|
249
|
+
class value_converter<From, Into,
|
250
|
+
typename std::enable_if<traits_extension::is_integer<From>::value &&
|
251
|
+
traits_extension::is_string<Into>::value>::type> : value_converter_base<Into>
|
252
|
+
{
|
253
|
+
public:
|
254
|
+
using allocator_type = typename value_converter_base<Into>::allocator_type;
|
255
|
+
|
256
|
+
Into convert(From value, semantic_tag, std::error_code&)
|
257
|
+
{
|
258
|
+
Into s(this->get_allocator());
|
259
|
+
jsoncons::detail::from_integer(value, s);
|
260
|
+
return s;
|
261
|
+
}
|
262
|
+
};
|
263
|
+
|
264
|
+
// From integer, Into string
|
265
|
+
template <class From, class Into>
|
266
|
+
class value_converter<From, Into,
|
267
|
+
typename std::enable_if<std::is_floating_point<From>::value &&
|
268
|
+
traits_extension::is_string<Into>::value>::type> : value_converter_base<Into>
|
269
|
+
{
|
270
|
+
public:
|
271
|
+
using allocator_type = typename value_converter_base<Into>::allocator_type;
|
272
|
+
|
273
|
+
Into convert(From value, semantic_tag, std::error_code&)
|
274
|
+
{
|
275
|
+
Into s(this->get_allocator());
|
276
|
+
jsoncons::detail::write_double f{float_chars_format::general,0};
|
277
|
+
f(value, s);
|
278
|
+
return s;
|
279
|
+
}
|
280
|
+
};
|
281
|
+
|
282
|
+
// From half, Into string
|
283
|
+
template <class Into>
|
284
|
+
class value_converter<half_arg_t, Into,
|
285
|
+
typename std::enable_if<traits_extension::is_string<Into>::value>::type> : value_converter_base<Into>
|
286
|
+
{
|
287
|
+
public:
|
288
|
+
using allocator_type = typename value_converter_base<Into>::allocator_type;
|
289
|
+
|
290
|
+
Into convert(uint16_t value, semantic_tag, std::error_code&)
|
291
|
+
{
|
292
|
+
Into s(this->get_allocator());
|
293
|
+
jsoncons::detail::write_double f{float_chars_format::general,0};
|
294
|
+
double x = binary::decode_half(value);
|
295
|
+
f(x, s);
|
296
|
+
return s;
|
297
|
+
}
|
298
|
+
};
|
299
|
+
|
300
|
+
// From bool, Into string
|
301
|
+
template <class From, class Into>
|
302
|
+
class value_converter<From, Into,
|
303
|
+
typename std::enable_if<traits_extension::is_bool<From>::value &&
|
304
|
+
traits_extension::is_string<Into>::value>::type> : value_converter_base<Into>
|
305
|
+
{
|
306
|
+
public:
|
307
|
+
using allocator_type = typename value_converter_base<Into>::allocator_type;
|
308
|
+
using char_type = typename Into::value_type;
|
309
|
+
|
310
|
+
JSONCONS_CPP14_CONSTEXPR
|
311
|
+
Into convert(From value, semantic_tag, std::error_code&)
|
312
|
+
{
|
313
|
+
constexpr const char_type* true_constant = JSONCONS_CSTRING_CONSTANT(char_type,"true");
|
314
|
+
constexpr const char_type* false_constant = JSONCONS_CSTRING_CONSTANT(char_type,"false");
|
315
|
+
|
316
|
+
return value ? Into(true_constant,4) : Into(false_constant,5);
|
317
|
+
}
|
318
|
+
};
|
319
|
+
|
320
|
+
// From null, Into string
|
321
|
+
template <class Into>
|
322
|
+
class value_converter<null_type, Into, void> : value_converter_base<Into>
|
323
|
+
{
|
324
|
+
public:
|
325
|
+
using allocator_type = typename value_converter_base<Into>::allocator_type;
|
326
|
+
using char_type = typename Into::value_type;
|
327
|
+
|
328
|
+
JSONCONS_CPP14_CONSTEXPR
|
329
|
+
Into convert(semantic_tag, std::error_code&)
|
330
|
+
{
|
331
|
+
constexpr const char_type* null_constant = JSONCONS_CSTRING_CONSTANT(char_type,"null");
|
332
|
+
|
333
|
+
return Into(null_constant,4);
|
334
|
+
}
|
335
|
+
};
|
336
|
+
|
337
|
+
} // namespace jsoncons
|
338
|
+
|
339
|
+
#endif
|
340
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
// Copyright 2013 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_BSON_BSON_HPP
|
8
|
+
#define JSONCONS_BSON_BSON_HPP
|
9
|
+
|
10
|
+
#include <string>
|
11
|
+
#include <vector>
|
12
|
+
#include <memory>
|
13
|
+
#include <type_traits> // std::enable_if
|
14
|
+
#include <istream> // std::basic_istream
|
15
|
+
#include <jsoncons/json.hpp>
|
16
|
+
#include <jsoncons/config/jsoncons_config.hpp>
|
17
|
+
#include <jsoncons_ext/bson/bson_encoder.hpp>
|
18
|
+
#include <jsoncons_ext/bson/bson_reader.hpp>
|
19
|
+
#include <jsoncons_ext/bson/bson_cursor.hpp>
|
20
|
+
#include <jsoncons_ext/bson/encode_bson.hpp>
|
21
|
+
#include <jsoncons_ext/bson/decode_bson.hpp>
|
22
|
+
|
23
|
+
#endif
|
@@ -0,0 +1,320 @@
|
|
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_BSON_BSON_CURSOR_HPP
|
8
|
+
#define JSONCONS_BSON_BSON_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/bson/bson_parser.hpp>
|
24
|
+
|
25
|
+
namespace jsoncons {
|
26
|
+
namespace bson {
|
27
|
+
|
28
|
+
template<class Source=jsoncons::binary_stream_source,class Allocator=std::allocator<char>>
|
29
|
+
class basic_bson_cursor : public basic_staj_cursor<char>, private virtual ser_context
|
30
|
+
{
|
31
|
+
using super_type = basic_staj_cursor<char>;
|
32
|
+
public:
|
33
|
+
using source_type = Source;
|
34
|
+
using char_type = char;
|
35
|
+
using allocator_type = Allocator;
|
36
|
+
private:
|
37
|
+
basic_bson_parser<Source,Allocator> parser_;
|
38
|
+
basic_staj_visitor<char_type> cursor_visitor_;
|
39
|
+
bool eof_;
|
40
|
+
|
41
|
+
// Noncopyable and nonmoveable
|
42
|
+
basic_bson_cursor(const basic_bson_cursor&) = delete;
|
43
|
+
basic_bson_cursor& operator=(const basic_bson_cursor&) = delete;
|
44
|
+
|
45
|
+
public:
|
46
|
+
using string_view_type = string_view;
|
47
|
+
|
48
|
+
template <class Sourceable>
|
49
|
+
basic_bson_cursor(Sourceable&& source,
|
50
|
+
const bson_decode_options& options = bson_decode_options(),
|
51
|
+
const Allocator& alloc = Allocator())
|
52
|
+
: parser_(std::forward<Sourceable>(source), options, alloc),
|
53
|
+
cursor_visitor_(accept_all),
|
54
|
+
eof_(false)
|
55
|
+
{
|
56
|
+
if (!done())
|
57
|
+
{
|
58
|
+
next();
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
// Constructors that set parse error codes
|
63
|
+
|
64
|
+
template <class Sourceable>
|
65
|
+
basic_bson_cursor(Sourceable&& source,
|
66
|
+
std::error_code& ec)
|
67
|
+
: basic_bson_cursor(std::allocator_arg, Allocator(),
|
68
|
+
std::forward<Sourceable>(source),
|
69
|
+
bson_decode_options(),
|
70
|
+
ec)
|
71
|
+
{
|
72
|
+
}
|
73
|
+
|
74
|
+
template <class Sourceable>
|
75
|
+
basic_bson_cursor(Sourceable&& source,
|
76
|
+
const bson_decode_options& options,
|
77
|
+
std::error_code& ec)
|
78
|
+
: basic_bson_cursor(std::allocator_arg, Allocator(),
|
79
|
+
std::forward<Sourceable>(source),
|
80
|
+
options,
|
81
|
+
ec)
|
82
|
+
{
|
83
|
+
}
|
84
|
+
|
85
|
+
template <class Sourceable>
|
86
|
+
basic_bson_cursor(std::allocator_arg_t, const Allocator& alloc,
|
87
|
+
Sourceable&& source,
|
88
|
+
const bson_decode_options& options,
|
89
|
+
std::error_code& ec)
|
90
|
+
: parser_(std::forward<Sourceable>(source), options, alloc),
|
91
|
+
cursor_visitor_(accept_all),
|
92
|
+
eof_(false)
|
93
|
+
{
|
94
|
+
if (!done())
|
95
|
+
{
|
96
|
+
next(ec);
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
void reset()
|
101
|
+
{
|
102
|
+
parser_.reset();
|
103
|
+
cursor_visitor_.reset();
|
104
|
+
eof_ = false;
|
105
|
+
if (!done())
|
106
|
+
{
|
107
|
+
next();
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
template <class Sourceable>
|
112
|
+
void reset(Sourceable&& source)
|
113
|
+
{
|
114
|
+
parser_.reset(std::forward<Sourceable>(source));
|
115
|
+
cursor_visitor_.reset();
|
116
|
+
eof_ = false;
|
117
|
+
if (!done())
|
118
|
+
{
|
119
|
+
next();
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
void reset(std::error_code& ec)
|
124
|
+
{
|
125
|
+
parser_.reset();
|
126
|
+
cursor_visitor_.reset();
|
127
|
+
eof_ = false;
|
128
|
+
if (!done())
|
129
|
+
{
|
130
|
+
next(ec);
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
template <class Sourceable>
|
135
|
+
void reset(Sourceable&& source, std::error_code& ec)
|
136
|
+
{
|
137
|
+
parser_.reset(std::forward<Sourceable>(source));
|
138
|
+
cursor_visitor_.reset();
|
139
|
+
eof_ = false;
|
140
|
+
if (!done())
|
141
|
+
{
|
142
|
+
next(ec);
|
143
|
+
}
|
144
|
+
}
|
145
|
+
|
146
|
+
bool done() const override
|
147
|
+
{
|
148
|
+
return parser_.done();
|
149
|
+
}
|
150
|
+
|
151
|
+
void array_expected(std::error_code& ec) override
|
152
|
+
{
|
153
|
+
if (cursor_visitor_.event().event_type() == staj_event_type::begin_object)
|
154
|
+
{
|
155
|
+
parser_.array_expected(cursor_visitor_, ec);
|
156
|
+
}
|
157
|
+
else
|
158
|
+
{
|
159
|
+
super_type::array_expected(ec);
|
160
|
+
}
|
161
|
+
}
|
162
|
+
|
163
|
+
const staj_event& current() const override
|
164
|
+
{
|
165
|
+
return cursor_visitor_.event();
|
166
|
+
}
|
167
|
+
|
168
|
+
void read_to(basic_json_visitor<char_type>& visitor) override
|
169
|
+
{
|
170
|
+
std::error_code ec;
|
171
|
+
read_to(visitor, ec);
|
172
|
+
if (ec)
|
173
|
+
{
|
174
|
+
JSONCONS_THROW(ser_error(ec,parser_.line(),parser_.column()));
|
175
|
+
}
|
176
|
+
}
|
177
|
+
|
178
|
+
void read_to(basic_json_visitor<char_type>& visitor,
|
179
|
+
std::error_code& ec) override
|
180
|
+
{
|
181
|
+
if (send_json_event(cursor_visitor_.event(), visitor, *this, ec))
|
182
|
+
{
|
183
|
+
read_next(visitor, ec);
|
184
|
+
}
|
185
|
+
}
|
186
|
+
|
187
|
+
void next() override
|
188
|
+
{
|
189
|
+
std::error_code ec;
|
190
|
+
next(ec);
|
191
|
+
if (ec)
|
192
|
+
{
|
193
|
+
JSONCONS_THROW(ser_error(ec,parser_.line(),parser_.column()));
|
194
|
+
}
|
195
|
+
}
|
196
|
+
|
197
|
+
void next(std::error_code& ec) override
|
198
|
+
{
|
199
|
+
read_next(ec);
|
200
|
+
}
|
201
|
+
|
202
|
+
const ser_context& context() const override
|
203
|
+
{
|
204
|
+
return *this;
|
205
|
+
}
|
206
|
+
|
207
|
+
bool eof() const
|
208
|
+
{
|
209
|
+
return eof_;
|
210
|
+
}
|
211
|
+
|
212
|
+
std::size_t line() const override
|
213
|
+
{
|
214
|
+
return parser_.line();
|
215
|
+
}
|
216
|
+
|
217
|
+
std::size_t column() const override
|
218
|
+
{
|
219
|
+
return parser_.column();
|
220
|
+
}
|
221
|
+
|
222
|
+
friend
|
223
|
+
staj_filter_view operator|(basic_bson_cursor& cursor,
|
224
|
+
std::function<bool(const staj_event&, const ser_context&)> pred)
|
225
|
+
{
|
226
|
+
return staj_filter_view(cursor, pred);
|
227
|
+
}
|
228
|
+
|
229
|
+
#if !defined(JSONCONS_NO_DEPRECATED)
|
230
|
+
|
231
|
+
template <class Sourceable>
|
232
|
+
JSONCONS_DEPRECATED_MSG("Instead, use pipe syntax for filter")
|
233
|
+
basic_bson_cursor(Sourceable&& source,
|
234
|
+
std::function<bool(const staj_event&, const ser_context&)> filter,
|
235
|
+
std::error_code& ec)
|
236
|
+
: basic_bson_cursor(std::allocator_arg, Allocator(),
|
237
|
+
std::forward<Sourceable>(source), filter, ec)
|
238
|
+
{
|
239
|
+
}
|
240
|
+
|
241
|
+
template <class Sourceable>
|
242
|
+
JSONCONS_DEPRECATED_MSG("Instead, use pipe syntax for filter")
|
243
|
+
basic_bson_cursor(Sourceable&& source,
|
244
|
+
std::function<bool(const staj_event&, const ser_context&)> filter,
|
245
|
+
const bson_decode_options& options = bson_decode_options(),
|
246
|
+
const Allocator& alloc = Allocator())
|
247
|
+
: parser_(std::forward<Sourceable>(source), options, alloc),
|
248
|
+
cursor_visitor_(filter),
|
249
|
+
eof_(false)
|
250
|
+
{
|
251
|
+
if (!done())
|
252
|
+
{
|
253
|
+
next();
|
254
|
+
}
|
255
|
+
}
|
256
|
+
|
257
|
+
template <class Sourceable>
|
258
|
+
JSONCONS_DEPRECATED_MSG("Instead, use pipe syntax for filter")
|
259
|
+
basic_bson_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
|
+
eof_(false)
|
266
|
+
{
|
267
|
+
if (!done())
|
268
|
+
{
|
269
|
+
next(ec);
|
270
|
+
}
|
271
|
+
}
|
272
|
+
|
273
|
+
JSONCONS_DEPRECATED_MSG("Instead, use read_to(basic_json_visitor<char_type>&)")
|
274
|
+
void read(basic_json_visitor<char_type>& visitor)
|
275
|
+
{
|
276
|
+
read_to(visitor);
|
277
|
+
}
|
278
|
+
|
279
|
+
JSONCONS_DEPRECATED_MSG("Instead, use read_to(basic_json_visitor<char_type>&, std::error_code&)")
|
280
|
+
void read(basic_json_visitor<char_type>& visitor,
|
281
|
+
std::error_code& ec)
|
282
|
+
{
|
283
|
+
read_to(visitor, ec);
|
284
|
+
}
|
285
|
+
#endif
|
286
|
+
private:
|
287
|
+
static bool accept_all(const staj_event&, const ser_context&)
|
288
|
+
{
|
289
|
+
return true;
|
290
|
+
}
|
291
|
+
|
292
|
+
void read_next(std::error_code& ec)
|
293
|
+
{
|
294
|
+
parser_.restart();
|
295
|
+
while (!parser_.stopped())
|
296
|
+
{
|
297
|
+
parser_.parse(cursor_visitor_, ec);
|
298
|
+
if (ec) return;
|
299
|
+
}
|
300
|
+
}
|
301
|
+
|
302
|
+
void read_next(basic_json_visitor<char_type>& visitor, std::error_code& ec)
|
303
|
+
{
|
304
|
+
parser_.restart();
|
305
|
+
while (!parser_.stopped())
|
306
|
+
{
|
307
|
+
parser_.parse(visitor, ec);
|
308
|
+
if (ec) return;
|
309
|
+
}
|
310
|
+
}
|
311
|
+
};
|
312
|
+
|
313
|
+
using bson_stream_cursor = basic_bson_cursor<jsoncons::binary_stream_source>;
|
314
|
+
using bson_bytes_cursor = basic_bson_cursor<jsoncons::bytes_source>;
|
315
|
+
|
316
|
+
} // namespace bson
|
317
|
+
} // namespace jsoncons
|
318
|
+
|
319
|
+
#endif
|
320
|
+
|