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