Skip to content

Commit 26d4a3f

Browse files
author
xiao.dong
committed
add ToJsonString to avoid include nlohmann/json.hpp
1 parent c661697 commit 26d4a3f

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

src/iceberg/json_internal.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ nlohmann::json ToJson(const Schema& schema) {
309309
return json;
310310
}
311311

312+
Result<std::string> ToJsonString(const Schema& schema) {
313+
return ToJsonString(ToJson(schema));
314+
}
315+
312316
nlohmann::json ToJson(const SnapshotRef& ref) {
313317
nlohmann::json json;
314318
json[kSnapshotId] = ref.snapshot_id;
@@ -490,6 +494,10 @@ nlohmann::json ToJson(const PartitionSpec& partition_spec) {
490494
return json;
491495
}
492496

497+
Result<std::string> ToJsonString(const PartitionSpec& partition_spec) {
498+
return ToJsonString(ToJson(partition_spec));
499+
}
500+
493501
Result<std::unique_ptr<PartitionField>> PartitionFieldFromJson(
494502
const nlohmann::json& json, bool allow_field_id_missing) {
495503
ICEBERG_ASSIGN_OR_RAISE(auto source_id, GetJsonValue<int32_t>(json, kSourceId));
@@ -785,6 +793,10 @@ nlohmann::json ToJson(const TableMetadata& table_metadata) {
785793
return json;
786794
}
787795

796+
Result<std::string> ToJsonString(const TableMetadata& table_metadata) {
797+
return ToJsonString(ToJson(table_metadata));
798+
}
799+
788800
namespace {
789801

790802
/// \brief Parse the schemas from the JSON object.

src/iceberg/json_internal.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ ICEBERG_EXPORT Result<std::unique_ptr<SortOrder>> SortOrderFromJson(
8080
/// \return The JSON representation of the schema.
8181
ICEBERG_EXPORT nlohmann::json ToJson(const Schema& schema);
8282

83+
/// \brief Convert an Iceberg Schema to JSON.
84+
///
85+
/// \param[in] schema The Iceberg schema to convert.
86+
/// \return The JSON string of the schema.
87+
ICEBERG_EXPORT Result<std::string> ToJsonString(const Schema& schema);
88+
8389
/// \brief Convert JSON to an Iceberg Schema.
8490
///
8591
/// \param[in] json The JSON representation of the schema.
@@ -148,6 +154,18 @@ ICEBERG_EXPORT Result<std::unique_ptr<PartitionField>> PartitionFieldFromJson(
148154
/// array.
149155
ICEBERG_EXPORT nlohmann::json ToJson(const PartitionSpec& partition_spec);
150156

157+
/// \brief Serializes a `PartitionSpec` object to JSON.
158+
///
159+
/// This function converts a `PartitionSpec` object into a JSON representation.
160+
/// The resulting JSON includes the spec ID and a list of `PartitionField` objects.
161+
/// Each `PartitionField` is serialized as described in the `ToJson(PartitionField)`
162+
/// function.
163+
///
164+
/// \param partition_spec The `PartitionSpec` object to be serialized.
165+
/// \return A JSON string of the `PartitionSpec` with its order ID and fields
166+
/// array.
167+
ICEBERG_EXPORT Result<std::string> ToJsonString(const PartitionSpec& partition_spec);
168+
151169
/// \brief Deserializes a JSON object into a `PartitionSpec` object.
152170
///
153171
/// This function parses the provided JSON and creates a `PartitionSpec` object.
@@ -246,6 +264,12 @@ ICEBERG_EXPORT Result<MetadataLogEntry> MetadataLogEntryFromJson(
246264
/// \return A JSON object representing the `TableMetadata`.
247265
ICEBERG_EXPORT nlohmann::json ToJson(const TableMetadata& table_metadata);
248266

267+
/// \brief Serializes a `TableMetadata` object to JSON.
268+
///
269+
/// \param table_metadata The `TableMetadata` object to be serialized.
270+
/// \return A JSON string of the `TableMetadata`.
271+
ICEBERG_EXPORT Result<std::string> ToJsonString(const TableMetadata& table_metadata);
272+
249273
/// \brief Deserializes a JSON object into a `TableMetadata` object.
250274
///
251275
/// \param json The JSON object representing a `TableMetadata`.

src/iceberg/test/metadata_io_test.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ TEST_F(MetadataIOTest, ReadWriteMetadata) {
9090
TEST_F(MetadataIOTest, ReadWriteCompressedMetadata) {
9191
TableMetadata metadata = PrepareMetadata();
9292

93-
auto json = ToJson(metadata);
94-
auto ret = ToJsonString(json);
93+
auto ret = ToJsonString(metadata);
9594
ASSERT_TRUE(ret.has_value());
9695
auto json_string = ret.value();
9796

src/iceberg/v1_metadata.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
#include "iceberg/v1_metadata.h"
2121

22-
#include <nlohmann/json.hpp>
23-
2422
#include "iceberg/json_internal.h"
2523
#include "iceberg/manifest_entry.h"
2624
#include "iceberg/manifest_list.h"
@@ -51,9 +49,9 @@ Status ManifestEntryAdapterV1::Init() {
5149
DataFile::kSortOrderId.field_id(),
5250
};
5351
ICEBERG_RETURN_UNEXPECTED(InitSchema(kManifestEntryFieldIds));
54-
metadata_["schema"] = ToJson(*manifest_schema_).dump();
52+
ICEBERG_ASSIGN_OR_RAISE(metadata_["schema"], ToJsonString(*manifest_schema_))
5553
if (partition_spec_ != nullptr) {
56-
metadata_["partition-spec"] = ToJson(*partition_spec_).dump();
54+
ICEBERG_ASSIGN_OR_RAISE(metadata_["partition-spec"], ToJsonString(*partition_spec_));
5755
metadata_["partition-spec-id"] = std::to_string(partition_spec_->spec_id());
5856
}
5957
metadata_["format-version"] = "1";

src/iceberg/v2_metadata.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
#include "iceberg/v2_metadata.h"
2121

22-
#include <nlohmann/json.hpp>
23-
2422
#include "iceberg/json_internal.h"
2523
#include "iceberg/manifest_entry.h"
2624
#include "iceberg/manifest_list.h"
@@ -55,9 +53,9 @@ Status ManifestEntryAdapterV2::Init() {
5553
DataFile::kReferencedDataFile.field_id(),
5654
};
5755
ICEBERG_RETURN_UNEXPECTED(InitSchema(kManifestEntryFieldIds));
58-
metadata_["schema"] = ToJson(*manifest_schema_).dump();
56+
ICEBERG_ASSIGN_OR_RAISE(metadata_["schema"], ToJsonString(*manifest_schema_))
5957
if (partition_spec_ != nullptr) {
60-
metadata_["partition-spec"] = ToJson(*partition_spec_).dump();
58+
ICEBERG_ASSIGN_OR_RAISE(metadata_["partition-spec"], ToJsonString(*partition_spec_));
6159
metadata_["partition-spec-id"] = std::to_string(partition_spec_->spec_id());
6260
}
6361
metadata_["format-version"] = "2";

src/iceberg/v3_metadata.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
#include "iceberg/v3_metadata.h"
2121

22-
#include <nlohmann/json.hpp>
23-
2422
#include "iceberg/json_internal.h"
2523
#include "iceberg/manifest_entry.h"
2624
#include "iceberg/manifest_list.h"
@@ -58,9 +56,9 @@ Status ManifestEntryAdapterV3::Init() {
5856
DataFile::kContentSize.field_id(),
5957
};
6058
ICEBERG_RETURN_UNEXPECTED(InitSchema(kManifestEntryFieldIds));
61-
metadata_["schema"] = ToJson(*manifest_schema_).dump();
59+
ICEBERG_ASSIGN_OR_RAISE(metadata_["schema"], ToJsonString(*manifest_schema_))
6260
if (partition_spec_ != nullptr) {
63-
metadata_["partition-spec"] = ToJson(*partition_spec_).dump();
61+
ICEBERG_ASSIGN_OR_RAISE(metadata_["partition-spec"], ToJsonString(*partition_spec_));
6462
metadata_["partition-spec-id"] = std::to_string(partition_spec_->spec_id());
6563
}
6664
metadata_["format-version"] = "3";

0 commit comments

Comments
 (0)