Fixing issue 961

This commit is contained in:
Daniel Lemire 2020-06-23 10:47:32 -04:00
parent aa4340ef5c
commit 696b0e29e4
4 changed files with 31 additions and 32 deletions

View File

@ -77,8 +77,7 @@ Once you have an element, you can navigate it with idiomatic C++ iterators, oper
* **Extracting Values:** You can cast a JSON element to a native type: `double(element)` or * **Extracting Values:** You can cast a JSON element to a native type: `double(element)` or
`double x = json_element`. This works for double, uint64_t, int64_t, bool, `double x = json_element`. This works for double, uint64_t, int64_t, bool,
dom::object and dom::array. An exception is thrown if the cast is not possible. You can also use is<*typename*>() to test if it is a dom::object and dom::array. An exception is thrown if the cast is not possible. You can also use is<*typename*>() to test if it is a given type, or use the equivalent without templates (`is_uint64()`, `is_uint64()`, `is_number()`, `is_double()`, , `is_bool()`, `is_integer()`, `is_string()`, `is_array()`, `is_object()`) or use the `type()` method: e.g., `element.type() == dom::element_type::DOUBLE`. Instead of casting, you can use get<*typename*>() to get the value: casts and get<*typename*>() can be used interchangeably. You can also use equivalent functions without templates: e.g., `get_string()`, `get_int64()`, `get_uint64()`, `get_double()` and `get_bool()`. You can use a variant usage of get<*typename*>() with error codes to avoid exceptions: e.g.,
given type, or use the `type()` method: e.g., `element.type() == dom::element_type::DOUBLE`. Instead of casting, you can use get<*typename*>() to get the value: casts and get<*typename*>() can be used interchangeably. You can use a variant usage of get<*typename*>() with error codes to avoid exceptions: e.g.,
```c++ ```c++
simdjson::error_code error; simdjson::error_code error;
simdjson::padded_string numberstring = "1.2"_padded; // our JSON input ("1.2") simdjson::padded_string numberstring = "1.2"_padded; // our JSON input ("1.2")

View File

