Merge pull request #968 from simdjson/issue961

Fixing issue 961
This commit is contained in:
Daniel Lemire 2020-06-23 19:48:43 -04:00 committed by GitHub
commit 3e35729eb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 33 deletions

View File

@ -98,10 +98,11 @@ Using the Parsed JSON
Once you have an element, you can navigate it with idiomatic C++ iterators, operators and casts.
* **Extracting Values:** You can cast a JSON element to a native type: `double(element)` or
* **Extracting Values (with exceptions):** 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,
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 `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.,
dom::object and dom::array. An exception is thrown if the cast is not possible.
* **Extracting Values (without expceptions):** You can use a variant usage of `get()` with error codes to avoid exceptions. You first declare the variable of the appropriate type (`double`, `uint64_t`, `int64_t`, `bool`,
`dom::object` and `dom::array`) and pass it by reference to `get()` which gives you back an error code: e.g.,
```c++
simdjson::error_code error;
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
* 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.
*
@ -100,7 +100,7 @@ public:
* Returns INCORRECT_TYPE if the JSON element is not an integer, or NUMBER_OUT_OF_RANGE
* 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.
*
@ -143,13 +143,13 @@ public:
*
* 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.
*
* 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.
*
@ -465,16 +465,16 @@ public:
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<std::string_view> get_string() const noexcept;
really_inline simdjson_result<int64_t> get_int64_t() const noexcept;
really_inline simdjson_result<uint64_t> get_uint64_t() const noexcept;
really_inline simdjson_result<int64_t> get_int64() 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<bool> get_bool() const noexcept;
really_inline bool is_array() const noexcept;
really_inline bool is_object() const noexcept;
really_inline bool is_string() const noexcept;
really_inline bool is_int64_t() const noexcept;
really_inline bool is_uint64_t() const noexcept;
really_inline bool is_int64() const noexcept;
really_inline bool is_uint64() const noexcept;
really_inline bool is_double() const noexcept;
really_inline bool is_bool() const noexcept;
really_inline bool is_null() const noexcept;

View File

@ -54,13 +54,13 @@ really_inline simdjson_result<std::string_view> simdjson_result<dom::element>::g
if (error()) { return error(); }
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(); }
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(); }
return first.get_uint64_t();
return first.get_uint64();
}
really_inline simdjson_result<double> simdjson_result<dom::element>::get_double() const noexcept {
if (error()) { return error(); }
@ -80,11 +80,11 @@ really_inline bool simdjson_result<dom::element>::is_object() const noexcept {
really_inline bool simdjson_result<dom::element>::is_string() const noexcept {
return !error() && first.is_string();
}
really_inline bool simdjson_result<dom::element>::is_int64_t() const noexcept {
return !error() && first.is_int64_t();
really_inline bool simdjson_result<dom::element>::is_int64() const noexcept {
return !error() && first.is_int64();
}
really_inline bool simdjson_result<dom::element>::is_uint64_t() const noexcept {
return !error() && first.is_uint64_t();
really_inline bool simdjson_result<dom::element>::is_uint64() const noexcept {
return !error() && first.is_uint64();
}
really_inline bool simdjson_result<dom::element>::is_double() const noexcept {
return !error() && first.is_double();
@ -198,7 +198,7 @@ inline simdjson_result<std::string_view> element::get_string() const noexcept {
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(tape.is_int64()) {
int64_t result = tape.next_tape_value<int64_t>();
@ -211,7 +211,7 @@ inline simdjson_result<uint64_t> element::get_uint64_t() const noexcept {
}
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(tape.is_uint64()) {
uint64_t result = tape.next_tape_value<uint64_t>();
@ -284,16 +284,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<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<int64_t> element::get<int64_t>() const noexcept { return get_int64_t(); }
template<> inline simdjson_result<uint64_t> element::get<uint64_t>() const noexcept { return get_uint64_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(); }
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(); }
inline bool element::is_array() const noexcept { return is<array>(); }
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_int64_t() const noexcept { return is<int64_t>(); }
inline bool element::is_uint64_t() const noexcept { return is<uint64_t>(); }
inline bool element::is_int64() const noexcept { return is<int64_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_bool() const noexcept { return is<bool>(); }

View File

@ -224,8 +224,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<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<uint64_t> cast_tester<uint64_t>::named_get(element element) { return element.get_uint64_t(); }
template<> simdjson_result<int64_t> cast_tester<int64_t>::named_get(element element) { return element.get_int64_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(); }
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(); }
@ -233,8 +233,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<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<uint64_t> cast_tester<uint64_t>::named_get(simdjson_result<element> element) { return element.get_uint64_t(); }
template<> simdjson_result<int64_t> cast_tester<int64_t>::named_get(simdjson_result<element> element) { return element.get_int64_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(); }
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(); }
@ -242,8 +242,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<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<uint64_t>::named_is(element element) { return element.is_uint64_t(); }
template<> bool cast_tester<int64_t>::named_is(element element) { return element.is_int64_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(); }
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(); }
@ -251,8 +251,8 @@ template<> bool cast_tester<array>::named_is(simdjson_result<element> element) {
template<> bool cast_tester<object>::named_is(simdjson_result<element> element) { return element.is_object(); }
template<> bool cast_tester<const char *>::named_is(simdjson_result<element> element) { return element.is_string(); }
template<> bool cast_tester<std::string_view>::named_is(simdjson_result<element> element) { return element.is_string(); }
template<> bool cast_tester<uint64_t>::named_is(simdjson_result<element> element) { return element.is_uint64_t(); }
template<> bool cast_tester<int64_t>::named_is(simdjson_result<element> element) { return element.is_int64_t(); }
template<> bool cast_tester<uint64_t>::named_is(simdjson_result<element> element) { return element.is_uint64(); }
template<> bool cast_tester<int64_t>::named_is(simdjson_result<element> element) { return element.is_int64(); }
template<> bool cast_tester<double>::named_is(simdjson_result<element> element) { return element.is_double(); }
template<> bool cast_tester<bool>::named_is(simdjson_result<element> element) { return element.is_bool(); }