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,445 @@
1
+ // Copyright 2021 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_JSONPATH_JSON_LOCATION_HPP
8
+ #define JSONCONS_JSONPATH_JSON_LOCATION_HPP
9
+
10
+ #include <string>
11
+ #include <vector>
12
+ #include <functional>
13
+ #include <algorithm> // std::reverse
14
+ #include <jsoncons/config/jsoncons_config.hpp>
15
+ #include <jsoncons/detail/write_number.hpp>
16
+ #include <jsoncons_ext/jsonpath/jsonpath_error.hpp>
17
+ #include <jsoncons/json_type.hpp>
18
+
19
+ namespace jsoncons {
20
+ namespace jsonpath {
21
+
22
+ template <class CharT>
23
+ class json_location;
24
+
25
+ enum class json_location_node_kind { root, index, name };
26
+
27
+ template <class CharT>
28
+ class json_location_node
29
+ {
30
+ friend class json_location<CharT>;
31
+ public:
32
+ using char_type = CharT;
33
+ using string_type = std::basic_string<CharT>;
34
+ private:
35
+
36
+ const json_location_node* parent_;
37
+ json_location_node_kind node_kind_;
38
+ string_type name_;
39
+ std::size_t index_;
40
+ public:
41
+ json_location_node(char_type c)
42
+ : parent_(nullptr), node_kind_(json_location_node_kind::root), index_(0)
43
+ {
44
+ name_.push_back(c);
45
+ }
46
+
47
+ json_location_node(const json_location_node* parent, const string_type& name)
48
+ : parent_(parent), node_kind_(json_location_node_kind::name), name_(name), index_(0)
49
+ {
50
+ }
51
+
52
+ json_location_node(const json_location_node* parent, std::size_t index)
53
+ : parent_(parent), node_kind_(json_location_node_kind::index), index_(index)
54
+ {
55
+ }
56
+
57
+ const json_location_node* parent() const { return parent_;}
58
+
59
+ json_location_node_kind node_kind() const
60
+ {
61
+ return node_kind_;
62
+ }
63
+
64
+ const string_type& name() const
65
+ {
66
+ return name_;
67
+ }
68
+
69
+ std::size_t index() const
70
+ {
71
+ return index_;
72
+ }
73
+
74
+ void swap(json_location_node& node)
75
+ {
76
+ std::swap(parent_, node.parent_);
77
+ std::swap(node_kind_, node.node_kind_);
78
+ std::swap(name_, node.name_);
79
+ std::swap(index_, node.index_);
80
+ }
81
+
82
+ private:
83
+
84
+ std::size_t node_hash() const
85
+ {
86
+ std::size_t h = node_kind_ == json_location_node_kind::index ? std::hash<std::size_t>{}(index_) : std::hash<string_type>{}(name_);
87
+
88
+ return h;
89
+ }
90
+
91
+ int compare_node(const json_location_node& other) const
92
+ {
93
+ int diff = 0;
94
+ if (node_kind_ != other.node_kind_)
95
+ {
96
+ diff = static_cast<int>(node_kind_) - static_cast<int>(other.node_kind_);
97
+ }
98
+ else
99
+ {
100
+ switch (node_kind_)
101
+ {
102
+ case json_location_node_kind::root:
103
+ diff = name_.compare(other.name_);
104
+ break;
105
+ case json_location_node_kind::index:
106
+ diff = index_ < other.index_ ? -1 : index_ > other.index_ ? 1 : 0;
107
+ break;
108
+ case json_location_node_kind::name:
109
+ diff = name_.compare(other.name_);
110
+ break;
111
+ }
112
+ }
113
+ return diff;
114
+ }
115
+ };
116
+
117
+ namespace detail {
118
+
119
+ template <class Iterator>
120
+ class json_location_iterator
121
+ {
122
+ Iterator it_;
123
+
124
+ public:
125
+ using iterator_category = std::random_access_iterator_tag;
126
+
127
+ using value_type = typename std::remove_pointer<typename std::iterator_traits<Iterator>::value_type>::type;
128
+ using difference_type = typename std::iterator_traits<Iterator>::difference_type;
129
+ using pointer = const value_type*;
130
+ using reference = const value_type&;
131
+
132
+ json_location_iterator() : it_()
133
+ {
134
+ }
135
+
136
+ explicit json_location_iterator(Iterator ptr) : it_(ptr)
137
+ {
138
+ }
139
+
140
+ json_location_iterator(const json_location_iterator&) = default;
141
+ json_location_iterator(json_location_iterator&&) = default;
142
+ json_location_iterator& operator=(const json_location_iterator&) = default;
143
+ json_location_iterator& operator=(json_location_iterator&&) = default;
144
+
145
+ template <class Iter,
146
+ class=typename std::enable_if<!std::is_same<Iter,Iterator>::value && std::is_convertible<Iter,Iterator>::value>::type>
147
+ json_location_iterator(const json_location_iterator<Iter>& other)
148
+ : it_(other.it_)
149
+ {
150
+ }
151
+
152
+ operator Iterator() const
153
+ {
154
+ return it_;
155
+ }
156
+
157
+ reference operator*() const
158
+ {
159
+ return *(*it_);
160
+ }
161
+
162
+ pointer operator->() const
163
+ {
164
+ return (*it_);
165
+ }
166
+
167
+ json_location_iterator& operator++()
168
+ {
169
+ ++it_;
170
+ return *this;
171
+ }
172
+
173
+ json_location_iterator operator++(int)
174
+ {
175
+ json_location_iterator temp = *this;
176
+ ++*this;
177
+ return temp;
178
+ }
179
+
180
+ json_location_iterator& operator--()
181
+ {
182
+ --it_;
183
+ return *this;
184
+ }
185
+
186
+ json_location_iterator operator--(int)
187
+ {
188
+ json_location_iterator temp = *this;
189
+ --*this;
190
+ return temp;
191
+ }
192
+
193
+ json_location_iterator& operator+=(const difference_type offset)
194
+ {
195
+ it_ += offset;
196
+ return *this;
197
+ }
198
+
199
+ json_location_iterator operator+(const difference_type offset) const
200
+ {
201
+ json_location_iterator temp = *this;
202
+ return temp += offset;
203
+ }
204
+
205
+ json_location_iterator& operator-=(const difference_type offset)
206
+ {
207
+ return *this += -offset;
208
+ }
209
+
210
+ json_location_iterator operator-(const difference_type offset) const
211
+ {
212
+ json_location_iterator temp = *this;
213
+ return temp -= offset;
214
+ }
215
+
216
+ difference_type operator-(const json_location_iterator& rhs) const noexcept
217
+ {
218
+ return it_ - rhs.it_;
219
+ }
220
+
221
+ reference operator[](const difference_type offset) const noexcept
222
+ {
223
+ return *(*(*this + offset));
224
+ }
225
+
226
+ bool operator==(const json_location_iterator& rhs) const noexcept
227
+ {
228
+ return it_ == rhs.it_;
229
+ }
230
+
231
+ bool operator!=(const json_location_iterator& rhs) const noexcept
232
+ {
233
+ return !(*this == rhs);
234
+ }
235
+
236
+ bool operator<(const json_location_iterator& rhs) const noexcept
237
+ {
238
+ return it_ < rhs.it_;
239
+ }
240
+
241
+ bool operator>(const json_location_iterator& rhs) const noexcept
242
+ {
243
+ return rhs < *this;
244
+ }
245
+
246
+ bool operator<=(const json_location_iterator& rhs) const noexcept
247
+ {
248
+ return !(rhs < *this);
249
+ }
250
+
251
+ bool operator>=(const json_location_iterator& rhs) const noexcept
252
+ {
253
+ return !(*this < rhs);
254
+ }
255
+
256
+ inline
257
+ friend json_location_iterator<Iterator> operator+(
258
+ difference_type offset, json_location_iterator<Iterator> next)
259
+ {
260
+ return next += offset;
261
+ }
262
+ };
263
+
264
+ } // namespace detail
265
+
266
+ template <class CharT>
267
+ class json_location
268
+ {
269
+ public:
270
+ using char_type = CharT;
271
+ using string_type = std::basic_string<CharT>;
272
+ using json_location_node_type = json_location_node<CharT>;
273
+ private:
274
+ std::vector<const json_location_node_type*> nodes_;
275
+ public:
276
+ using iterator = typename detail::json_location_iterator<typename std::vector<const json_location_node_type*>::iterator>;
277
+ using const_iterator = typename detail::json_location_iterator<typename std::vector<const json_location_node_type*>::const_iterator>;
278
+
279
+ json_location(const json_location_node_type& node)
280
+ {
281
+ const json_location_node_type* p = std::addressof(node);
282
+ do
283
+ {
284
+ nodes_.push_back(p);
285
+ p = p->parent_;
286
+ }
287
+ while (p != nullptr);
288
+
289
+ std::reverse(nodes_.begin(), nodes_.end());
290
+ }
291
+
292
+ iterator begin()
293
+ {
294
+ return iterator(nodes_.begin());
295
+ }
296
+
297
+ iterator end()
298
+ {
299
+ return iterator(nodes_.end());
300
+ }
301
+
302
+ const_iterator begin() const
303
+ {
304
+ return const_iterator(nodes_.begin());
305
+ }
306
+
307
+ const_iterator end() const
308
+ {
309
+ return const_iterator(nodes_.end());
310
+ }
311
+
312
+ const json_location_node_type& last() const
313
+ {
314
+ return *nodes_.back();
315
+ }
316
+
317
+ string_type to_string() const
318
+ {
319
+ string_type buffer;
320
+
321
+ for (const auto& node : nodes_)
322
+ {
323
+ switch (node->node_kind())
324
+ {
325
+ case json_location_node_kind::root:
326
+ buffer.append(node->name());
327
+ break;
328
+ case json_location_node_kind::name:
329
+ buffer.push_back('[');
330
+ buffer.push_back('\'');
331
+ for (auto c : node->name())
332
+ {
333
+ if (c == '\'')
334
+ {
335
+ buffer.push_back('\\');
336
+ buffer.push_back('\'');
337
+ }
338
+ else
339
+ {
340
+ buffer.push_back(c);
341
+ }
342
+ }
343
+ buffer.push_back('\'');
344
+ buffer.push_back(']');
345
+ break;
346
+ case json_location_node_kind::index:
347
+ buffer.push_back('[');
348
+ jsoncons::detail::from_integer(node->index(), buffer);
349
+ buffer.push_back(']');
350
+ break;
351
+ }
352
+ }
353
+
354
+ return buffer;
355
+ }
356
+
357
+ int compare(const json_location& other) const
358
+ {
359
+ if (this == &other)
360
+ {
361
+ return 0;
362
+ }
363
+
364
+ auto it1 = nodes_.begin();
365
+ auto it2 = other.nodes_.begin();
366
+ while (it1 != nodes_.end() && it2 != other.nodes_.end())
367
+ {
368
+ int diff = (*it1)->compare_node(*(*it2));
369
+ if (diff != 0)
370
+ {
371
+ return diff;
372
+ }
373
+ ++it1;
374
+ ++it2;
375
+ }
376
+ return (nodes_.size() < other.nodes_.size()) ? -1 : (nodes_.size() == other.nodes_.size()) ? 0 : 1;
377
+ }
378
+
379
+ std::size_t hash() const
380
+ {
381
+
382
+ auto it = nodes_.begin();
383
+ std::size_t hash = (*it).hash();
384
+ ++it;
385
+
386
+ while (it != nodes_.end())
387
+ {
388
+ hash += 17*(*it)->node_hash();
389
+ ++it;
390
+ }
391
+
392
+ return hash;
393
+ }
394
+
395
+ friend bool operator==(const json_location& lhs, const json_location& rhs)
396
+ {
397
+ return lhs.compare(rhs) == 0;
398
+ }
399
+
400
+ friend bool operator!=(const json_location& lhs, const json_location& rhs)
401
+ {
402
+ return !(lhs == rhs);
403
+ }
404
+
405
+ friend bool operator<(const json_location& lhs, const json_location& rhs)
406
+ {
407
+ return lhs.compare(rhs) < 0;
408
+ }
409
+ };
410
+
411
+ template <class Json>
412
+ Json* select(Json& root, const json_location<typename Json::char_type>& path)
413
+ {
414
+ Json* current = std::addressof(root);
415
+ for (const auto& json_location_node : path)
416
+ {
417
+ if (json_location_node.node_kind() == json_location_node_kind::index)
418
+ {
419
+ if (current->type() != json_type::array_value || json_location_node.index() >= current->size())
420
+ {
421
+ return nullptr;
422
+ }
423
+ current = std::addressof(current->at(json_location_node.index()));
424
+ }
425
+ else if (json_location_node.node_kind() == json_location_node_kind::name)
426
+ {
427
+ if (current->type() != json_type::object_value)
428
+ {
429
+ return nullptr;
430
+ }
431
+ auto it = current->find(json_location_node.name());
432
+ if (it == current->object_range().end())
433
+ {
434
+ return nullptr;
435
+ }
436
+ current = std::addressof(it->value());
437
+ }
438
+ }
439
+ return current;
440
+ }
441
+
442
+ } // namespace jsonpath
443
+ } // namespace jsoncons
444
+
445
+ #endif
@@ -0,0 +1,115 @@
1
+ // Copyright 2021 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_JSONPATH_JSON_QUERY_HPP
8
+ #define JSONCONS_JSONPATH_JSON_QUERY_HPP
9
+
10
+ #include <jsoncons/json.hpp>
11
+ #include <jsoncons_ext/jsonpath/jsonpath_expression.hpp>
12
+
13
+ namespace jsoncons {
14
+ namespace jsonpath {
15
+
16
+ template<class Json>
17
+ Json json_query(const Json& instance,
18
+ const typename Json::string_view_type& path,
19
+ result_options options = result_options(),
20
+ const custom_functions<Json>& functions = custom_functions<Json>())
21
+ {
22
+ auto expr = make_expression<Json>(path, functions);
23
+ return expr.evaluate(instance, options);
24
+ }
25
+
26
+ template<class Json,class Callback>
27
+ typename std::enable_if<traits_extension::is_binary_function_object<Callback,const std::basic_string<typename Json::char_type>&,const Json&>::value,void>::type
28
+ json_query(const Json& instance,
29
+ const typename Json::string_view_type& path,
30
+ Callback callback,
31
+ result_options options = result_options(),
32
+ const custom_functions<Json>& functions = custom_functions<Json>())
33
+ {
34
+ auto expr = make_expression<Json>(path, functions);
35
+ expr.evaluate(instance, callback, options);
36
+ }
37
+
38
+ template<class Json, class T>
39
+ typename std::enable_if<is_json_type_traits_specialized<Json,T>::value,void>::type
40
+ json_replace(Json& instance, const typename Json::string_view_type& path, T&& new_value,
41
+ result_options options = result_options::nodups,
42
+ const custom_functions<Json>& funcs = custom_functions<Json>())
43
+ {
44
+ using evaluator_t = typename jsoncons::jsonpath::detail::jsonpath_evaluator<Json, Json&>;
45
+ //using string_type = typename evaluator_t::string_type;
46
+ using value_type = typename evaluator_t::value_type;
47
+ using reference = typename evaluator_t::reference;
48
+ using json_selector_t = typename evaluator_t::path_expression_type;
49
+ using json_location_type = typename evaluator_t::json_location_type;
50
+
51
+ jsoncons::jsonpath::detail::static_resources<value_type,reference> static_resources(funcs);
52
+ evaluator_t e;
53
+ json_selector_t expr = e.compile(static_resources, path);
54
+
55
+ jsoncons::jsonpath::detail::dynamic_resources<Json,reference> resources;
56
+ auto callback = [&new_value](const json_location_type&, reference v)
57
+ {
58
+ v = std::forward<T>(new_value);
59
+ };
60
+ expr.evaluate(resources, instance, resources.root_path_node(), instance, callback, options);
61
+ }
62
+
63
+ template<class Json, class UnaryCallback>
64
+ typename std::enable_if<traits_extension::is_unary_function_object<UnaryCallback,Json>::value,void>::type
65
+ json_replace(Json& instance, const typename Json::string_view_type& path , UnaryCallback callback)
66
+ {
67
+ using evaluator_t = typename jsoncons::jsonpath::detail::jsonpath_evaluator<Json, Json&>;
68
+ //using string_type = typename evaluator_t::string_type;
69
+ using value_type = typename evaluator_t::value_type;
70
+ using reference = typename evaluator_t::reference;
71
+ using json_selector_t = typename evaluator_t::path_expression_type;
72
+ using json_location_type = typename evaluator_t::json_location_type;
73
+
74
+ jsoncons::jsonpath::detail::static_resources<value_type,reference> static_resources;
75
+ evaluator_t e;
76
+ json_selector_t expr = e.compile(static_resources, path);
77
+
78
+ jsoncons::jsonpath::detail::dynamic_resources<Json,reference> resources;
79
+ auto f = [callback](const json_location_type&, reference v)
80
+ {
81
+ v = callback(v);
82
+ };
83
+ expr.evaluate(resources, instance, resources.root_path_node(), instance, f, result_options::nodups);
84
+ }
85
+
86
+ template<class Json, class BinaryCallback>
87
+ typename std::enable_if<traits_extension::is_binary_function_object<BinaryCallback,const std::basic_string<typename Json::char_type>&,Json&>::value,void>::type
88
+ json_replace(Json& instance, const typename Json::string_view_type& path , BinaryCallback callback,
89
+ result_options options = result_options::nodups,
90
+ const custom_functions<Json>& funcs = custom_functions<Json>())
91
+ {
92
+ using evaluator_t = typename jsoncons::jsonpath::detail::jsonpath_evaluator<Json, Json&>;
93
+ //using string_type = typename evaluator_t::string_type;
94
+ using value_type = typename evaluator_t::value_type;
95
+ using reference = typename evaluator_t::reference;
96
+ using json_selector_t = typename evaluator_t::path_expression_type;
97
+ using json_location_type = typename evaluator_t::json_location_type;
98
+
99
+ jsoncons::jsonpath::detail::static_resources<value_type,reference> static_resources(funcs);
100
+ evaluator_t e;
101
+ json_selector_t expr = e.compile(static_resources, path);
102
+
103
+ jsoncons::jsonpath::detail::dynamic_resources<Json,reference> resources;
104
+
105
+ auto f = [&callback](const json_location_type& path, reference val)
106
+ {
107
+ callback(path.to_string(), val);
108
+ };
109
+ expr.evaluate(resources, instance, resources.root_path_node(), instance, f, options);
110
+ }
111
+
112
+ } // namespace jsonpath
113
+ } // namespace jsoncons
114
+
115
+ #endif
@@ -0,0 +1,13 @@
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_JSONPATH_JSONPATH_HPP
8
+ #define JSONCONS_JSONPATH_JSONPATH_HPP
9
+
10
+ #include <jsoncons_ext/jsonpath/json_query.hpp>
11
+ #include <jsoncons_ext/jsonpath/flatten.hpp>
12
+
13
+ #endif