MCPcopy Create free account
hub / github.com/apache/arrow / ScalarFromJSONString

Function ScalarFromJSONString

cpp/src/arrow/json/from_string.cc:1102–1138  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1100}
1101
1102Result<std::shared_ptr<Scalar>> ScalarFromJSONString(
1103 const std::shared_ptr<DataType>& type, std::string_view json_string) {
1104 std::shared_ptr<JSONConverter> converter;
1105 RETURN_NOT_OK(GetConverter(type, &converter));
1106
1107 simdjson::padded_string padded_string{
1108 arrow::internal::JoinToString("[", json_string, "]")};
1109
1110 sj::parser parser;
1111 sj::document json_doc;
1112 auto error = parser.iterate(padded_string).get(json_doc);
1113 if (error) {
1114 return Status::Invalid("JSON parse error: ", simdjson::error_message(error));
1115 }
1116
1117 sj::value json_obj;
1118 if (auto error_code = json_doc.get_value().get(json_obj);
1119 error_code != simdjson::SUCCESS) {
1120 return Status::Invalid("JSON parse error: ", simdjson::error_message(error_code));
1121 }
1122 ARROW_ASSIGN_OR_RAISE(auto singleton_array, internal::GetJsonAs<sj::array>(json_obj));
1123
1124 ARROW_ASSIGN_OR_RAISE(int32_t num_elements, converter->AppendValues(singleton_array));
1125 if (num_elements != 1) {
1126 return Status::Invalid("Expected exactly one JSON value, got ", num_elements,
1127 " values");
1128 }
1129 // The parser is lazy and does not look past the end of the wrapped value by
1130 // itself, e.g. the input "1] , [2" would otherwise be accepted as 1
1131 if (!json_doc.at_end()) {
1132 return Status::Invalid("JSON parse error: trailing content after JSON value");
1133 }
1134
1135 std::shared_ptr<Array> array;
1136 RETURN_NOT_OK(converter->Finish(&array));
1137 return array->GetScalar(0);
1138}
1139
1140Result<std::shared_ptr<Scalar>> DictScalarFromJSONString(
1141 const std::shared_ptr<DataType>& type, std::string_view index_json,

Callers 2

TestSessionFunction · 0.85

Calls 7

JoinToStringFunction · 0.85
GetConverterFunction · 0.70
InvalidFunction · 0.50
getMethod · 0.45
get_valueMethod · 0.45
FinishMethod · 0.45
GetScalarMethod · 0.45

Tested by 1

TestSessionFunction · 0.68