Skip to content

Commit 0b8c357

Browse files
committed
Add get_X and is_X methods
1 parent efc168f commit 0b8c357

4 files changed

Lines changed: 352 additions & 126 deletions

File tree

include/simdjson/dom/element.h

Lines changed: 147 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,135 @@ class element {
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).
@@ -290,12 +417,29 @@ struct simdjson_result<dom::element> : public internal::simdjson_result_base<dom
290417
really_inline simdjson_result(error_code error) noexcept; ///< @private
291418

292419
inline simdjson_result<dom::element_type> type() const noexcept;
293-
inline simdjson_result<bool> is_null() const noexcept;
294420
template<typename T>
295421
inline simdjson_result<bool> is() const noexcept;
296422
template<typename T>
297423
inline simdjson_result<T> get() const noexcept;
298424

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+
299443
inline simdjson_result<dom::element> operator[](const std::string_view &key) const noexcept;
300444
inline simdjson_result<dom::element> operator[](const char *key) const noexcept;
301445
inline simdjson_result<dom::element> at(const std::string_view &json_pointer) const noexcept;

include/simdjson/inline/element.h

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ inline simdjson_result<dom::element_type> simdjson_result<dom::element>::type()
2222
if (error()) { return error(); }
2323
return first.type();
2424
}
25-
inline simdjson_result<bool> simdjson_result<dom::element>::is_null() const noexcept {
26-
if (error()) { return error(); }
27-
return first.is_null();
28-
}
25+
2926
template<typename T>
3027
inline simdjson_result<bool> simdjson_result<dom::element>::is() const noexcept {
3128
if (error()) { return error(); }
@@ -37,6 +34,73 @@ inline simdjson_result<T> simdjson_result<dom::element>::get() const noexcept {
3734
return first.get<T>();
3835
}
3936

37+
inline simdjson_result<dom::array> simdjson_result<dom::element>::get_array() const noexcept {
38+
if (error()) { return error(); }
39+
return first.get_array();
40+
}
41+
inline simdjson_result<dom::object> simdjson_result<dom::element>::get_object() const noexcept {
42+
if (error()) { return error(); }
43+
return first.get_object();
44+
}
45+
inline simdjson_result<const char *> simdjson_result<dom::element>::get_c_str() const noexcept {
46+
if (error()) { return error(); }
47+
return first.get_c_str();
48+
}
49+
inline simdjson_result<std::string_view> simdjson_result<dom::element>::get_string() const noexcept {
50+
if (error()) { return error(); }
51+
return first.get_string();
52+
}
53+
inline simdjson_result<int64_t> simdjson_result<dom::element>::get_int64_t() const noexcept {
54+
if (error()) { return error(); }
55+
return first.get_int64_t();
56+
}
57+
inline simdjson_result<uint64_t> simdjson_result<dom::element>::get_uint64_t() const noexcept {
58+
if (error()) { return error(); }
59+
return first.get_uint64_t();
60+
}
61+
inline simdjson_result<double> simdjson_result<dom::element>::get_double() const noexcept {
62+
if (error()) { return error(); }
63+
return first.get_double();
64+
}
65+
inline simdjson_result<bool> simdjson_result<dom::element>::get_bool() const noexcept {
66+
if (error()) { return error(); }
67+
return first.get_bool();
68+
}
69+
70+
inline simdjson_result<bool> simdjson_result<dom::element>::is_array() const noexcept {
71+
if (error()) { return error(); }
72+
return first.is_array();
73+
}
74+
inline simdjson_result<bool> simdjson_result<dom::element>::is_object() const noexcept {
75+
if (error()) { return error(); }
76+
return first.is_object();
77+
}
78+
inline simdjson_result<bool> simdjson_result<dom::element>::is_string() const noexcept {
79+
if (error()) { return error(); }
80+
return first.is_string();
81+
}
82+
inline simdjson_result<bool> simdjson_result<dom::element>::is_int64_t() const noexcept {
83+
if (error()) { return error(); }
84+
return first.is_int64_t();
85+
}
86+
inline simdjson_result<bool> simdjson_result<dom::element>::is_uint64_t() const noexcept {
87+
if (error()) { return error(); }
88+
return first.is_uint64_t();
89+
}
90+
inline simdjson_result<bool> simdjson_result<dom::element>::is_double() const noexcept {
91+
if (error()) { return error(); }
92+
return first.is_double();
93+
}
94+
inline simdjson_result<bool> simdjson_result<dom::element>::is_bool() const noexcept {
95+
if (error()) { return error(); }
96+
return first.is_bool();
97+
}
98+
99+
inline simdjson_result<bool> simdjson_result<dom::element>::is_null() const noexcept {
100+
if (error()) { return error(); }
101+
return first.is_null();
102+
}
103+
40104
inline simdjson_result<dom::element> simdjson_result<dom::element>::operator[](const std::string_view &key) const noexcept {
41105
if (error()) { return error(); }
42106
return first[key];
@@ -112,9 +176,6 @@ inline element_type element::type() const noexcept {
112176
auto tape_type = tape.tape_ref_type();
113177
return tape_type == internal::tape_type::FALSE_VALUE ? element_type::BOOL : static_cast<element_type>(tape_type);
114178
}
115-
really_inline bool element::is_null() const noexcept {
116-
return tape.is_null_on_tape();
117-
}
118179

119180
template<>
120181
inline simdjson_result<bool> element::get<bool>() const noexcept {
@@ -215,11 +276,32 @@ inline simdjson_result<object> element::get<object>() const noexcept {
215276
}
216277

217278
template<typename T>
218-
really_inline bool element::is() const noexcept {
279+
inline bool element::is() const noexcept {
219280
auto result = get<T>();
220281
return !result.error();
221282
}
222283

284+
inline simdjson_result<array> element::get_array() const noexcept { return get<array>(); }
285+
inline simdjson_result<object> element::get_object() const noexcept { return get<object>(); }
286+
inline simdjson_result<const char *> element::get_c_str() const noexcept { return get<const char *>(); }
287+
inline simdjson_result<std::string_view> element::get_string() const noexcept { return get<std::string_view>(); }
288+
inline simdjson_result<int64_t> element::get_int64_t() const noexcept { return get<int64_t>(); }
289+
inline simdjson_result<uint64_t> element::get_uint64_t() const noexcept { return get<uint64_t>(); }
290+
inline simdjson_result<double> element::get_double() const noexcept { return get<double>(); }
291+
inline simdjson_result<bool> element::get_bool() const noexcept { return get<bool>(); }
292+
293+
inline bool element::is_array() const noexcept { return is<array>(); }
294+
inline bool element::is_object() const noexcept { return is<object>(); }
295+
inline bool element::is_string() const noexcept { return is<std::string_view>(); }
296+
inline bool element::is_int64_t() const noexcept { return is<int64_t>(); }
297+
inline bool element::is_uint64_t() const noexcept { return is<uint64_t>(); }
298+
inline bool element::is_double() const noexcept { return is<double>(); }
299+
inline bool element::is_bool() const noexcept { return is<bool>(); }
300+
301+
inline bool element::is_null() const noexcept {
302+
return tape.is_null_on_tape();
303+
}
304+
223305
#if SIMDJSON_EXCEPTIONS
224306

225307
inline element::operator bool() const noexcept(false) { return get<bool>(); }

tests/basictests.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,12 +1370,12 @@ namespace type_tests {
13701370

13711371
RUN_TEST( tester.test_get(element) );
13721372
RUN_TEST( tester.test_get(result) );
1373-
// RUN_TEST( tester.test_named_get(element) );
1374-
// RUN_TEST( tester.test_named_get(result) );
1373+
RUN_TEST( tester.test_named_get(element) );
1374+
RUN_TEST( tester.test_named_get(result) );
13751375
RUN_TEST( tester.test_is(element, true) );
13761376
RUN_TEST( tester.test_is(result, true) );
1377-
// RUN_TEST( tester.test_named_is(element, true) );
1378-
// RUN_TEST( tester.test_named_is(result, true) );
1377+
RUN_TEST( tester.test_named_is(element, true) );
1378+
RUN_TEST( tester.test_named_is(result, true) );
13791379
#if SIMDJSON_EXCEPTIONS
13801380
RUN_TEST( tester.test_implicit_cast(element) );
13811381
RUN_TEST( tester.test_implicit_cast(result) );
@@ -1396,12 +1396,12 @@ namespace type_tests {
13961396

13971397
RUN_TEST( tester.test_get_error(element, expected_error) );
13981398
RUN_TEST( tester.test_get_error(result, expected_error) );
1399-
// RUN_TEST( tester.test_named_get_error(element, expected_error) );
1400-
// RUN_TEST( tester.test_named_get_error(result, expected_error) );
1399+
RUN_TEST( tester.test_named_get_error(element, expected_error) );
1400+
RUN_TEST( tester.test_named_get_error(result, expected_error) );
14011401
RUN_TEST( tester.test_is(element, false) );
14021402
RUN_TEST( tester.test_is(result, false) );
1403-
// RUN_TEST( tester.test_named_is(element, false) );
1404-
// RUN_TEST( tester.test_named_is(result, false) );
1403+
RUN_TEST( tester.test_named_is(element, false) );
1404+
RUN_TEST( tester.test_named_is(result, false) );
14051405
#if SIMDJSON_EXCEPTIONS
14061406
RUN_TEST( tester.test_implicit_cast_error(element, expected_error) );
14071407
RUN_TEST( tester.test_implicit_cast_error(result, expected_error) );

0 commit comments

Comments
 (0)