@ -90,7 +90,7 @@ public:
* Returns INCORRECT_TYPE if the JSON element is not an integer, or NUMBER_OUT_OF_RANGE * Returns INCORRECT_TYPE if the JSON element is not an integer, or NUMBER_OUT_OF_RANGE
* if it is negative. * if it is negative.
*/ */
inline simdjson_result<int64_t> get_int64_t() const noexcept; inline simdjson_result<int64_t> get_int64() const noexcept;
/** /**
* Cast this element to an unsigned integer. * Cast this element to an unsigned integer.
* *
@ -100,7 +100,7 @@ public:
* Returns INCORRECT_TYPE if the JSON element is not an integer, or NUMBER_OUT_OF_RANGE * Returns INCORRECT_TYPE if the JSON element is not an integer, or NUMBER_OUT_OF_RANGE
* if it is too large. * if it is too large.
*/ */
inline simdjson_result<uint64_t> get_uint64_t() const noexcept; inline simdjson_result<uint64_t> get_uint64() const noexcept;
/** /**
* Cast this element to an double floating-point. * Cast this element to an double floating-point.
* *
@ -143,13 +143,13 @@ public:
* *
* Equivalent to is<int64_t>(). * Equivalent to is<int64_t>().
*/ */
inline bool is_int64_t() const noexcept; inline bool is_int64() const noexcept;
/** /**
* Whether this element is a json number that fits in an unsigned 64-bit integer. * Whether this element is a json number that fits in an unsigned 64-bit integer.
* *
* Equivalent to is<uint64_t>(). * Equivalent to is<uint64_t>().
*/ */
inline bool is_uint64_t() const noexcept; inline bool is_uint64() const noexcept;
/** /**
* Whether this element is a json number that fits in a double. * Whether this element is a json number that fits in a double.
* *
@ -484,16 +484,16 @@ public:
really_inline simdjson_result<dom::object> get_object() const noexcept; really_inline simdjson_result<dom::object> get_object() const noexcept;
really_inline simdjson_result<const char *> get_c_str() const noexcept; really_inline simdjson_result<const char *> get_c_str() const noexcept;
really_inline simdjson_result<std::string_view> get_string() const noexcept; really_inline simdjson_result<std::string_view> get_string() const noexcept;
really_inline simdjson_result<int64_t> get_int64_t() const noexcept; really_inline simdjson_result<int64_t> get_int64() const noexcept;
really_inline simdjson_result<uint64_t> get_uint64_t() const noexcept; really_inline simdjson_result<uint64_t> get_uint64() const noexcept;
really_inline simdjson_result<double> get_double() const noexcept; really_inline simdjson_result<double> get_double() const noexcept;
really_inline simdjson_result<bool> get_bool() const noexcept; really_inline simdjson_result<bool> get_bool() const noexcept;
really_inline simdjson_result<bool> is_array() const noexcept; really_inline simdjson_result<bool> is_array() const noexcept;
really_inline simdjson_result<bool> is_object() const noexcept; really_inline simdjson_result<bool> is_object() const noexcept;
really_inline simdjson_result<bool> is_string() const noexcept; really_inline simdjson_result<bool> is_string() const noexcept;
really_inline simdjson_result<bool> is_int64_t() const noexcept; really_inline simdjson_result<bool> is_int64() const noexcept;
really_inline simdjson_result<bool> is_uint64_t() const noexcept; really_inline simdjson_result<bool> is_uint64() const noexcept;
really_inline simdjson_result<bool> is_double() const noexcept; really_inline simdjson_result<bool> is_double() const noexcept;
really_inline simdjson_result<bool> is_bool() const noexcept; really_inline simdjson_result<bool> is_bool() const noexcept;
really_inline simdjson_result<bool> is_null() const noexcept; really_inline simdjson_result<bool> is_null() const noexcept;

View File

@ -55,13 +55,13 @@ really_inline simdjson_result<std::string_view> simdjson_result<dom::element>::g
if (error()) { return error(); } if (error()) { return error(); }
return first.get_string(); return first.get_string();
} }
really_inline simdjson_result<int64_t> simdjson_result<dom::element>::get_int64_t() const noexcept { really_inline simdjson_result<int64_t> simdjson_result<dom::element>::get_int64() const noexcept {
if (error()) { return error(); } if (error()) { return error(); }
return first.get_int64_t(); return first.get_int64();
} }
really_inline simdjson_result<uint64_t> simdjson_result<dom::element>::get_uint64_t() const noexcept { really_inline simdjson_result<uint64_t> simdjson_result<dom::element>::get_uint64() const noexcept {
if (error()) { return error(); } if (error()) { return error(); }
return first.get_uint64_t(); return first.get_uint64();
} }
really_inline simdjson_result<double> simdjson_result<dom::element>::get_double() const noexcept { really_inline simdjson_result<double> simdjson_result<dom::element>::get_double() const noexcept {
if (error()) { return error(); } if (error()) { return error(); }
@ -84,13 +84,13 @@ really_inline simdjson_result<bool> simdjson_result<dom::element>::is_string() c
if (error()) { return error(); } if (error()) { return error(); }
return first.is_string(); return first.is_string();
} }
really_inline simdjson_result<bool> simdjson_result<dom::element>::is_int64_t() const noexcept { really_inline simdjson_result<bool> simdjson_result<dom::element>::is_int64() const noexcept {
if (error()) { return error(); } if (error()) { return error(); }
return first.is_int64_t(); return first.is_int64();
} }
really_inline simdjson_result<bool> simdjson_result<dom::element>::is_uint64_t() const noexcept { really_inline simdjson_result<bool> simdjson_result<dom::element>::is_uint64() const noexcept {
if (error()) { return error(); } if (error()) { return error(); }
return first.is_uint64_t(); return first.is_uint64();
} }
really_inline simdjson_result<bool> simdjson_result<dom::element>::is_double() const noexcept { really_inline simdjson_result<bool> simdjson_result<dom::element>::is_double() const noexcept {
if (error()) { return error(); } if (error()) { return error(); }
@ -207,7 +207,7 @@ inline simdjson_result<std::string_view> element::get_string() const noexcept {
return INCORRECT_TYPE; return INCORRECT_TYPE;
} }
} }
inline simdjson_result<uint64_t> element::get_uint64_t() const noexcept { inline simdjson_result<uint64_t> element::get_uint64() const noexcept {
if(unlikely(!tape.is_uint64())) { // branch rarely taken if(unlikely(!tape.is_uint64())) { // branch rarely taken
if(tape.is_int64()) { if(tape.is_int64()) {
int64_t result = tape.next_tape_value<int64_t>(); int64_t result = tape.next_tape_value<int64_t>();
@ -220,7 +220,7 @@ inline simdjson_result<uint64_t> element::get_uint64_t() const noexcept {
} }
return tape.next_tape_value<int64_t>(); return tape.next_tape_value<int64_t>();
} }
inline simdjson_result<int64_t> element::get_int64_t() const noexcept { inline simdjson_result<int64_t> element::get_int64() const noexcept {
if(unlikely(!tape.is_int64())) { // branch rarely taken if(unlikely(!tape.is_int64())) { // branch rarely taken
if(tape.is_uint64()) { if(tape.is_uint64()) {
uint64_t result = tape.next_tape_value<uint64_t>(); uint64_t result = tape.next_tape_value<uint64_t>();
@ -293,16 +293,16 @@ template<> inline simdjson_result<array> element::get<array>() const noexcept {
template<> inline simdjson_result<object> element::get<object>() const noexcept { return get_object(); } template<> inline simdjson_result<object> element::get<object>() const noexcept { return get_object(); }
template<> inline simdjson_result<const char *> element::get<const char *>() const noexcept { return get_c_str(); } template<> inline simdjson_result<const char *> element::get<const char *>() const noexcept { return get_c_str(); }
template<> inline simdjson_result<std::string_view> element::get<std::string_view>() const noexcept { return get_string(); } template<> inline simdjson_result<std::string_view> element::get<std::string_view>() const noexcept { return get_string(); }
template<> inline simdjson_result<int64_t> element::get<int64_t>() const noexcept { return get_int64_t(); } template<> inline simdjson_result<int64_t> element::get<int64_t>() const noexcept { return get_int64(); }
template<> inline simdjson_result<uint64_t> element::get<uint64_t>() const noexcept { return get_uint64_t(); } template<> inline simdjson_result<uint64_t> element::get<uint64_t>() const noexcept { return get_uint64(); }
template<> inline simdjson_result<double> element::get<double>() const noexcept { return get_double(); } template<> inline simdjson_result<double> element::get<double>() const noexcept { return get_double(); }
template<> inline simdjson_result<bool> element::get<bool>() const noexcept { return get_bool(); } template<> inline simdjson_result<bool> element::get<bool>() const noexcept { return get_bool(); }
inline bool element::is_array() const noexcept { return is<array>(); } inline bool element::is_array() const noexcept { return is<array>(); }
inline bool element::is_object() const noexcept { return is<object>(); } inline bool element::is_object() const noexcept { return is<object>(); }
inline bool element::is_string() const noexcept { return is<std::string_view>(); } inline bool element::is_string() const noexcept { return is<std::string_view>(); }
inline bool element::is_int64_t() const noexcept { return is<int64_t>(); } inline bool element::is_int64() const noexcept { return is<int64_t>(); }
inline bool element::is_uint64_t() const noexcept { return is<uint64_t>(); } inline bool element::is_uint64() const noexcept { return is<uint64_t>(); }
inline bool element::is_double() const noexcept { return is<double>(); } inline bool element::is_double() const noexcept { return is<double>(); }
inline bool element::is_bool() const noexcept { return is<bool>(); } inline bool element::is_bool() const noexcept { return is<bool>(); }

View File

@ -229,8 +229,8 @@ template<> simdjson_result<array> cast_tester<array>::named_get(element element)
template<> simdjson_result<object> cast_tester<object>::named_get(element element) { return element.get_object(); } template<> simdjson_result<object> cast_tester<object>::named_get(element element) { return element.get_object(); }
template<> simdjson_result<const char *> cast_tester<const char *>::named_get(element element) { return element.get_c_str(); } template<> simdjson_result<const char *> cast_tester<const char *>::named_get(element element) { return element.get_c_str(); }
template<> simdjson_result<std::string_view> cast_tester<std::string_view>::named_get(element element) { return element.get_string(); } template<> simdjson_result<std::string_view> cast_tester<std::string_view>::named_get(element element) { return element.get_string(); }
template<> simdjson_result<uint64_t> cast_tester<uint64_t>::named_get(element element) { return element.get_uint64_t(); } template<> simdjson_result<uint64_t> cast_tester<uint64_t>::named_get(element element) { return element.get_uint64(); }
template<> simdjson_result<int64_t> cast_tester<int64_t>::named_get(element element) { return element.get_int64_t(); } template<> simdjson_result<int64_t> cast_tester<int64_t>::named_get(element element) { return element.get_int64(); }
template<> simdjson_result<double> cast_tester<double>::named_get(element element) { return element.get_double(); } template<> simdjson_result<double> cast_tester<double>::named_get(element element) { return element.get_double(); }
template<> simdjson_result<bool> cast_tester<bool>::named_get(element element) { return element.get_bool(); } template<> simdjson_result<bool> cast_tester<bool>::named_get(element element) { return element.get_bool(); }
@ -238,8 +238,8 @@ template<> simdjson_result<array> cast_tester<array>::named_get(simdjson_result<
template<> simdjson_result<object> cast_tester<object>::named_get(simdjson_result<element> element) { return element.get_object(); } template<> simdjson_result<object> cast_tester<object>::named_get(simdjson_result<element> element) { return element.get_object(); }
template<> simdjson_result<const char *> cast_tester<const char *>::named_get(simdjson_result<element> element) { return element.get_c_str(); } template<> simdjson_result<const char *> cast_tester<const char *>::named_get(simdjson_result<element> element) { return element.get_c_str(); }
template<> simdjson_result<std::string_view> cast_tester<std::string_view>::named_get(simdjson_result<element> element) { return element.get_string(); } template<> simdjson_result<std::string_view> cast_tester<std::string_view>::named_get(simdjson_result<element> element) { return element.get_string(); }
template<> simdjson_result<uint64_t> cast_tester<uint64_t>::named_get(simdjson_result<element> element) { return element.get_uint64_t(); } template<> simdjson_result<uint64_t> cast_tester<uint64_t>::named_get(simdjson_result<element> element) { return element.get_uint64(); }
template<> simdjson_result<int64_t> cast_tester<int64_t>::named_get(simdjson_result<element> element) { return element.get_int64_t(); } template<> simdjson_result<int64_t> cast_tester<int64_t>::named_get(simdjson_result<element> element) { return element.get_int64(); }
template<> simdjson_result<double> cast_tester<double>::named_get(simdjson_result<element> element) { return element.get_double(); } template<> simdjson_result<double> cast_tester<double>::named_get(simdjson_result<element> element) { return element.get_double(); }
template<> simdjson_result<bool> cast_tester<bool>::named_get(simdjson_result<element> element) { return element.get_bool(); } template<> simdjson_result<bool> cast_tester<bool>::named_get(simdjson_result<element> element) { return element.get_bool(); }
@ -247,8 +247,8 @@ template<> bool cast_tester<array>::named_is(element element) { return element.i
template<> bool cast_tester<object>::named_is(element element) { return element.is_object(); } template<> bool cast_tester<object>::named_is(element element) { return element.is_object(); }
template<> bool cast_tester<const char *>::named_is(element element) { return element.is_string(); } template<> bool cast_tester<const char *>::named_is(element element) { return element.is_string(); }
template<> bool cast_tester<std::string_view>::named_is(element element) { return element.is_string(); } template<> bool cast_tester<std::string_view>::named_is(element element) { return element.is_string(); }
template<> bool cast_tester<uint64_t>::named_is(element element) { return element.is_uint64_t(); } template<> bool cast_tester<uint64_t>::named_is(element element) { return element.is_uint64(); }
template<> bool cast_tester<int64_t>::named_is(element element) { return element.is_int64_t(); } template<> bool cast_tester<int64_t>::named_is(element element) { return element.is_int64(); }
template<> bool cast_tester<double>::named_is(element element) { return element.is_double(); } template<> bool cast_tester<double>::named_is(element element) { return element.is_double(); }
template<> bool cast_tester<bool>::named_is(element element) { return element.is_bool(); } template<> bool cast_tester<bool>::named_is(element element) { return element.is_bool(); }
@ -256,8 +256,8 @@ template<> simdjson_result<bool> cast_tester<array>::named_is(simdjson_result<el
template<> simdjson_result<bool> cast_tester<object>::named_is(simdjson_result<element> element) { return element.is_object(); } template<> simdjson_result<bool> cast_tester<object>::named_is(simdjson_result<element> element) { return element.is_object(); }
template<> simdjson_result<bool> cast_tester<const char *>::named_is(simdjson_result<element> element) { return element.is_string(); } template<> simdjson_result<bool> cast_tester<const char *>::named_is(simdjson_result<element> element) { return element.is_string(); }
template<> simdjson_result<bool> cast_tester<std::string_view>::named_is(simdjson_result<element> element) { return element.is_string(); } template<> simdjson_result<bool> cast_tester<std::string_view>::named_is(simdjson_result<element> element) { return element.is_string(); }
template<> simdjson_result<bool> cast_tester<uint64_t>::named_is(simdjson_result<element> element) { return element.is_uint64_t(); } template<> simdjson_result<bool> cast_tester<uint64_t>::named_is(simdjson_result<element> element) { return element.is_uint64(); }
template<> simdjson_result<bool> cast_tester<int64_t>::named_is(simdjson_result<element> element) { return element.is_int64_t(); } template<> simdjson_result<bool> cast_tester<int64_t>::named_is(simdjson_result<element> element) { return element.is_int64(); }
template<> simdjson_result<bool> cast_tester<double>::named_is(simdjson_result<element> element) { return element.is_double(); } template<> simdjson_result<bool> cast_tester<double>::named_is(simdjson_result<element> element) { return element.is_double(); }
template<> simdjson_result<bool> cast_tester<bool>::named_is(simdjson_result<element> element) { return element.is_bool(); } template<> simdjson_result<bool> cast_tester<bool>::named_is(simdjson_result<element> element) { return element.is_bool(); }