Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions cpp/src/arrow/json/from_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace json {
using ::arrow::internal::checked_cast;
using ::arrow::internal::checked_pointer_cast;

namespace internal {
namespace {

constexpr auto kParseFlags = rj::kParseFullPrecisionFlag | rj::kParseNanAndInfFlag;

Expand Down Expand Up @@ -90,9 +90,9 @@ Status JSONTypeError(const char* expected_type, rj::Type json_type) {
JsonTypeName(json_type));
}

class Converter {
class JSONConverter {
public:
virtual ~Converter() = default;
virtual ~JSONConverter() = default;

virtual Status Init() { return Status::OK(); }

Expand All @@ -117,11 +117,12 @@ class Converter {
std::shared_ptr<DataType> type_;
};

Status GetConverter(const std::shared_ptr<DataType>&, std::shared_ptr<Converter>* out);
Status GetConverter(const std::shared_ptr<DataType>&,
std::shared_ptr<JSONConverter>* out);

// CRTP
template <class Derived>
class ConcreteConverter : public Converter {
class ConcreteConverter : public JSONConverter {
public:
Result<int64_t> SizeOfJSONArray(const rj::Value& json_obj) {
if (!json_obj.IsArray()) {
Expand Down Expand Up @@ -601,7 +602,7 @@ class VarLengthListLikeConverter final

private:
std::shared_ptr<BuilderType> builder_;
std::shared_ptr<Converter> child_converter_;
std::shared_ptr<JSONConverter> child_converter_;
};

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -653,7 +654,7 @@ class MapConverter final : public ConcreteConverter<MapConverter> {

private:
std::shared_ptr<MapBuilder> builder_;
std::shared_ptr<Converter> key_converter_, item_converter_;
std::shared_ptr<JSONConverter> key_converter_, item_converter_;
};

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -691,7 +692,7 @@ class FixedSizeListConverter final : public ConcreteConverter<FixedSizeListConve
private:
int32_t list_size_;
std::shared_ptr<FixedSizeListBuilder> builder_;
std::shared_ptr<Converter> child_converter_;
std::shared_ptr<JSONConverter> child_converter_;
};

// ------------------------------------------------------------------------
Expand All @@ -704,7 +705,7 @@ class StructConverter final : public ConcreteConverter<StructConverter> {
Status Init() override {
std::vector<std::shared_ptr<ArrayBuilder>> child_builders;
for (const auto& field : type_->fields()) {
std::shared_ptr<Converter> child_converter;
std::shared_ptr<JSONConverter> child_converter;
RETURN_NOT_OK(GetConverter(field->type(), &child_converter));
child_converters_.push_back(child_converter);
child_builders.push_back(child_converter->builder());
Expand Down Expand Up @@ -762,7 +763,7 @@ class StructConverter final : public ConcreteConverter<StructConverter> {

private:
std::shared_ptr<StructBuilder> builder_;
std::vector<std::shared_ptr<Converter>> child_converters_;
std::vector<std::shared_ptr<JSONConverter>> child_converters_;
};

// ------------------------------------------------------------------------
Expand All @@ -783,7 +784,7 @@ class UnionConverter final : public ConcreteConverter<UnionConverter> {
}
std::vector<std::shared_ptr<ArrayBuilder>> child_builders;
for (const auto& field : type_->fields()) {
std::shared_ptr<Converter> child_converter;
std::shared_ptr<JSONConverter> child_converter;
RETURN_NOT_OK(GetConverter(field->type(), &child_converter));
child_converters_.push_back(child_converter);
child_builders.push_back(child_converter->builder());
Expand Down Expand Up @@ -841,7 +842,7 @@ class UnionConverter final : public ConcreteConverter<UnionConverter> {
private:
UnionMode::type mode_;
std::shared_ptr<ArrayBuilder> builder_;
std::vector<std::shared_ptr<Converter>> child_converters_;
std::vector<std::shared_ptr<JSONConverter>> child_converters_;
std::vector<int8_t> type_id_to_child_num_;
};

Expand All @@ -854,8 +855,8 @@ Status ConversionNotImplemented(const std::shared_ptr<DataType>& type) {
}

Status GetDictConverter(const std::shared_ptr<DataType>& type,
std::shared_ptr<Converter>* out) {
std::shared_ptr<Converter> res;
std::shared_ptr<JSONConverter>* out) {
std::shared_ptr<JSONConverter> res;

const auto value_type = checked_cast<const DictionaryType&>(*type).value_type();

Expand Down Expand Up @@ -905,12 +906,12 @@ Status GetDictConverter(const std::shared_ptr<DataType>& type,
}

Status GetConverter(const std::shared_ptr<DataType>& type,
std::shared_ptr<Converter>* out) {
std::shared_ptr<JSONConverter>* out) {
if (type->id() == Type::DICTIONARY) {
return GetDictConverter(type, out);
}

std::shared_ptr<Converter> res;
std::shared_ptr<JSONConverter> res;

#define SIMPLE_CONVERTER_CASE(ID, CLASS) \
case ID: \
Expand Down Expand Up @@ -972,15 +973,15 @@ Status GetConverter(const std::shared_ptr<DataType>& type,
return Status::OK();
}

} // namespace internal
} // namespace

Result<std::shared_ptr<Array>> ArrayFromJSONString(const std::shared_ptr<DataType>& type,
std::string_view json_string) {
std::shared_ptr<internal::Converter> converter;
std::shared_ptr<JSONConverter> converter;
RETURN_NOT_OK(GetConverter(type, &converter));

rj::Document json_doc;
json_doc.Parse<internal::kParseFlags>(json_string.data(), json_string.length());
json_doc.Parse<kParseFlags>(json_string.data(), json_string.length());
if (json_doc.HasParseError()) {
return Status::Invalid("JSON parse error at offset ", json_doc.GetErrorOffset(), ": ",
GetParseError_En(json_doc.GetParseError()));
Expand Down Expand Up @@ -1037,11 +1038,11 @@ Status DictArrayFromJSONString(const std::shared_ptr<DataType>& type,

Status ScalarFromJSONString(const std::shared_ptr<DataType>& type,
std::string_view json_string, std::shared_ptr<Scalar>* out) {
std::shared_ptr<internal::Converter> converter;
std::shared_ptr<JSONConverter> converter;
RETURN_NOT_OK(GetConverter(type, &converter));

rj::Document json_doc;
json_doc.Parse<internal::kParseFlags>(json_string.data(), json_string.length());
json_doc.Parse<kParseFlags>(json_string.data(), json_string.length());
if (json_doc.HasParseError()) {
return Status::Invalid("JSON parse error at offset ", json_doc.GetErrorOffset(), ": ",
GetParseError_En(json_doc.GetParseError()));
Expand Down