simdjson  0.3.1
Ridiculously Fast JSON
parsedjson_iterator.h
1 // TODO Remove this -- deprecated API and files
2 
3 #ifndef SIMDJSON_PARSEDJSON_ITERATOR_H
4 #define SIMDJSON_PARSEDJSON_ITERATOR_H
5 
6 #include <cstring>
7 #include <string>
8 #include <iostream>
9 #include <iterator>
10 #include <limits>
11 #include <stdexcept>
12 
13 #include "simdjson/document.h"
14 #include "simdjson/parsedjson.h"
15 #include "simdjson/internal/jsonformatutils.h"
16 
17 namespace simdjson {
18 
19 class [[deprecated("Use the new DOM navigation API instead (see doc/usage.md)")]] dom::parser::Iterator {
20 public:
21  inline Iterator(const dom::parser &parser) noexcept(false);
22  inline Iterator(const Iterator &o) noexcept;
23  inline ~Iterator() noexcept;
24 
25  inline bool is_ok() const;
26 
27  // useful for debugging purposes
28  inline size_t get_tape_location() const;
29 
30  // useful for debugging purposes
31  inline size_t get_tape_length() const;
32 
33  // returns the current depth (start at 1 with 0 reserved for the fictitious
34  // root node)
35  inline size_t get_depth() const;
36 
37  // A scope is a series of nodes at the same depth, typically it is either an
38  // object ({) or an array ([). The root node has type 'r'.
39  inline uint8_t get_scope_type() const;
40 
41  // move forward in document order
42  inline bool move_forward();
43 
44  // retrieve the character code of what we're looking at:
45  // [{"slutfn are the possibilities
46  inline uint8_t get_type() const {
47  return current_type; // short functions should be inlined!
48  }
49 
50  // get the int64_t value at this node; valid only if get_type is "l"
51  inline int64_t get_integer() const {
52  if (location + 1 >= tape_length) {
53  return 0; // default value in case of error
54  }
55  return static_cast<int64_t>(doc.tape[location + 1]);
56  }
57 
58  // get the value as uint64; valid only if if get_type is "u"
59  inline uint64_t get_unsigned_integer() const {
60  if (location + 1 >= tape_length) {
61  return 0; // default value in case of error
62  }
63  return doc.tape[location + 1];
64  }
65 
66  // get the string value at this node (NULL ended); valid only if get_type is "
67  // note that tabs, and line endings are escaped in the returned value (see
68  // print_with_escapes) return value is valid UTF-8, it may contain NULL chars
69  // within the string: get_string_length determines the true string length.
70  inline const char *get_string() const {
71  return reinterpret_cast<const char *>(
72  doc.string_buf.get() + (current_val & internal::JSON_VALUE_MASK) + sizeof(uint32_t));
73  }
74 
75  // return the length of the string in bytes
76  inline uint32_t get_string_length() const {
77  uint32_t answer;
78  memcpy(&answer,
79  reinterpret_cast<const char *>(doc.string_buf.get() +
80  (current_val & internal::JSON_VALUE_MASK)),
81  sizeof(uint32_t));
82  return answer;
83  }
84 
85  // get the double value at this node; valid only if
86  // get_type() is "d"
87  inline double get_double() const {
88  if (location + 1 >= tape_length) {
89  return std::numeric_limits<double>::quiet_NaN(); // default value in
90  // case of error
91  }
92  double answer;
93  memcpy(&answer, &doc.tape[location + 1], sizeof(answer));
94  return answer;
95  }
96 
97  inline bool is_object_or_array() const { return is_object() || is_array(); }
98 
99  inline bool is_object() const { return get_type() == '{'; }
100 
101  inline bool is_array() const { return get_type() == '['; }
102 
103  inline bool is_string() const { return get_type() == '"'; }
104 
105  // Returns true if the current type of node is an signed integer.
106  // You can get its value with `get_integer()`.
107  inline bool is_integer() const { return get_type() == 'l'; }
108 
109  // Returns true if the current type of node is an unsigned integer.
110  // You can get its value with `get_unsigned_integer()`.
111  //
112  // NOTE:
113  // Only a large value, which is out of range of a 64-bit signed integer, is
114  // represented internally as an unsigned node. On the other hand, a typical
115  // positive integer, such as 1, 42, or 1000000, is as a signed node.
116  // Be aware this function returns false for a signed node.
117  inline bool is_unsigned_integer() const { return get_type() == 'u'; }
118 
119  inline bool is_double() const { return get_type() == 'd'; }
120 
121  inline bool is_number() const {
122  return is_integer() || is_unsigned_integer() || is_double();
123  }
124 
125  inline bool is_true() const { return get_type() == 't'; }
126 
127  inline bool is_false() const { return get_type() == 'f'; }
128 
129  inline bool is_null() const { return get_type() == 'n'; }
130 
131  static bool is_object_or_array(uint8_t type) {
132  return ((type == '[') || (type == '{'));
133  }
134 
135  // when at {, go one level deep, looking for a given key
136  // if successful, we are left pointing at the value,
137  // if not, we are still pointing at the object ({)
138  // (in case of repeated keys, this only finds the first one).
139  // We seek the key using C's strcmp so if your JSON strings contain
140  // NULL chars, this would trigger a false positive: if you expect that
141  // to be the case, take extra precautions.
142  // Furthermore, we do the comparison character-by-character
143  // without taking into account Unicode equivalence.
144  inline bool move_to_key(const char *key);
145 
146  // as above, but case insensitive lookup (strcmpi instead of strcmp)
147  inline bool move_to_key_insensitive(const char *key);
148 
149  // when at {, go one level deep, looking for a given key
150  // if successful, we are left pointing at the value,
151  // if not, we are still pointing at the object ({)
152  // (in case of repeated keys, this only finds the first one).
153  // The string we search for can contain NULL values.
154  // Furthermore, we do the comparison character-by-character
155  // without taking into account Unicode equivalence.
156  inline bool move_to_key(const char *key, uint32_t length);
157 
158  // when at a key location within an object, this moves to the accompanying
159  // value (located next to it). This is equivalent but much faster than
160  // calling "next()".
161  inline void move_to_value();
162 
163  // when at [, go one level deep, and advance to the given index.
164  // if successful, we are left pointing at the value,
165  // if not, we are still pointing at the array ([)
166  inline bool move_to_index(uint32_t index);
167 
168  // Moves the iterator to the value corresponding to the json pointer.
169  // Always search from the root of the document.
170  // if successful, we are left pointing at the value,
171  // if not, we are still pointing the same value we were pointing before the
172  // call. The json pointer follows the rfc6901 standard's syntax:
173  // https://tools.ietf.org/html/rfc6901 However, the standard says "If a
174  // referenced member name is not unique in an object, the member that is
175  // referenced is undefined, and evaluation fails". Here we just return the
176  // first corresponding value. The length parameter is the length of the
177  // jsonpointer string ('pointer').
178  inline bool move_to(const char *pointer, uint32_t length);
179 
180  // Moves the iterator to the value corresponding to the json pointer.
181  // Always search from the root of the document.
182  // if successful, we are left pointing at the value,
183  // if not, we are still pointing the same value we were pointing before the
184  // call. The json pointer implementation follows the rfc6901 standard's
185  // syntax: https://tools.ietf.org/html/rfc6901 However, the standard says
186  // "If a referenced member name is not unique in an object, the member that
187  // is referenced is undefined, and evaluation fails". Here we just return
188  // the first corresponding value.
189  inline bool move_to(const std::string &pointer) {
190  return move_to(pointer.c_str(), pointer.length());
191  }
192 
193  private:
194  // Almost the same as move_to(), except it searches from the current
195  // position. The pointer's syntax is identical, though that case is not
196  // handled by the rfc6901 standard. The '/' is still required at the
197  // beginning. However, contrary to move_to(), the URI Fragment Identifier
198  // Representation is not supported here. Also, in case of failure, we are
199  // left pointing at the closest value it could reach. For these reasons it
200  // is private. It exists because it is used by move_to().
201  inline bool relative_move_to(const char *pointer, uint32_t length);
202 
203  public:
204  // throughout return true if we can do the navigation, false
205  // otherwise
206 
207  // Withing a given scope (series of nodes at the same depth within either an
208  // array or an object), we move forward.
209  // Thus, given [true, null, {"a":1}, [1,2]], we would visit true, null, {
210  // and [. At the object ({) or at the array ([), you can issue a "down" to
211  // visit their content. valid if we're not at the end of a scope (returns
212  // true).
213  inline bool next();
214 
215  // Within a given scope (series of nodes at the same depth within either an
216  // array or an object), we move backward.
217  // Thus, given [true, null, {"a":1}, [1,2]], we would visit ], }, null, true
218  // when starting at the end of the scope. At the object ({) or at the array
219  // ([), you can issue a "down" to visit their content.
220  // Performance warning: This function is implemented by starting again
221  // from the beginning of the scope and scanning forward. You should expect
222  // it to be relatively slow.
223  inline bool prev();
224 
225  // Moves back to either the containing array or object (type { or [) from
226  // within a contained scope.
227  // Valid unless we are at the first level of the document
228  inline bool up();
229 
230  // Valid if we're at a [ or { and it starts a non-empty scope; moves us to
231  // start of that deeper scope if it not empty. Thus, given [true, null,
232  // {"a":1}, [1,2]], if we are at the { node, we would move to the "a" node.
233  inline bool down();
234 
235  // move us to the start of our current scope,
236  // a scope is a series of nodes at the same level
237  inline void to_start_scope();
238 
239  inline void rewind() {
240  while (up())
241  ;
242  }
243 
244  // void to_end_scope(); // move us to
245  // the start of our current scope; always succeeds
246 
247  // print the node we are currently pointing at
248  inline bool print(std::ostream &os, bool escape_strings = true) const;
249  typedef struct {
250  size_t start_of_scope;
251  uint8_t scope_type;
252  } scopeindex_t;
253 
254  private:
255  const document &doc;
256  size_t max_depth;
257  size_t depth;
258  size_t location; // our current location on a tape
259  size_t tape_length;
260  uint8_t current_type;
261  uint64_t current_val;
262  scopeindex_t *depth_index;
263 };
264 
265 } // namespace simdjson
266 
267 #endif
simdjson::dom::document
A parsed JSON document.
Definition: document.h:318
simdjson::dom::parser
A persistent document parser.
Definition: document.h:620
simdjson::dom::parser::Iterator::scopeindex_t
Definition: parsedjson_iterator.h:249
simdjson::dom::parser::Iterator
Definition: parsedjson_iterator.h:19