Skip to content
Merged
Show file tree
Hide file tree
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
39 changes: 21 additions & 18 deletions cpp/src/arrow/extension/fixed_shape_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "arrow/array/array_nested.h"
#include "arrow/array/array_primitive.h"
#include "arrow/json/json_writer_internal.h"
#include "arrow/json/rapidjson_defs.h" // IWYU pragma: keep
#include "arrow/tensor.h"
#include "arrow/util/logging_internal.h"
Expand All @@ -33,9 +34,9 @@
#include "arrow/util/string.h"

#include <rapidjson/document.h>
#include <rapidjson/writer.h>

namespace rj = arrow::rapidjson;
using ::arrow::json::JsonWriter;

namespace arrow::extension {

Expand Down Expand Up @@ -72,36 +73,38 @@ std::string FixedShapeTensorType::ToString(bool show_metadata) const {
}

std::string FixedShapeTensorType::Serialize() const {
rj::Document document;
document.SetObject();
rj::Document::AllocatorType& allocator = document.GetAllocator();
JsonWriter writer;

writer.StartObject();

rj::Value shape(rj::kArrayType);
writer.Key("shape");
writer.StartArray();
for (auto v : shape_) {
shape.PushBack(v, allocator);
writer.Int64(v);
}
document.AddMember(rj::Value("shape", allocator), shape, allocator);
writer.EndArray();

if (!permutation_.empty()) {
rj::Value permutation(rj::kArrayType);
writer.Key("permutation");
writer.StartArray();
for (auto v : permutation_) {
permutation.PushBack(v, allocator);
writer.Int64(v);
}
document.AddMember(rj::Value("permutation", allocator), permutation, allocator);
writer.EndArray();
}

if (!dim_names_.empty()) {
rj::Value dim_names(rj::kArrayType);
for (const std::string& v : dim_names_) {
dim_names.PushBack(rj::Value{}.SetString(v.c_str(), allocator), allocator);
writer.Key("dim_names");
writer.StartArray();
for (const auto& v : dim_names_) {
writer.String(v);
}
document.AddMember(rj::Value("dim_names", allocator), dim_names, allocator);
writer.EndArray();
}

rj::StringBuffer buffer;
rj::Writer<rj::StringBuffer> writer(buffer);
document.Accept(writer);
return buffer.GetString();
writer.EndObject();

return std::string(writer.GetString());
}

Result<std::shared_ptr<DataType>> FixedShapeTensorType::Deserialize(
Expand Down
27 changes: 13 additions & 14 deletions cpp/src/arrow/extension/opaque.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

#include <sstream>

#include "arrow/json/json_writer_internal.h"
#include "arrow/json/rapidjson_defs.h" // IWYU pragma: keep
#include "arrow/util/logging_internal.h"

#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <rapidjson/writer.h>

using ::arrow::json::JsonWriter;

namespace arrow::extension {

Expand All @@ -46,19 +48,16 @@ bool OpaqueType::ExtensionEquals(const ExtensionType& other) const {
}

std::string OpaqueType::Serialize() const {
rapidjson::Document document;
document.SetObject();
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();

rapidjson::Value type_name(rapidjson::StringRef(type_name_));
document.AddMember(rapidjson::Value("type_name", allocator), type_name, allocator);
rapidjson::Value vendor_name(rapidjson::StringRef(vendor_name_));
document.AddMember(rapidjson::Value("vendor_name", allocator), vendor_name, allocator);

rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
document.Accept(writer);
return buffer.GetString();
JsonWriter writer;

writer.StartObject();

writer.StringField("type_name", type_name_);
writer.StringField("vendor_name", vendor_name_);

writer.EndObject();

return std::string(writer.GetString());
}

Result<std::shared_ptr<DataType>> OpaqueType::Deserialize(
Expand Down
43 changes: 23 additions & 20 deletions cpp/src/arrow/extension/variable_shape_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "arrow/extension/variable_shape_tensor.h"

#include "arrow/array/array_primitive.h"
#include "arrow/json/json_writer_internal.h"
#include "arrow/json/rapidjson_defs.h" // IWYU pragma: keep
#include "arrow/scalar.h"
#include "arrow/tensor.h"
Expand All @@ -30,9 +31,9 @@
#include "arrow/util/string.h"

#include <rapidjson/document.h>
#include <rapidjson/writer.h>

namespace rj = arrow::rapidjson;
using ::arrow::json::JsonWriter;

namespace arrow::extension {

Expand Down Expand Up @@ -82,42 +83,44 @@ std::string VariableShapeTensorType::ToString(bool show_metadata) const {
}

std::string VariableShapeTensorType::Serialize() const {
rj::Document document;
document.SetObject();
rj::Document::AllocatorType& allocator = document.GetAllocator();
JsonWriter writer;

writer.StartObject();

if (!permutation_.empty()) {
rj::Value permutation(rj::kArrayType);
writer.Key("permutation");
writer.StartArray();
for (auto v : permutation_) {
permutation.PushBack(v, allocator);
writer.Int64(v);
}
document.AddMember(rj::Value("permutation", allocator), permutation, allocator);
writer.EndArray();
}

if (!dim_names_.empty()) {
rj::Value dim_names(rj::kArrayType);
for (const std::string& v : dim_names_) {
dim_names.PushBack(rj::Value{}.SetString(v.c_str(), allocator), allocator);
writer.Key("dim_names");
writer.StartArray();
for (const auto& v : dim_names_) {
writer.String(v);
}
document.AddMember(rj::Value("dim_names", allocator), dim_names, allocator);
writer.EndArray();
}

if (!uniform_shape_.empty()) {
rj::Value uniform_shape(rj::kArrayType);
for (auto v : uniform_shape_) {
writer.Key("uniform_shape");
writer.StartArray();
for (const auto& v : uniform_shape_) {
if (v.has_value()) {
uniform_shape.PushBack(v.value(), allocator);
writer.Int64(*v);
} else {
uniform_shape.PushBack(rj::Value{}.SetNull(), allocator);
writer.Null();
}
}
document.AddMember(rj::Value("uniform_shape", allocator), uniform_shape, allocator);
writer.EndArray();
}

rj::StringBuffer buffer;
rj::Writer<rj::StringBuffer> writer(buffer);
document.Accept(writer);
return buffer.GetString();
writer.EndObject();

return std::string(writer.GetString());
}

Result<std::shared_ptr<DataType>> VariableShapeTensorType::Deserialize(
Expand Down
Loading