pitrou commented on code in PR #50874:
URL: https://github.com/apache/arrow/pull/50874#discussion_r3850640755
##########
cpp/src/arrow/util/simdjson_internal.h:
##########
@@ -91,6 +94,122 @@ Result<T>
ResolveSimdjsonResult(simdjson::simdjson_result<T> result,
return value;
}
+inline const char* JsonTypeName(simdjson::dom::element_type type) {
+ switch (type) {
+ case simdjson::dom::element_type::ARRAY:
+ return "array";
+ case simdjson::dom::element_type::OBJECT:
+ return "object";
+ case simdjson::dom::element_type::INT64:
+ case simdjson::dom::element_type::UINT64:
+ case simdjson::dom::element_type::DOUBLE:
+ case simdjson::dom::element_type::BIGINT:
+ return "number";
+ case simdjson::dom::element_type::STRING:
+ return "string";
+ case simdjson::dom::element_type::BOOL:
+ return "boolean";
+ case simdjson::dom::element_type::NULL_VALUE:
+ return "null";
+ }
+ return "unknown";
+}
+
+inline Result<simdjson::dom::array> GetJsonArray(simdjson::dom::element value,
+ std::string_view name) {
+ if (!value.is_array()) {
+ return Status::Invalid(name, " must be an array, got ",
JsonTypeName(value.type()));
+ }
+ return ResolveSimdjsonResult(value.get_array(), "Failed to get JSON array");
+}
+
+inline Result<int64_t> GetJsonInt(simdjson::dom::element value,
std::string_view name,
+ std::string_view expected) {
+ if (!value.is_int64()) {
+ return Status::Invalid(name, " must contain ", expected, ", got ",
+ JsonTypeName(value.type()));
+ }
+ return ResolveSimdjsonResult(value.get_int64(), "Failed to get JSON
integer");
+}
+
+inline Result<simdjson::dom::object> ParseJsonObject(simdjson::dom::parser&
parser,
+ const std::string& json) {
+ return ResolveSimdjsonResult(parser.parse(json).get_object(),
+ "Invalid serialized JSON data");
+}
+
+// object.at_key() performs a linear search. This is acceptable here
+// since these objects are expected to contain only a small number of fields.
+inline Result<std::optional<simdjson::dom::element>> GetOptionalJsonField(
+ const simdjson::dom::object& object, std::string_view key) {
+ auto field = object.at_key(key);
+ if (field.error() == simdjson::NO_SUCH_FIELD) {
+ return std::nullopt;
+ }
+
+ ARROW_ASSIGN_OR_RAISE(
+ auto value,
+ ResolveSimdjsonResult(std::move(field), "Failed to get JSON object
field"));
+
+ return std::optional<simdjson::dom::element>(value);
Review Comment:
```suggestion
return std::optional<simdjson::dom::element>(std::move(value));
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]