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,315 @@
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_ENCODE_JSON_HPP
8
+ #define JSONCONS_ENCODE_JSON_HPP
9
+
10
+ #include <iostream>
11
+ #include <string>
12
+ #include <tuple>
13
+ #include <memory>
14
+ #include <istream> // std::basic_istream
15
+ #include <jsoncons/encode_traits.hpp>
16
+ #include <jsoncons/json_cursor.hpp>
17
+
18
+ namespace jsoncons {
19
+
20
+ // to string
21
+
22
+ template <class T, class Container>
23
+ typename std::enable_if<traits_extension::is_basic_json<T>::value &&
24
+ traits_extension::is_back_insertable_char_container<Container>::value>::type
25
+ encode_json(const T& val,
26
+ Container& s,
27
+ const basic_json_encode_options<typename Container::value_type>& options =
28
+ basic_json_encode_options<typename Container::value_type>())
29
+ {
30
+ using char_type = typename Container::value_type;
31
+
32
+ basic_compact_json_encoder<char_type, jsoncons::string_sink<Container>> encoder(s, options);
33
+ val.dump(encoder);
34
+ }
35
+
36
+ template <class T, class Container>
37
+ typename std::enable_if<!traits_extension::is_basic_json<T>::value &&
38
+ traits_extension::is_back_insertable_char_container<Container>::value>::type
39
+ encode_json(const T& val,
40
+ Container& s,
41
+ const basic_json_encode_options<typename Container::value_type>& options = basic_json_encode_options<typename Container::value_type>())
42
+ {
43
+ using char_type = typename Container::value_type;
44
+
45
+ basic_compact_json_encoder<char_type, jsoncons::string_sink<Container>> encoder(s, options);
46
+ encode_json(val, encoder);
47
+ }
48
+
49
+ // to stream
50
+
51
+ template <class T, class CharT>
52
+ typename std::enable_if<traits_extension::is_basic_json<T>::value>::type
53
+ encode_json(const T& val,
54
+ std::basic_ostream<CharT>& os,
55
+ const basic_json_encode_options<CharT>& options = basic_json_encode_options<CharT>())
56
+ {
57
+ basic_compact_json_encoder<CharT> encoder(os, options);
58
+ val.dump(encoder);
59
+ }
60
+
61
+ template <class T, class CharT>
62
+ typename std::enable_if<!traits_extension::is_basic_json<T>::value>::type
63
+ encode_json(const T& val,
64
+ std::basic_ostream<CharT>& os,
65
+ const basic_json_encode_options<CharT>& options = basic_json_encode_options<CharT>())
66
+ {
67
+ basic_compact_json_encoder<CharT> encoder(os, options);
68
+ encode_json(val, encoder);
69
+ }
70
+
71
+ // encode_json_pretty
72
+
73
+ template <class T, class Container>
74
+ typename std::enable_if<traits_extension::is_basic_json<T>::value &&
75
+ traits_extension::is_back_insertable_char_container<Container>::value>::type
76
+ encode_json_pretty(const T& val,
77
+ Container& s,
78
+ const basic_json_encode_options<typename Container::value_type>& options = basic_json_encode_options<typename Container::value_type>())
79
+ {
80
+ using char_type = typename Container::value_type;
81
+
82
+ basic_json_encoder<char_type,jsoncons::string_sink<Container>> encoder(s, options);
83
+ val.dump(encoder);
84
+ }
85
+
86
+ template <class T, class Container>
87
+ typename std::enable_if<!traits_extension::is_basic_json<T>::value &&
88
+ traits_extension::is_back_insertable_char_container<Container>::value>::type
89
+ encode_json_pretty(const T& val,
90
+ Container& s,
91
+ const basic_json_encode_options<typename Container::value_type>& options = basic_json_encode_options<typename Container::value_type>())
92
+ {
93
+ using char_type = typename Container::value_type;
94
+ basic_json_encoder<char_type,jsoncons::string_sink<Container>> encoder(s, options);
95
+ encode_json(val, encoder);
96
+ }
97
+
98
+ template <class T, class CharT>
99
+ typename std::enable_if<traits_extension::is_basic_json<T>::value>::type
100
+ encode_json_pretty(const T& val,
101
+ std::basic_ostream<CharT>& os,
102
+ const basic_json_encode_options<CharT>& options = basic_json_encode_options<CharT>())
103
+ {
104
+ basic_json_encoder<CharT> encoder(os, options);
105
+ val.dump(encoder);
106
+ }
107
+
108
+ template <class T, class CharT>
109
+ typename std::enable_if<!traits_extension::is_basic_json<T>::value>::type
110
+ encode_json_pretty(const T& val,
111
+ std::basic_ostream<CharT>& os,
112
+ const basic_json_encode_options<CharT>& options = basic_json_encode_options<CharT>())
113
+ {
114
+ basic_json_encoder<CharT> encoder(os, options);
115
+ encode_json(val, encoder);
116
+ }
117
+
118
+ template <class T, class CharT>
119
+ void encode_json(const T& val, basic_json_visitor<CharT>& encoder)
120
+ {
121
+ std::error_code ec;
122
+ encode_traits<T,CharT>::encode(val, encoder, basic_json<CharT>(), ec);
123
+ if (ec)
124
+ {
125
+ JSONCONS_THROW(ser_error(ec));
126
+ }
127
+ encoder.flush();
128
+ }
129
+
130
+ template <class T, class Container, class TempAllocator>
131
+ void encode_json(temp_allocator_arg_t, const TempAllocator& temp_alloc,
132
+ const T& val,
133
+ Container& s,
134
+ indenting line_indent = indenting::no_indent)
135
+ {
136
+ encode_json(temp_allocator_arg, temp_alloc, val, s, basic_json_encode_options<typename Container::value_type>(), line_indent);
137
+ }
138
+
139
+ // legacy
140
+
141
+ template <class T, class Container>
142
+ void encode_json(const T& val, Container& s, indenting line_indent)
143
+ {
144
+ if (line_indent == indenting::indent)
145
+ {
146
+ encode_json_pretty(val,s);
147
+ }
148
+ else
149
+ {
150
+ encode_json(val,s);
151
+ }
152
+ }
153
+
154
+ template <class T, class Container>
155
+ void encode_json(const T& val,
156
+ Container& s,
157
+ const basic_json_encode_options<typename Container::value_type>& options,
158
+ indenting line_indent)
159
+ {
160
+ if (line_indent == indenting::indent)
161
+ {
162
+ encode_json_pretty(val,s,options);
163
+ }
164
+ else
165
+ {
166
+ encode_json(val,s,options);
167
+ }
168
+ }
169
+
170
+ template <class T, class CharT>
171
+ void encode_json(const T& val,
172
+ std::basic_ostream<CharT>& os,
173
+ indenting line_indent)
174
+ {
175
+ if (line_indent == indenting::indent)
176
+ {
177
+ encode_json_pretty(val, os);
178
+ }
179
+ else
180
+ {
181
+ encode_json(val, os);
182
+ }
183
+ }
184
+
185
+ template <class T, class CharT>
186
+ void encode_json(const T& val,
187
+ std::basic_ostream<CharT>& os,
188
+ const basic_json_encode_options<CharT>& options,
189
+ indenting line_indent)
190
+ {
191
+ if (line_indent == indenting::indent)
192
+ {
193
+ encode_json_pretty(val, os, options);
194
+ }
195
+ else
196
+ {
197
+ encode_json(val, os, options);
198
+ }
199
+ }
200
+
201
+ //end legacy
202
+
203
+ template <class T, class Container, class TempAllocator>
204
+ typename std::enable_if<traits_extension::is_basic_json<T>::value &&
205
+ traits_extension::is_back_insertable_char_container<Container>::value>::type
206
+ encode_json(temp_allocator_arg_t, const TempAllocator& temp_alloc,
207
+ const T& val,
208
+ Container& s,
209
+ const basic_json_encode_options<typename Container::value_type>& options,
210
+ indenting line_indent = indenting::no_indent)
211
+ {
212
+ using char_type = typename Container::value_type;
213
+ if (line_indent == indenting::indent)
214
+ {
215
+ basic_json_encoder<char_type,jsoncons::string_sink<Container>,TempAllocator> encoder(s, options, temp_alloc);
216
+ val.dump(encoder);
217
+ }
218
+ else
219
+ {
220
+ basic_compact_json_encoder<char_type, jsoncons::string_sink<Container>,TempAllocator> encoder(s, options, temp_alloc);
221
+ val.dump(encoder);
222
+ }
223
+ }
224
+
225
+ template <class T, class Container, class TempAllocator>
226
+ typename std::enable_if<!traits_extension::is_basic_json<T>::value &&
227
+ traits_extension::is_back_insertable_char_container<Container>::value>::type
228
+ encode_json(temp_allocator_arg_t, const TempAllocator& temp_alloc,
229
+ const T& val,
230
+ Container& s,
231
+ const basic_json_encode_options<typename Container::value_type>& options,
232
+ indenting line_indent)
233
+ {
234
+ using char_type = typename Container::value_type;
235
+ if (line_indent == indenting::indent)
236
+ {
237
+ basic_json_encoder<char_type,jsoncons::string_sink<Container>,TempAllocator> encoder(s, options, temp_alloc);
238
+ encode_json(temp_allocator_arg, temp_alloc, val, encoder);
239
+ }
240
+ else
241
+ {
242
+ basic_compact_json_encoder<char_type,jsoncons::string_sink<Container>,TempAllocator> encoder(s, options, temp_alloc);
243
+ encode_json(temp_allocator_arg, temp_alloc, val, encoder);
244
+ }
245
+ }
246
+
247
+ template <class T, class CharT, class TempAllocator>
248
+ void encode_json(temp_allocator_arg_t, const TempAllocator& temp_alloc,
249
+ const T& val,
250
+ std::basic_ostream<CharT>& os,
251
+ indenting line_indent = indenting::no_indent)
252
+ {
253
+ encode_json(temp_allocator_arg, temp_alloc, val, os, basic_json_encode_options<CharT>(), line_indent);
254
+ }
255
+
256
+ template <class T, class CharT, class TempAllocator>
257
+ typename std::enable_if<traits_extension::is_basic_json<T>::value>::type
258
+ encode_json(temp_allocator_arg_t, const TempAllocator& temp_alloc,
259
+ const T& val,
260
+ std::basic_ostream<CharT>& os,
261
+ const basic_json_encode_options<CharT>& options,
262
+ indenting line_indent = indenting::no_indent)
263
+ {
264
+ if (line_indent == indenting::indent)
265
+ {
266
+ basic_json_encoder<CharT,jsoncons::stream_sink<CharT>,TempAllocator> encoder(os, options, temp_alloc);
267
+ val.dump(encoder);
268
+ }
269
+ else
270
+ {
271
+ basic_compact_json_encoder<CharT,jsoncons::stream_sink<CharT>,TempAllocator> encoder(os, options, temp_alloc);
272
+ val.dump(encoder);
273
+ }
274
+ }
275
+
276
+ template <class T, class CharT, class TempAllocator>
277
+ typename std::enable_if<!traits_extension::is_basic_json<T>::value>::type
278
+ encode_json(temp_allocator_arg_t, const TempAllocator& temp_alloc,
279
+ const T& val,
280
+ std::basic_ostream<CharT>& os,
281
+ const basic_json_encode_options<CharT>& options,
282
+ indenting line_indent)
283
+ {
284
+ if (line_indent == indenting::indent)
285
+ {
286
+ basic_json_encoder<CharT> encoder(os, options);
287
+ encode_json(temp_allocator_arg, temp_alloc, val, encoder);
288
+ }
289
+ else
290
+ {
291
+ basic_compact_json_encoder<CharT> encoder(os, options);
292
+ encode_json(temp_allocator_arg, temp_alloc, val, encoder);
293
+ }
294
+ }
295
+
296
+ template <class T, class CharT, class TempAllocator>
297
+ typename std::enable_if<!traits_extension::is_basic_json<T>::value>::type
298
+ encode_json(temp_allocator_arg_t, const TempAllocator& temp_alloc,
299
+ const T& val,
300
+ basic_json_visitor<CharT>& encoder)
301
+ {
302
+ std::error_code ec;
303
+ basic_json<CharT,sorted_policy,TempAllocator> proto(temp_alloc);
304
+ encode_traits<T,CharT>::encode(val, encoder, proto, ec);
305
+ if (ec)
306
+ {
307
+ JSONCONS_THROW(ser_error(ec));
308
+ }
309
+ encoder.flush();
310
+ }
311
+
312
+ } // jsoncons
313
+
314
+ #endif
315
+
@@ -0,0 +1,378 @@
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_ENCODE_TRAITS_HPP
8
+ #define JSONCONS_ENCODE_TRAITS_HPP
9
+
10
+ #include <string>
11
+ #include <tuple>
12
+ #include <array>
13
+ #include <memory>
14
+ #include <type_traits> // std::enable_if, std::true_type, std::false_type
15
+ #include <jsoncons/json_visitor.hpp>
16
+ #include <jsoncons/json_decoder.hpp>
17
+ #include <jsoncons/json_options.hpp>
18
+ #include <jsoncons/json_encoder.hpp>
19
+ #include <jsoncons/json_type_traits.hpp>
20
+ #include <jsoncons/conv_error.hpp>
21
+
22
+ namespace jsoncons {
23
+
24
+ // encode_traits
25
+
26
+ template <class T, class CharT, class Enable = void>
27
+ struct encode_traits
28
+ {
29
+ template <class Json>
30
+ static void encode(const T& val,
31
+ basic_json_visitor<CharT>& encoder,
32
+ const Json& proto,
33
+ std::error_code& ec)
34
+ {
35
+ encode(std::integral_constant<bool, traits_extension::is_stateless<typename Json::allocator_type>::value>(),
36
+ val, encoder, proto, ec);
37
+ }
38
+ private:
39
+ template <class Json>
40
+ static void encode(std::true_type,
41
+ const T& val,
42
+ basic_json_visitor<CharT>& encoder,
43
+ const Json& /*proto*/,
44
+ std::error_code& ec)
45
+ {
46
+ auto j = json_type_traits<Json,T>::to_json(val);
47
+ j.dump(encoder, ec);
48
+ }
49
+ template <class Json>
50
+ static void encode(std::false_type,
51
+ const T& val,
52
+ basic_json_visitor<CharT>& encoder,
53
+ const Json& proto,
54
+ std::error_code& ec)
55
+ {
56
+ auto j = json_type_traits<Json,T>::to_json(val, proto.get_allocator());
57
+ j.dump(encoder, ec);
58
+ }
59
+ };
60
+
61
+ // specializations
62
+
63
+ // bool
64
+ template <class T, class CharT>
65
+ struct encode_traits<T,CharT,
66
+ typename std::enable_if<traits_extension::is_bool<T>::value
67
+ >::type>
68
+ {
69
+ template <class Json>
70
+ static void encode(const T& val,
71
+ basic_json_visitor<CharT>& encoder,
72
+ const Json&,
73
+ std::error_code& ec)
74
+ {
75
+ encoder.bool_value(val,semantic_tag::none,ser_context(),ec);
76
+ }
77
+ };
78
+
79
+ // uint
80
+ template <class T, class CharT>
81
+ struct encode_traits<T,CharT,
82
+ typename std::enable_if<traits_extension::is_u8_u16_u32_or_u64<T>::value
83
+ >::type>
84
+ {
85
+ template <class Json>
86
+ static void encode(const T& val,
87
+ basic_json_visitor<CharT>& encoder,
88
+ const Json&,
89
+ std::error_code& ec)
90
+ {
91
+ encoder.uint64_value(val,semantic_tag::none,ser_context(),ec);
92
+ }
93
+ };
94
+
95
+ // int
96
+ template <class T, class CharT>
97
+ struct encode_traits<T,CharT,
98
+ typename std::enable_if<traits_extension::is_i8_i16_i32_or_i64<T>::value
99
+ >::type>
100
+ {
101
+ template <class Json>
102
+ static void encode(const T& val,
103
+ basic_json_visitor<CharT>& encoder,
104
+ const Json&,
105
+ std::error_code& ec)
106
+ {
107
+ encoder.int64_value(val,semantic_tag::none,ser_context(),ec);
108
+ }
109
+ };
110
+
111
+ // float or double
112
+ template <class T, class CharT>
113
+ struct encode_traits<T,CharT,
114
+ typename std::enable_if<traits_extension::is_float_or_double<T>::value
115
+ >::type>
116
+ {
117
+ template <class Json>
118
+ static void encode(const T& val,
119
+ basic_json_visitor<CharT>& encoder,
120
+ const Json&,
121
+ std::error_code& ec)
122
+ {
123
+ encoder.double_value(val,semantic_tag::none,ser_context(),ec);
124
+ }
125
+ };
126
+
127
+ // string
128
+ template <class T, class CharT>
129
+ struct encode_traits<T,CharT,
130
+ typename std::enable_if<traits_extension::is_string<T>::value &&
131
+ std::is_same<typename T::value_type,CharT>::value
132
+ >::type>
133
+ {
134
+ template <class Json>
135
+ static void encode(const T& val,
136
+ basic_json_visitor<CharT>& encoder,
137
+ const Json&,
138
+ std::error_code& ec)
139
+ {
140
+ encoder.string_value(val,semantic_tag::none,ser_context(),ec);
141
+ }
142
+ };
143
+ template <class T, class CharT>
144
+ struct encode_traits<T,CharT,
145
+ typename std::enable_if<traits_extension::is_string<T>::value &&
146
+ !std::is_same<typename T::value_type,CharT>::value
147
+ >::type>
148
+ {
149
+ template <class Json>
150
+ static void encode(const T& val,
151
+ basic_json_visitor<CharT>& encoder,
152
+ const Json&,
153
+ std::error_code& ec)
154
+ {
155
+ std::basic_string<CharT> s;
156
+ unicode_traits::convert(val.data(), val.size(), s);
157
+ encoder.string_value(s,semantic_tag::none,ser_context(),ec);
158
+ }
159
+ };
160
+
161
+ // std::pair
162
+
163
+ template <class T1, class T2, class CharT>
164
+ struct encode_traits<std::pair<T1, T2>, CharT>
165
+ {
166
+ using value_type = std::pair<T1, T2>;
167
+
168
+ template <class Json>
169
+ static void encode(const value_type& val,
170
+ basic_json_visitor<CharT>& encoder,
171
+ const Json& proto,
172
+ std::error_code& ec)
173
+ {
174
+ encoder.begin_array(2,semantic_tag::none,ser_context(),ec);
175
+ if (ec) return;
176
+ encode_traits<T1,CharT>::encode(val.first, encoder, proto, ec);
177
+ if (ec) return;
178
+ encode_traits<T2,CharT>::encode(val.second, encoder, proto, ec);
179
+ if (ec) return;
180
+ encoder.end_array(ser_context(),ec);
181
+ }
182
+ };
183
+
184
+ // std::tuple
185
+
186
+ namespace detail
187
+ {
188
+ template<size_t Pos, std::size_t Size, class Json, class Tuple>
189
+ struct json_serialize_tuple_helper
190
+ {
191
+ using char_type = typename Json::char_type;
192
+ using element_type = typename std::tuple_element<Size-Pos, Tuple>::type;
193
+ using next = json_serialize_tuple_helper<Pos-1, Size, Json, Tuple>;
194
+
195
+ static void encode(const Tuple& tuple,
196
+ basic_json_visitor<char_type>& encoder,
197
+ const Json& proto,
198
+ std::error_code& ec)
199
+ {
200
+ encode_traits<element_type,char_type>::encode(std::get<Size-Pos>(tuple), encoder, proto, ec);
201
+ if (ec) return;
202
+ next::encode(tuple, encoder, proto, ec);
203
+ }
204
+ };
205
+
206
+ template<size_t Size, class Json, class Tuple>
207
+ struct json_serialize_tuple_helper<0, Size, Json, Tuple>
208
+ {
209
+ using char_type = typename Json::char_type;
210
+ static void encode(const Tuple&,
211
+ basic_json_visitor<char_type>&,
212
+ const Json&,
213
+ std::error_code&)
214
+ {
215
+ }
216
+ };
217
+ } // namespace detail
218
+
219
+
220
+ template <class CharT, typename... E>
221
+ struct encode_traits<std::tuple<E...>, CharT>
222
+ {
223
+ using value_type = std::tuple<E...>;
224
+ static constexpr std::size_t size = sizeof...(E);
225
+
226
+ template <class Json>
227
+ static void encode(const value_type& val,
228
+ basic_json_visitor<CharT>& encoder,
229
+ const Json& proto,
230
+ std::error_code& ec)
231
+ {
232
+ using helper = jsoncons::detail::json_serialize_tuple_helper<size, size, Json, std::tuple<E...>>;
233
+ encoder.begin_array(size,semantic_tag::none,ser_context(),ec);
234
+ if (ec) return;
235
+ helper::encode(val, encoder, proto, ec);
236
+ if (ec) return;
237
+ encoder.end_array(ser_context(),ec);
238
+ }
239
+ };
240
+
241
+ // vector like
242
+ template <class T, class CharT>
243
+ struct encode_traits<T,CharT,
244
+ typename std::enable_if<!is_json_type_traits_declared<T>::value &&
245
+ traits_extension::is_list_like<T>::value &&
246
+ !traits_extension::is_typed_array<T>::value
247
+ >::type>
248
+ {
249
+ using value_type = typename T::value_type;
250
+
251
+ template <class Json>
252
+ static void encode(const T& val,
253
+ basic_json_visitor<CharT>& encoder,
254
+ const Json& proto,
255
+ std::error_code& ec)
256
+ {
257
+ encoder.begin_array(val.size(),semantic_tag::none,ser_context(),ec);
258
+ if (ec) return;
259
+ for (auto it = std::begin(val); it != std::end(val); ++it)
260
+ {
261
+ encode_traits<value_type,CharT>::encode(*it, encoder, proto, ec);
262
+ if (ec) return;
263
+ }
264
+ encoder.end_array(ser_context(), ec);
265
+ }
266
+ };
267
+
268
+ template <class T, class CharT>
269
+ struct encode_traits<T,CharT,
270
+ typename std::enable_if<!is_json_type_traits_declared<T>::value &&
271
+ traits_extension::is_list_like<T>::value &&
272
+ traits_extension::is_typed_array<T>::value
273
+ >::type>
274
+ {
275
+ using value_type = typename T::value_type;
276
+
277
+ template <class Json>
278
+ static void encode(const T& val,
279
+ basic_json_visitor<CharT>& encoder,
280
+ const Json&,
281
+ std::error_code& ec)
282
+ {
283
+ encoder.typed_array(jsoncons::span<const value_type>(val), semantic_tag::none, ser_context(), ec);
284
+ }
285
+ };
286
+
287
+ // std::array
288
+
289
+ template <class T, class CharT, std::size_t N>
290
+ struct encode_traits<std::array<T,N>,CharT>
291
+ {
292
+ using value_type = typename std::array<T,N>::value_type;
293
+
294
+ template <class Json>
295
+ static void encode(const std::array<T, N>& val,
296
+ basic_json_visitor<CharT>& encoder,
297
+ const Json& proto,
298
+ std::error_code& ec)
299
+ {
300
+ encoder.begin_array(val.size(),semantic_tag::none,ser_context(),ec);
301
+ if (ec) return;
302
+ for (auto it = std::begin(val); it != std::end(val); ++it)
303
+ {
304
+ encode_traits<value_type,CharT>::encode(*it, encoder, proto, ec);
305
+ if (ec) return;
306
+ }
307
+ encoder.end_array(ser_context(),ec);
308
+ }
309
+ };
310
+
311
+ // map like
312
+
313
+ template <class T, class CharT>
314
+ struct encode_traits<T,CharT,
315
+ typename std::enable_if<!is_json_type_traits_declared<T>::value &&
316
+ traits_extension::is_map_like<T>::value &&
317
+ traits_extension::is_constructible_from_const_pointer_and_size<typename T::key_type>::value
318
+ >::type>
319
+ {
320
+ using mapped_type = typename T::mapped_type;
321
+ using value_type = typename T::value_type;
322
+ using key_type = typename T::key_type;
323
+
324
+ template <class Json>
325
+ static void encode(const T& val,
326
+ basic_json_visitor<CharT>& encoder,
327
+ const Json& proto,
328
+ std::error_code& ec)
329
+ {
330
+ encoder.begin_object(val.size(), semantic_tag::none, ser_context(), ec);
331
+ if (ec) return;
332
+ for (auto it = std::begin(val); it != std::end(val); ++it)
333
+ {
334
+ encoder.key(it->first);
335
+ encode_traits<mapped_type,CharT>::encode(it->second, encoder, proto, ec);
336
+ if (ec) return;
337
+ }
338
+ encoder.end_object(ser_context(), ec);
339
+ if (ec) return;
340
+ }
341
+ };
342
+
343
+ template <class T, class CharT>
344
+ struct encode_traits<T,CharT,
345
+ typename std::enable_if<!is_json_type_traits_declared<T>::value &&
346
+ traits_extension::is_map_like<T>::value &&
347
+ std::is_integral<typename T::key_type>::value
348
+ >::type>
349
+ {
350
+ using mapped_type = typename T::mapped_type;
351
+ using value_type = typename T::value_type;
352
+ using key_type = typename T::key_type;
353
+
354
+ template <class Json>
355
+ static void encode(const T& val,
356
+ basic_json_visitor<CharT>& encoder,
357
+ const Json& proto,
358
+ std::error_code& ec)
359
+ {
360
+ encoder.begin_object(val.size(), semantic_tag::none, ser_context(), ec);
361
+ if (ec) return;
362
+ for (auto it = std::begin(val); it != std::end(val); ++it)
363
+ {
364
+ std::basic_string<typename Json::char_type> s;
365
+ jsoncons::detail::from_integer(it->first,s);
366
+ encoder.key(s);
367
+ encode_traits<mapped_type,CharT>::encode(it->second, encoder, proto, ec);
368
+ if (ec) return;
369
+ }
370
+ encoder.end_object(ser_context(), ec);
371
+ if (ec) return;
372
+ }
373
+ };
374
+
375
+ } // jsoncons
376
+
377
+ #endif
378
+