jsoncons 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,348 @@
|
|
|
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_CSV_CSV_READER_HPP
|
|
8
|
+
#define JSONCONS_CSV_CSV_READER_HPP
|
|
9
|
+
|
|
10
|
+
#include <string>
|
|
11
|
+
#include <vector>
|
|
12
|
+
#include <stdexcept>
|
|
13
|
+
#include <memory> // std::allocator
|
|
14
|
+
#include <utility> // std::move
|
|
15
|
+
#include <istream> // std::basic_istream
|
|
16
|
+
#include <jsoncons/source.hpp>
|
|
17
|
+
#include <jsoncons/json_exception.hpp>
|
|
18
|
+
#include <jsoncons/json_visitor.hpp>
|
|
19
|
+
#include <jsoncons_ext/csv/csv_error.hpp>
|
|
20
|
+
#include <jsoncons_ext/csv/csv_parser.hpp>
|
|
21
|
+
#include <jsoncons/json.hpp>
|
|
22
|
+
#include <jsoncons/json_reader.hpp>
|
|
23
|
+
#include <jsoncons/json_decoder.hpp>
|
|
24
|
+
#include <jsoncons/source_adaptor.hpp>
|
|
25
|
+
#include <jsoncons_ext/csv/csv_options.hpp>
|
|
26
|
+
|
|
27
|
+
namespace jsoncons { namespace csv {
|
|
28
|
+
|
|
29
|
+
template<class CharT,class Source=jsoncons::stream_source<CharT>,class Allocator=std::allocator<char>>
|
|
30
|
+
class basic_csv_reader
|
|
31
|
+
{
|
|
32
|
+
struct stack_item
|
|
33
|
+
{
|
|
34
|
+
stack_item() noexcept
|
|
35
|
+
: array_begun_(false)
|
|
36
|
+
{
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
bool array_begun_;
|
|
40
|
+
};
|
|
41
|
+
using char_type = CharT;
|
|
42
|
+
using temp_allocator_type = Allocator;
|
|
43
|
+
typedef typename std::allocator_traits<temp_allocator_type>:: template rebind_alloc<CharT> char_allocator_type;
|
|
44
|
+
|
|
45
|
+
basic_csv_reader(const basic_csv_reader&) = delete;
|
|
46
|
+
basic_csv_reader& operator = (const basic_csv_reader&) = delete;
|
|
47
|
+
|
|
48
|
+
basic_default_json_visitor<CharT> default_visitor_;
|
|
49
|
+
text_source_adaptor<Source> source_;
|
|
50
|
+
basic_json_visitor<CharT>& visitor_;
|
|
51
|
+
basic_csv_parser<CharT,Allocator> parser_;
|
|
52
|
+
|
|
53
|
+
public:
|
|
54
|
+
// Structural characters
|
|
55
|
+
static constexpr size_t default_max_buffer_size = 16384;
|
|
56
|
+
//! Parse an input stream of CSV text into a json object
|
|
57
|
+
/*!
|
|
58
|
+
\param is The input stream to read from
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
template <class Sourceable>
|
|
62
|
+
basic_csv_reader(Sourceable&& source,
|
|
63
|
+
basic_json_visitor<CharT>& visitor,
|
|
64
|
+
const Allocator& alloc = Allocator())
|
|
65
|
+
|
|
66
|
+
: basic_csv_reader(std::forward<Sourceable>(source),
|
|
67
|
+
visitor,
|
|
68
|
+
basic_csv_decode_options<CharT>(),
|
|
69
|
+
default_csv_parsing(),
|
|
70
|
+
alloc)
|
|
71
|
+
{
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
template <class Sourceable>
|
|
75
|
+
basic_csv_reader(Sourceable&& source,
|
|
76
|
+
basic_json_visitor<CharT>& visitor,
|
|
77
|
+
const basic_csv_decode_options<CharT>& options,
|
|
78
|
+
const Allocator& alloc = Allocator())
|
|
79
|
+
|
|
80
|
+
: basic_csv_reader(std::forward<Sourceable>(source),
|
|
81
|
+
visitor,
|
|
82
|
+
options,
|
|
83
|
+
default_csv_parsing(),
|
|
84
|
+
alloc)
|
|
85
|
+
{
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
template <class Sourceable>
|
|
89
|
+
basic_csv_reader(Sourceable&& source,
|
|
90
|
+
basic_json_visitor<CharT>& visitor,
|
|
91
|
+
std::function<bool(csv_errc,const ser_context&)> err_handler,
|
|
92
|
+
const Allocator& alloc = Allocator())
|
|
93
|
+
: basic_csv_reader(std::forward<Sourceable>(source),
|
|
94
|
+
visitor,
|
|
95
|
+
basic_csv_decode_options<CharT>(),
|
|
96
|
+
err_handler,
|
|
97
|
+
alloc)
|
|
98
|
+
{
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
template <class Sourceable>
|
|
102
|
+
basic_csv_reader(Sourceable&& source,
|
|
103
|
+
basic_json_visitor<CharT>& visitor,
|
|
104
|
+
const basic_csv_decode_options<CharT>& options,
|
|
105
|
+
std::function<bool(csv_errc,const ser_context&)> err_handler,
|
|
106
|
+
const Allocator& alloc = Allocator())
|
|
107
|
+
: source_(std::forward<Sourceable>(source)),
|
|
108
|
+
visitor_(visitor),
|
|
109
|
+
parser_(options, err_handler, alloc)
|
|
110
|
+
|
|
111
|
+
{
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
~basic_csv_reader() noexcept = default;
|
|
115
|
+
|
|
116
|
+
void read()
|
|
117
|
+
{
|
|
118
|
+
std::error_code ec;
|
|
119
|
+
read(ec);
|
|
120
|
+
if (ec)
|
|
121
|
+
{
|
|
122
|
+
JSONCONS_THROW(ser_error(ec,parser_.line(),parser_.column()));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
void read(std::error_code& ec)
|
|
127
|
+
{
|
|
128
|
+
read_internal(ec);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
std::size_t line() const
|
|
132
|
+
{
|
|
133
|
+
return parser_.line();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
std::size_t column() const
|
|
137
|
+
{
|
|
138
|
+
return parser_.column();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
bool eof() const
|
|
142
|
+
{
|
|
143
|
+
return parser_.source_exhausted() && source_.eof();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private:
|
|
147
|
+
|
|
148
|
+
void read_internal(std::error_code& ec)
|
|
149
|
+
{
|
|
150
|
+
if (source_.is_error())
|
|
151
|
+
{
|
|
152
|
+
ec = csv_errc::source_error;
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
while (!parser_.stopped())
|
|
156
|
+
{
|
|
157
|
+
if (parser_.source_exhausted())
|
|
158
|
+
{
|
|
159
|
+
auto s = source_.read_buffer(ec);
|
|
160
|
+
if (ec) return;
|
|
161
|
+
if (s.size() > 0)
|
|
162
|
+
{
|
|
163
|
+
parser_.update(s.data(),s.size());
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
parser_.parse_some(visitor_, ec);
|
|
167
|
+
if (ec) return;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
template<class CharT,class Source=jsoncons::stream_source<CharT>,class Allocator=std::allocator<char>>
|
|
173
|
+
class legacy_basic_csv_reader
|
|
174
|
+
{
|
|
175
|
+
struct stack_item
|
|
176
|
+
{
|
|
177
|
+
stack_item() noexcept
|
|
178
|
+
: array_begun_(false)
|
|
179
|
+
{
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
bool array_begun_;
|
|
183
|
+
};
|
|
184
|
+
using char_type = CharT;
|
|
185
|
+
using temp_allocator_type = Allocator;
|
|
186
|
+
typedef typename std::allocator_traits<temp_allocator_type>:: template rebind_alloc<CharT> char_allocator_type;
|
|
187
|
+
|
|
188
|
+
legacy_basic_csv_reader(const legacy_basic_csv_reader&) = delete;
|
|
189
|
+
legacy_basic_csv_reader& operator = (const legacy_basic_csv_reader&) = delete;
|
|
190
|
+
|
|
191
|
+
basic_default_json_visitor<CharT> default_visitor_;
|
|
192
|
+
text_source_adaptor<Source> source_;
|
|
193
|
+
basic_json_visitor<CharT>& visitor_;
|
|
194
|
+
basic_csv_parser<CharT,Allocator> parser_;
|
|
195
|
+
|
|
196
|
+
public:
|
|
197
|
+
// Structural characters
|
|
198
|
+
static constexpr size_t default_max_buffer_size = 16384;
|
|
199
|
+
//! Parse an input stream of CSV text into a json object
|
|
200
|
+
/*!
|
|
201
|
+
\param is The input stream to read from
|
|
202
|
+
*/
|
|
203
|
+
|
|
204
|
+
template <class Sourceable>
|
|
205
|
+
legacy_basic_csv_reader(Sourceable&& source,
|
|
206
|
+
basic_json_visitor<CharT>& visitor,
|
|
207
|
+
const Allocator& alloc = Allocator())
|
|
208
|
+
|
|
209
|
+
: legacy_basic_csv_reader(std::forward<Sourceable>(source),
|
|
210
|
+
visitor,
|
|
211
|
+
basic_csv_decode_options<CharT>(),
|
|
212
|
+
default_csv_parsing(),
|
|
213
|
+
alloc)
|
|
214
|
+
{
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
template <class Sourceable>
|
|
218
|
+
legacy_basic_csv_reader(Sourceable&& source,
|
|
219
|
+
basic_json_visitor<CharT>& visitor,
|
|
220
|
+
const basic_csv_decode_options<CharT>& options,
|
|
221
|
+
const Allocator& alloc = Allocator())
|
|
222
|
+
|
|
223
|
+
: legacy_basic_csv_reader(std::forward<Sourceable>(source),
|
|
224
|
+
visitor,
|
|
225
|
+
options,
|
|
226
|
+
default_csv_parsing(),
|
|
227
|
+
alloc)
|
|
228
|
+
{
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
template <class Sourceable>
|
|
232
|
+
legacy_basic_csv_reader(Sourceable&& source,
|
|
233
|
+
basic_json_visitor<CharT>& visitor,
|
|
234
|
+
std::function<bool(csv_errc,const ser_context&)> err_handler,
|
|
235
|
+
const Allocator& alloc = Allocator())
|
|
236
|
+
: legacy_basic_csv_reader(std::forward<Sourceable>(source),
|
|
237
|
+
visitor,
|
|
238
|
+
basic_csv_decode_options<CharT>(),
|
|
239
|
+
err_handler,
|
|
240
|
+
alloc)
|
|
241
|
+
{
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
template <class Sourceable>
|
|
245
|
+
legacy_basic_csv_reader(Sourceable&& source,
|
|
246
|
+
basic_json_visitor<CharT>& visitor,
|
|
247
|
+
const basic_csv_decode_options<CharT>& options,
|
|
248
|
+
std::function<bool(csv_errc,const ser_context&)> err_handler,
|
|
249
|
+
const Allocator& alloc = Allocator(),
|
|
250
|
+
typename std::enable_if<!std::is_constructible<jsoncons::basic_string_view<CharT>,Sourceable>::value>::type* = 0)
|
|
251
|
+
: source_(std::forward<Sourceable>(source)),
|
|
252
|
+
visitor_(visitor),
|
|
253
|
+
parser_(options, err_handler, alloc)
|
|
254
|
+
{
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
template <class Sourceable>
|
|
258
|
+
legacy_basic_csv_reader(Sourceable&& source,
|
|
259
|
+
basic_json_visitor<CharT>& visitor,
|
|
260
|
+
const basic_csv_decode_options<CharT>& options,
|
|
261
|
+
std::function<bool(csv_errc,const ser_context&)> err_handler,
|
|
262
|
+
const Allocator& alloc = Allocator(),
|
|
263
|
+
typename std::enable_if<std::is_constructible<jsoncons::basic_string_view<CharT>,Sourceable>::value>::type* = 0)
|
|
264
|
+
: source_(),
|
|
265
|
+
visitor_(visitor),
|
|
266
|
+
parser_(options, err_handler, alloc)
|
|
267
|
+
{
|
|
268
|
+
jsoncons::basic_string_view<CharT> sv(std::forward<Sourceable>(source));
|
|
269
|
+
auto r = unicode_traits::detect_encoding_from_bom(sv.data(), sv.size());
|
|
270
|
+
if (!(r.encoding == unicode_traits::encoding_kind::utf8 || r.encoding == unicode_traits::encoding_kind::undetected))
|
|
271
|
+
{
|
|
272
|
+
JSONCONS_THROW(ser_error(json_errc::illegal_unicode_character,parser_.line(),parser_.column()));
|
|
273
|
+
}
|
|
274
|
+
std::size_t offset = (r.ptr - sv.data());
|
|
275
|
+
parser_.update(sv.data()+offset,sv.size()-offset);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
~legacy_basic_csv_reader() noexcept = default;
|
|
279
|
+
|
|
280
|
+
void read()
|
|
281
|
+
{
|
|
282
|
+
std::error_code ec;
|
|
283
|
+
read(ec);
|
|
284
|
+
if (ec)
|
|
285
|
+
{
|
|
286
|
+
JSONCONS_THROW(ser_error(ec,parser_.line(),parser_.column()));
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
void read(std::error_code& ec)
|
|
291
|
+
{
|
|
292
|
+
read_internal(ec);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
std::size_t line() const
|
|
296
|
+
{
|
|
297
|
+
return parser_.line();
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
std::size_t column() const
|
|
301
|
+
{
|
|
302
|
+
return parser_.column();
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
bool eof() const
|
|
306
|
+
{
|
|
307
|
+
return parser_.source_exhausted() && source_.eof();
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
private:
|
|
311
|
+
|
|
312
|
+
void read_internal(std::error_code& ec)
|
|
313
|
+
{
|
|
314
|
+
if (source_.is_error())
|
|
315
|
+
{
|
|
316
|
+
ec = csv_errc::source_error;
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
while (!parser_.stopped())
|
|
320
|
+
{
|
|
321
|
+
if (parser_.source_exhausted())
|
|
322
|
+
{
|
|
323
|
+
auto s = source_.read_buffer(ec);
|
|
324
|
+
if (ec) return;
|
|
325
|
+
if (s.size() > 0)
|
|
326
|
+
{
|
|
327
|
+
parser_.update(s.data(),s.size());
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
parser_.parse_some(visitor_, ec);
|
|
331
|
+
if (ec) return;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
#if !defined(JSONCONS_NO_DEPRECATED)
|
|
337
|
+
using csv_reader = legacy_basic_csv_reader<char>;
|
|
338
|
+
using wcsv_reader = legacy_basic_csv_reader<wchar_t>;
|
|
339
|
+
#endif
|
|
340
|
+
|
|
341
|
+
using csv_string_reader = basic_csv_reader<char,string_source<char>>;
|
|
342
|
+
using wcsv_string_reader = basic_csv_reader<wchar_t,string_source<wchar_t>>;
|
|
343
|
+
using csv_stream_reader = basic_csv_reader<char,stream_source<char>>;
|
|
344
|
+
using wcsv_stream_reader = basic_csv_reader<wchar_t,stream_source<wchar_t>>;
|
|
345
|
+
|
|
346
|
+
}}
|
|
347
|
+
|
|
348
|
+
#endif
|
|
@@ -0,0 +1,12 @@
|
|
|
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_CSV_CSV_SERIALIZER_HPP
|
|
8
|
+
#define JSONCONS_CSV_CSV_SERIALIZER_HPP
|
|
9
|
+
|
|
10
|
+
#include <jsoncons_ext/csv/csv_encoder.hpp>
|
|
11
|
+
|
|
12
|
+
#endif
|
|
@@ -0,0 +1,208 @@
|
|
|
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_CSV_DECODE_CSV_HPP
|
|
8
|
+
#define JSONCONS_CSV_DECODE_CSV_HPP
|
|
9
|
+
|
|
10
|
+
#include <jsoncons_ext/csv/csv_options.hpp>
|
|
11
|
+
#include <jsoncons_ext/csv/csv_reader.hpp>
|
|
12
|
+
#include <jsoncons_ext/csv/csv_encoder.hpp>
|
|
13
|
+
#include <jsoncons_ext/csv/csv_cursor.hpp>
|
|
14
|
+
|
|
15
|
+
namespace jsoncons {
|
|
16
|
+
namespace csv {
|
|
17
|
+
|
|
18
|
+
template <class T,class Source>
|
|
19
|
+
typename std::enable_if<traits_extension::is_basic_json<T>::value &&
|
|
20
|
+
traits_extension::is_sequence_of<Source,typename T::char_type>::value,T>::type
|
|
21
|
+
decode_csv(const Source& s, const basic_csv_decode_options<typename Source::value_type>& options = basic_csv_decode_options<typename Source::value_type>())
|
|
22
|
+
{
|
|
23
|
+
using char_type = typename Source::value_type;
|
|
24
|
+
|
|
25
|
+
json_decoder<T> decoder;
|
|
26
|
+
|
|
27
|
+
basic_csv_reader<char_type,jsoncons::string_source<char_type>> reader(s,decoder,options);
|
|
28
|
+
reader.read();
|
|
29
|
+
if (!decoder.is_valid())
|
|
30
|
+
{
|
|
31
|
+
JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column()));
|
|
32
|
+
}
|
|
33
|
+
return decoder.get_result();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
template <class T,class Source>
|
|
37
|
+
typename std::enable_if<!traits_extension::is_basic_json<T>::value &&
|
|
38
|
+
traits_extension::is_char_sequence<Source>::value,T>::type
|
|
39
|
+
decode_csv(const Source& s, const basic_csv_decode_options<typename Source::value_type>& options = basic_csv_decode_options<typename Source::value_type>())
|
|
40
|
+
{
|
|
41
|
+
using char_type = typename Source::value_type;
|
|
42
|
+
|
|
43
|
+
basic_csv_cursor<char_type> cursor(s, options);
|
|
44
|
+
jsoncons::json_decoder<basic_json<char_type>> decoder;
|
|
45
|
+
|
|
46
|
+
std::error_code ec;
|
|
47
|
+
T val = decode_traits<T,char_type>::decode(cursor, decoder, ec);
|
|
48
|
+
if (ec)
|
|
49
|
+
{
|
|
50
|
+
JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column()));
|
|
51
|
+
}
|
|
52
|
+
return val;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
template <class T,class CharT>
|
|
56
|
+
typename std::enable_if<traits_extension::is_basic_json<T>::value,T>::type
|
|
57
|
+
decode_csv(std::basic_istream<CharT>& is, const basic_csv_decode_options<CharT>& options = basic_csv_decode_options<CharT>())
|
|
58
|
+
{
|
|
59
|
+
using char_type = CharT;
|
|
60
|
+
|
|
61
|
+
json_decoder<T> decoder;
|
|
62
|
+
|
|
63
|
+
basic_csv_reader<char_type,jsoncons::stream_source<char_type>> reader(is,decoder,options);
|
|
64
|
+
reader.read();
|
|
65
|
+
if (!decoder.is_valid())
|
|
66
|
+
{
|
|
67
|
+
JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column()));
|
|
68
|
+
}
|
|
69
|
+
return decoder.get_result();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
template <class T,class CharT>
|
|
73
|
+
typename std::enable_if<!traits_extension::is_basic_json<T>::value,T>::type
|
|
74
|
+
decode_csv(std::basic_istream<CharT>& is, const basic_csv_decode_options<CharT>& options = basic_csv_decode_options<CharT>())
|
|
75
|
+
{
|
|
76
|
+
basic_csv_cursor<CharT> cursor(is, options);
|
|
77
|
+
jsoncons::json_decoder<basic_json<CharT>> decoder;
|
|
78
|
+
|
|
79
|
+
std::error_code ec;
|
|
80
|
+
T val = decode_traits<T,CharT>::decode(cursor, decoder, ec);
|
|
81
|
+
if (ec)
|
|
82
|
+
{
|
|
83
|
+
JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column()));
|
|
84
|
+
}
|
|
85
|
+
return val;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
template <class T, class InputIt>
|
|
89
|
+
typename std::enable_if<traits_extension::is_basic_json<T>::value,T>::type
|
|
90
|
+
decode_csv(InputIt first, InputIt last,
|
|
91
|
+
const basic_csv_decode_options<typename std::iterator_traits<InputIt>::value_type>& options =
|
|
92
|
+
basic_csv_decode_options<typename std::iterator_traits<InputIt>::value_type>())
|
|
93
|
+
{
|
|
94
|
+
using char_type = typename std::iterator_traits<InputIt>::value_type;
|
|
95
|
+
|
|
96
|
+
jsoncons::json_decoder<T> decoder;
|
|
97
|
+
basic_csv_reader<char_type, iterator_source<InputIt>> reader(iterator_source<InputIt>(first,last), decoder, options);
|
|
98
|
+
reader.read();
|
|
99
|
+
if (!decoder.is_valid())
|
|
100
|
+
{
|
|
101
|
+
JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column()));
|
|
102
|
+
}
|
|
103
|
+
return decoder.get_result();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
template <class T, class InputIt>
|
|
107
|
+
typename std::enable_if<!traits_extension::is_basic_json<T>::value,T>::type
|
|
108
|
+
decode_csv(InputIt first, InputIt last,
|
|
109
|
+
const basic_csv_decode_options<typename std::iterator_traits<InputIt>::value_type>& options =
|
|
110
|
+
basic_csv_decode_options<typename std::iterator_traits<InputIt>::value_type>())
|
|
111
|
+
{
|
|
112
|
+
using char_type = typename std::iterator_traits<InputIt>::value_type;
|
|
113
|
+
|
|
114
|
+
basic_csv_cursor<char_type,iterator_source<InputIt>> cursor(iterator_source<InputIt>(first, last), options);
|
|
115
|
+
jsoncons::json_decoder<basic_json<char_type>> decoder;
|
|
116
|
+
std::error_code ec;
|
|
117
|
+
T val = decode_traits<T,char_type>::decode(cursor, decoder, ec);
|
|
118
|
+
if (ec)
|
|
119
|
+
{
|
|
120
|
+
JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column()));
|
|
121
|
+
}
|
|
122
|
+
return val;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// With leading allocator parameter
|
|
126
|
+
|
|
127
|
+
template <class T,class Source,class TempAllocator>
|
|
128
|
+
typename std::enable_if<traits_extension::is_basic_json<T>::value &&
|
|
129
|
+
traits_extension::is_sequence_of<Source,typename T::char_type>::value,T>::type
|
|
130
|
+
decode_csv(temp_allocator_arg_t, const TempAllocator& temp_alloc,
|
|
131
|
+
const Source& s,
|
|
132
|
+
const basic_csv_decode_options<typename Source::value_type>& options = basic_csv_decode_options<typename Source::value_type>())
|
|
133
|
+
{
|
|
134
|
+
using char_type = typename Source::value_type;
|
|
135
|
+
|
|
136
|
+
json_decoder<T,TempAllocator> decoder(temp_alloc);
|
|
137
|
+
|
|
138
|
+
basic_csv_reader<char_type,jsoncons::string_source<char_type>,TempAllocator> reader(s,decoder,options,temp_alloc);
|
|
139
|
+
reader.read();
|
|
140
|
+
if (!decoder.is_valid())
|
|
141
|
+
{
|
|
142
|
+
JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column()));
|
|
143
|
+
}
|
|
144
|
+
return decoder.get_result();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
template <class T,class Source,class TempAllocator>
|
|
148
|
+
typename std::enable_if<!traits_extension::is_basic_json<T>::value &&
|
|
149
|
+
traits_extension::is_char_sequence<Source>::value,T>::type
|
|
150
|
+
decode_csv(temp_allocator_arg_t, const TempAllocator& temp_alloc,
|
|
151
|
+
const Source& s,
|
|
152
|
+
const basic_csv_decode_options<typename Source::value_type>& options = basic_csv_decode_options<typename Source::value_type>())
|
|
153
|
+
{
|
|
154
|
+
using char_type = typename Source::value_type;
|
|
155
|
+
|
|
156
|
+
basic_csv_cursor<char_type,stream_source<char_type>,TempAllocator> cursor(s, options, temp_alloc);
|
|
157
|
+
json_decoder<basic_json<char_type,sorted_policy,TempAllocator>,TempAllocator> decoder(temp_alloc, temp_alloc);
|
|
158
|
+
|
|
159
|
+
std::error_code ec;
|
|
160
|
+
T val = decode_traits<T,char_type>::decode(cursor, decoder, ec);
|
|
161
|
+
if (ec)
|
|
162
|
+
{
|
|
163
|
+
JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column()));
|
|
164
|
+
}
|
|
165
|
+
return val;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
template <class T,class CharT,class TempAllocator>
|
|
169
|
+
typename std::enable_if<traits_extension::is_basic_json<T>::value,T>::type
|
|
170
|
+
decode_csv(temp_allocator_arg_t, const TempAllocator& temp_alloc,
|
|
171
|
+
std::basic_istream<CharT>& is,
|
|
172
|
+
const basic_csv_decode_options<CharT>& options = basic_csv_decode_options<CharT>())
|
|
173
|
+
{
|
|
174
|
+
using char_type = CharT;
|
|
175
|
+
|
|
176
|
+
json_decoder<T,TempAllocator> decoder(temp_alloc);
|
|
177
|
+
|
|
178
|
+
basic_csv_reader<char_type,jsoncons::string_source<char_type>,TempAllocator> reader(is,decoder,options,temp_alloc);
|
|
179
|
+
reader.read();
|
|
180
|
+
if (!decoder.is_valid())
|
|
181
|
+
{
|
|
182
|
+
JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column()));
|
|
183
|
+
}
|
|
184
|
+
return decoder.get_result();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
template <class T,class CharT,class TempAllocator>
|
|
188
|
+
typename std::enable_if<!traits_extension::is_basic_json<T>::value,T>::type
|
|
189
|
+
decode_csv(temp_allocator_arg_t, const TempAllocator& temp_alloc,
|
|
190
|
+
std::basic_istream<CharT>& is,
|
|
191
|
+
const basic_csv_decode_options<CharT>& options = basic_csv_decode_options<CharT>())
|
|
192
|
+
{
|
|
193
|
+
basic_csv_cursor<CharT,stream_source<CharT>,TempAllocator> cursor(is, options, temp_alloc);
|
|
194
|
+
json_decoder<basic_json<CharT,sorted_policy,TempAllocator>,TempAllocator> decoder(temp_alloc, temp_alloc);
|
|
195
|
+
|
|
196
|
+
std::error_code ec;
|
|
197
|
+
T val = decode_traits<T,CharT>::decode(cursor, decoder, ec);
|
|
198
|
+
if (ec)
|
|
199
|
+
{
|
|
200
|
+
JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column()));
|
|
201
|
+
}
|
|
202
|
+
return val;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
} // namespace csv
|
|
206
|
+
} // namespace jsoncons
|
|
207
|
+
|
|
208
|
+
#endif
|
|
@@ -0,0 +1,122 @@
|
|
|
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_CSV_ENCODE_CSV_HPP
|
|
8
|
+
#define JSONCONS_CSV_ENCODE_CSV_HPP
|
|
9
|
+
|
|
10
|
+
#include <jsoncons_ext/csv/csv_options.hpp>
|
|
11
|
+
#include <jsoncons_ext/csv/csv_reader.hpp>
|
|
12
|
+
#include <jsoncons_ext/csv/csv_encoder.hpp>
|
|
13
|
+
|
|
14
|
+
namespace jsoncons {
|
|
15
|
+
namespace csv {
|
|
16
|
+
|
|
17
|
+
template <class T,class Container>
|
|
18
|
+
typename std::enable_if<traits_extension::is_basic_json<T>::value &&
|
|
19
|
+
traits_extension::is_back_insertable_char_container<Container>::value>::type
|
|
20
|
+
encode_csv(const T& j, Container& s, const basic_csv_encode_options<typename Container::value_type>& options = basic_csv_encode_options<typename Container::value_type>())
|
|
21
|
+
{
|
|
22
|
+
using char_type = typename Container::value_type;
|
|
23
|
+
basic_csv_encoder<char_type,jsoncons::string_sink<std::basic_string<char_type>>> encoder(s,options);
|
|
24
|
+
j.dump(encoder);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
template <class T,class Container>
|
|
28
|
+
typename std::enable_if<!traits_extension::is_basic_json<T>::value &&
|
|
29
|
+
traits_extension::is_back_insertable_char_container<Container>::value>::type
|
|
30
|
+
encode_csv(const T& val, Container& s, const basic_csv_encode_options<typename Container::value_type>& options = basic_csv_encode_options<typename Container::value_type>())
|
|
31
|
+
{
|
|
32
|
+
using char_type = typename Container::value_type;
|
|
33
|
+
basic_csv_encoder<char_type,jsoncons::string_sink<std::basic_string<char_type>>> encoder(s,options);
|
|
34
|
+
std::error_code ec;
|
|
35
|
+
encode_traits<T,char_type>::encode(val, encoder, json(), ec);
|
|
36
|
+
if (ec)
|
|
37
|
+
{
|
|
38
|
+
JSONCONS_THROW(ser_error(ec));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
template <class T, class CharT>
|
|
43
|
+
typename std::enable_if<traits_extension::is_basic_json<T>::value,void>::type
|
|
44
|
+
encode_csv(const T& j, std::basic_ostream<CharT>& os, const basic_csv_encode_options<CharT>& options = basic_csv_encode_options<CharT>())
|
|
45
|
+
{
|
|
46
|
+
using char_type = CharT;
|
|
47
|
+
basic_csv_encoder<char_type,jsoncons::stream_sink<char_type>> encoder(os,options);
|
|
48
|
+
j.dump(encoder);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
template <class T, class CharT>
|
|
52
|
+
typename std::enable_if<!traits_extension::is_basic_json<T>::value,void>::type
|
|
53
|
+
encode_csv(const T& val, std::basic_ostream<CharT>& os, const basic_csv_encode_options<CharT>& options = basic_csv_encode_options<CharT>())
|
|
54
|
+
{
|
|
55
|
+
using char_type = CharT;
|
|
56
|
+
basic_csv_encoder<char_type,jsoncons::stream_sink<char_type>> encoder(os,options);
|
|
57
|
+
std::error_code ec;
|
|
58
|
+
encode_traits<T,CharT>::encode(val, encoder, json(), ec);
|
|
59
|
+
if (ec)
|
|
60
|
+
{
|
|
61
|
+
JSONCONS_THROW(ser_error(ec));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// with temp_allocator_arg_t
|
|
66
|
+
|
|
67
|
+
template <class T, class Container, class TempAllocator>
|
|
68
|
+
typename std::enable_if<traits_extension::is_basic_json<T>::value &&
|
|
69
|
+
traits_extension::is_back_insertable_char_container<Container>::value>::type
|
|
70
|
+
encode_csv(temp_allocator_arg_t, const TempAllocator& temp_alloc,
|
|
71
|
+
const T& j, Container& s, const basic_csv_encode_options<typename Container::value_type>& options = basic_csv_encode_options<typename Container::value_type>())
|
|
72
|
+
{
|
|
73
|
+
using char_type = typename Container::value_type;
|
|
74
|
+
basic_csv_encoder<char_type,jsoncons::string_sink<std::basic_string<char_type>>,TempAllocator> encoder(s, options, temp_alloc);
|
|
75
|
+
j.dump(encoder);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
template <class T, class Container, class TempAllocator>
|
|
79
|
+
typename std::enable_if<!traits_extension::is_basic_json<T>::value &&
|
|
80
|
+
traits_extension::is_back_insertable_char_container<Container>::value>::type
|
|
81
|
+
encode_csv(temp_allocator_arg_t, const TempAllocator& temp_alloc,
|
|
82
|
+
const T& val, Container& s, const basic_csv_encode_options<typename Container::value_type>& options = basic_csv_encode_options<typename Container::value_type>())
|
|
83
|
+
{
|
|
84
|
+
using char_type = typename Container::value_type;
|
|
85
|
+
basic_csv_encoder<char_type,jsoncons::string_sink<std::basic_string<char_type>>,TempAllocator> encoder(s, options, temp_alloc);
|
|
86
|
+
std::error_code ec;
|
|
87
|
+
encode_traits<T,char_type>::encode(val, encoder, json(), ec);
|
|
88
|
+
if (ec)
|
|
89
|
+
{
|
|
90
|
+
JSONCONS_THROW(ser_error(ec));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
template <class T, class CharT, class TempAllocator>
|
|
95
|
+
typename std::enable_if<traits_extension::is_basic_json<T>::value,void>::type
|
|
96
|
+
encode_csv(temp_allocator_arg_t, const TempAllocator& temp_alloc,
|
|
97
|
+
const T& j, std::basic_ostream<CharT>& os, const basic_csv_encode_options<CharT>& options = basic_csv_encode_options<CharT>())
|
|
98
|
+
{
|
|
99
|
+
using char_type = CharT;
|
|
100
|
+
basic_csv_encoder<char_type,jsoncons::stream_sink<char_type>,TempAllocator> encoder(os, options, temp_alloc);
|
|
101
|
+
j.dump(encoder);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
template <class T, class CharT, class TempAllocator>
|
|
105
|
+
typename std::enable_if<!traits_extension::is_basic_json<T>::value,void>::type
|
|
106
|
+
encode_csv(temp_allocator_arg_t, const TempAllocator& temp_alloc,
|
|
107
|
+
const T& val, std::basic_ostream<CharT>& os, const basic_csv_encode_options<CharT>& options = basic_csv_encode_options<CharT>())
|
|
108
|
+
{
|
|
109
|
+
using char_type = CharT;
|
|
110
|
+
basic_csv_encoder<char_type,jsoncons::stream_sink<char_type>,TempAllocator> encoder(os, options, temp_alloc);
|
|
111
|
+
std::error_code ec;
|
|
112
|
+
encode_traits<T,CharT>::encode(val, encoder, json(), ec);
|
|
113
|
+
if (ec)
|
|
114
|
+
{
|
|
115
|
+
JSONCONS_THROW(ser_error(ec));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
} // namespace csv
|
|
120
|
+
} // namespace jsoncons
|
|
121
|
+
|
|
122
|
+
#endif
|