Skip to content

Commit a5ccff7

Browse files
authored
Merge pull request simdjson#949 from simdjson/jkeiser/get-type
Add non-template get_xxx/is_xxx methods to element
2 parents 2cc84b6 + 1d8c2d6 commit a5ccff7

File tree

10 files changed

+735
-368
lines changed

10 files changed

+735
-368
lines changed

include/simdjson/dom/array.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class element;
1616
/**
1717
* JSON array.
1818
*/
19-
class array : protected internal::tape_ref {
19+
class array {
2020
public:
2121
/** Create a new, invalid array */
2222
really_inline array() noexcept;
2323

24-
class iterator : protected internal::tape_ref {
24+
class iterator {
2525
public:
2626
/**
2727
* Get the actual value
@@ -41,7 +41,8 @@ class array : protected internal::tape_ref {
4141
*/
4242
inline bool operator!=(const iterator& other) const noexcept;
4343
private:
44-
really_inline iterator(const document *doc, size_t json_index) noexcept;
44+
really_inline iterator(const internal::tape_ref &tape) noexcept;
45+
internal::tape_ref tape;
4546
friend class array;
4647
};
4748

@@ -98,7 +99,8 @@ class array : protected internal::tape_ref {
9899
inline simdjson_result<element> at(size_t index) const noexcept;
99100

100101
private:
101-
really_inline array(const document *doc, size_t json_index) noexcept;
102+
really_inline array(const internal::tape_ref &tape) noexcept;
103+
internal::tape_ref tape;
102104
friend class element;
103105
friend struct simdjson_result<element>;
104106
template<typename T>

include/simdjson/dom/element.h

Lines changed: 150 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,143 @@ enum class element_type {
3535
* References an element in a JSON document, representing a JSON null, boolean, string, number,
3636
* array or object.
3737
*/
38-
class element : protected internal::tape_ref {
38+
class element {
3939
public:
4040
/** Create a new, invalid element. */
4141
really_inline element() noexcept;
4242

4343
/** The type of this element. */
4444
really_inline element_type type() const noexcept;
4545

46-
/** Whether this element is a json `null`. */
47-
really_inline bool is_null() const noexcept;
46+
/**
47+
* Cast this element to an array.
48+
*
49+
* Equivalent to get<array>().
50+
*
51+
* @returns An object that can be used to iterate the array, or:
52+
* INCORRECT_TYPE if the JSON element is not an array.
53+
*/
54+
inline simdjson_result<array> get_array() const noexcept;
55+
/**
56+
* Cast this element to an object.
57+
*
58+
* Equivalent to get<object>().
59+
*
60+
* @returns An object that can be used to look up or iterate the object's fields, or:
61+
* INCORRECT_TYPE if the JSON element is not an object.
62+
*/
63+
inline simdjson_result<object> get_object() const noexcept;
64+
/**
65+
* Cast this element to a string.
66+
*
67+
* Equivalent to get<const char *>().
68+
*
69+
* @returns An pointer to a null-terminated string. This string is stored in the parser and will
70+
* be invalidated the next time it parses a document or when it is destroyed.
71+
* Returns INCORRECT_TYPE if the JSON element is not a string.
72+
*/
73+
inline simdjson_result<const char *> get_c_str() const noexcept;
74+
/**
75+
* Cast this element to a string.
76+
*
77+
* Equivalent to get<std::string_view>().
78+
*
79+
* @returns A string. The string is stored in the parser and will be invalidated the next time it
80+
* parses a document or when it is destroyed.
81+
* Returns INCORRECT_TYPE if the JSON element is not a string.
82+
*/
83+
inline simdjson_result<std::string_view> get_string() const noexcept;
84+
/**
85+
* Cast this element to a signed integer.
86+
*
87+
* Equivalent to get<int64_t>().
88+
*
89+
* @returns A signed 64-bit integer.
90+
* Returns INCORRECT_TYPE if the JSON element is not an integer, or NUMBER_OUT_OF_RANGE
91+
* if it is negative.
92+
*/
93+
inline simdjson_result<int64_t> get_int64_t() const noexcept;
94+
/**
95+
* Cast this element to an unsigned integer.
96+
*
97+
* Equivalent to get<uint64_t>().
98+
*
99+
* @returns An unsigned 64-bit integer.
100+
* Returns INCORRECT_TYPE if the JSON element is not an integer, or NUMBER_OUT_OF_RANGE
101+
* if it is too large.
102+
*/
103+
inline simdjson_result<uint64_t> get_uint64_t() const noexcept;
104+
/**
105+
* Cast this element to an double floating-point.
106+
*
107+
* Equivalent to get<double>().
108+
*
109+
* @returns A double value.
110+
* Returns INCORRECT_TYPE if the JSON element is not a number.
111+
*/
112+
inline simdjson_result<double> get_double() const noexcept;
113+
/**
114+
* Cast this element to a bool.
115+
*
116+
* Equivalent to get<bool>().
117+
*
118+
* @returns A bool value.
119+
* Returns INCORRECT_TYPE if the JSON element is not a boolean.
120+
*/
121+
inline simdjson_result<bool> get_bool() const noexcept;
122+
123+
/**
124+
* Whether this element is a json array.
125+
*
126+
* Equivalent to is<array>().
127+
*/
128+
inline bool is_array() const noexcept;
129+
/**
130+
* Whether this element is a json object.
131+
*
132+
* Equivalent to is<object>().
133+
*/
134+
inline bool is_object() const noexcept;
135+
/**
136+
* Whether this element is a json string.
137+
*
138+
* Equivalent to is<std::string_view>() or is<const char *>().
139+
*/
140+
inline bool is_string() const noexcept;
141+
/**
142+
* Whether this element is a json number that fits in a signed 64-bit integer.
143+
*
144+
* Equivalent to is<int64_t>().
145+
*/
146+
inline bool is_int64_t() const noexcept;
147+
/**
148+
* Whether this element is a json number that fits in an unsigned 64-bit integer.
149+
*
150+
* Equivalent to is<uint64_t>().
151+
*/
152+
inline bool is_uint64_t() const noexcept;
153+
/**
154+
* Whether this element is a json number that fits in a double.
155+
*
156+
* Equivalent to is<double>().
157+
*/
158+
inline bool is_double() const noexcept;
159+
/**
160+
* Whether this element is a json number.
161+
*
162+
* Both integers and floating points will return true.
163+
*/
164+
inline bool is_number() const noexcept;
165+
/**
166+
* Whether this element is a json `true` or `false`.
167+
*
168+
* Equivalent to is<bool>().
169+
*/
170+
inline bool is_bool() const noexcept;
171+
/**
172+
* Whether this element is a json `null`.
173+
*/
174+
inline bool is_null() const noexcept;
48175

49176
/**
50177
* Tell whether the value can be cast to provided type (T).
@@ -249,7 +376,8 @@ class element : protected internal::tape_ref {
249376
inline bool dump_raw_tape(std::ostream &out) const noexcept;
250377

251378
private:
252-
really_inline element(const document *doc, size_t json_index) noexcept;
379+
really_inline element(const internal::tape_ref &tape) noexcept;
380+
internal::tape_ref tape;
253381
friend class document;
254382
friend class object;
255383
friend class array;
@@ -289,12 +417,29 @@ struct simdjson_result<dom::element> : public internal::simdjson_result_base<dom
289417
really_inline simdjson_result(error_code error) noexcept; ///< @private
290418

291419
inline simdjson_result<dom::element_type> type() const noexcept;
292-
inline simdjson_result<bool> is_null() const noexcept;
293420
template<typename T>
294421
inline simdjson_result<bool> is() const noexcept;
295422
template<typename T>
296423
inline simdjson_result<T> get() const noexcept;
297424

425+
inline simdjson_result<dom::array> get_array() const noexcept;
426+
inline simdjson_result<dom::object> get_object() const noexcept;
427+
inline simdjson_result<const char *> get_c_str() const noexcept;
428+
inline simdjson_result<std::string_view> get_string() const noexcept;
429+
inline simdjson_result<int64_t> get_int64_t() const noexcept;
430+
inline simdjson_result<uint64_t> get_uint64_t() const noexcept;
431+
inline simdjson_result<double> get_double() const noexcept;
432+
inline simdjson_result<bool> get_bool() const noexcept;
433+
434+
inline simdjson_result<bool> is_array() const noexcept;
435+
inline simdjson_result<bool> is_object() const noexcept;
436+
inline simdjson_result<bool> is_string() const noexcept;
437+
inline simdjson_result<bool> is_int64_t() const noexcept;
438+
inline simdjson_result<bool> is_uint64_t() const noexcept;
439+
inline simdjson_result<bool> is_double() const noexcept;
440+
inline simdjson_result<bool> is_bool() const noexcept;
441+
inline simdjson_result<bool> is_null() const noexcept;
442+
298443
inline simdjson_result<dom::element> operator[](const std::string_view &key) const noexcept;
299444
inline simdjson_result<dom::element> operator[](const char *key) const noexcept;
300445
inline simdjson_result<dom::element> at(const std::string_view &json_pointer) const noexcept;

include/simdjson/dom/object.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class key_value_pair;
1717
/**
1818
* JSON object.
1919
*/
20-
class object : protected internal::tape_ref {
20+
class object {
2121
public:
2222
/** Create a new, invalid object */
2323
really_inline object() noexcept;
2424

25-
class iterator : protected internal::tape_ref {
25+
class iterator {
2626
public:
2727
/**
2828
* Get the actual key/value pair
@@ -70,7 +70,10 @@ class object : protected internal::tape_ref {
7070
*/
7171
inline element value() const noexcept;
7272
private:
73-
really_inline iterator(const document *doc, size_t json_index) noexcept;
73+
really_inline iterator(const internal::tape_ref &tape) noexcept;
74+
75+
internal::tape_ref tape;
76+
7477
friend class object;
7578
};
7679

@@ -172,7 +175,10 @@ class object : protected internal::tape_ref {
172175
inline simdjson_result<element> at_key_case_insensitive(const std::string_view &key) const noexcept;
173176

174177
private:
175-
really_inline object(const document *doc, size_t json_index) noexcept;
178+
really_inline object(const internal::tape_ref &tape) noexcept;
179+
180+
internal::tape_ref tape;
181+
176182
friend class element;
177183
friend struct simdjson_result<element>;
178184
template<typename T>

include/simdjson/inline/array.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ namespace dom {
5050
//
5151
// array inline implementation
5252
//
53-
really_inline array::array() noexcept : internal::tape_ref() {}
54-
really_inline array::array(const document *_doc, size_t _json_index) noexcept : internal::tape_ref(_doc, _json_index) {}
53+
really_inline array::array() noexcept : tape{} {}
54+
really_inline array::array(const internal::tape_ref &_tape) noexcept : tape{_tape} {}
5555
inline array::iterator array::begin() const noexcept {
56-
return iterator(doc, json_index + 1);
56+
return internal::tape_ref(tape.doc, tape.json_index + 1);
5757
}
5858
inline array::iterator array::end() const noexcept {
59-
return iterator(doc, after_element() - 1);
59+
return internal::tape_ref(tape.doc, tape.after_element() - 1);
6060
}
6161
inline size_t array::size() const noexcept {
62-
return scope_count();
62+
return tape.scope_count();
6363
}
6464
inline simdjson_result<element> array::at(const std::string_view &json_pointer) const noexcept {
6565
// - means "the append position" or "the element after the end of the array"
@@ -83,7 +83,7 @@ inline simdjson_result<element> array::at(const std::string_view &json_pointer)
8383
if (i == 0) { return INVALID_JSON_POINTER; } // "Empty string in JSON pointer array index"
8484

8585
// Get the child
86-
auto child = array(doc, json_index).at(array_index);
86+
auto child = array(tape).at(array_index);
8787
// If there is a /, we're not done yet, call recursively.
8888
if (i < json_pointer.length()) {
8989
child = child.at(json_pointer.substr(i+1));
@@ -102,15 +102,15 @@ inline simdjson_result<element> array::at(size_t index) const noexcept {
102102
//
103103
// array::iterator inline implementation
104104
//
105-
really_inline array::iterator::iterator(const document *_doc, size_t _json_index) noexcept : internal::tape_ref(_doc, _json_index) { }
105+
really_inline array::iterator::iterator(const internal::tape_ref &_tape) noexcept : tape{_tape} { }
106106
inline element array::iterator::operator*() const noexcept {
107-
return element(doc, json_index);
107+
return element(tape);
108108
}
109109
inline bool array::iterator::operator!=(const array::iterator& other) const noexcept {
110-
return json_index != other.json_index;
110+
return tape.json_index != other.tape.json_index;
111111
}
112112
inline array::iterator& array::iterator::operator++() noexcept {
113-
json_index = after_element();
113+
tape.json_index = tape.after_element();
114114
return *this;
115115
}
116116

include/simdjson/inline/document.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace dom {
1717
// document inline implementation
1818
//
1919
inline element document::root() const noexcept {
20-
return element(this, 1);
20+
return element(internal::tape_ref(this, 1));
2121
}
2222

2323
WARN_UNUSED

0 commit comments

Comments
 (0)