1919
2020#include " iceberg/catalog/rest/json_internal.h"
2121
22- #include < ranges>
2322#include < string>
2423#include < unordered_map>
2524#include < utility>
2928
3029#include " iceberg/catalog/rest/types.h"
3130#include " iceberg/json_internal.h"
32- #include " iceberg/partition_spec.h"
33- #include " iceberg/schema.h"
34- #include " iceberg/sort_order.h"
35- #include " iceberg/table_metadata.h"
31+ #include " iceberg/table_identifier.h"
3632#include " iceberg/util/json_util_internal.h"
3733#include " iceberg/util/macros.h"
3834
@@ -66,7 +62,6 @@ constexpr std::string_view kIdentifiers = "identifiers";
6662
6763} // namespace
6864
69- // CreateNamespaceRequest
7065nlohmann::json ToJson (const CreateNamespaceRequest& request) {
7166 nlohmann::json json;
7267 json[kNamespace ] = request.namespace_ .levels ;
@@ -83,12 +78,10 @@ Result<CreateNamespaceRequest> CreateNamespaceRequestFromJson(
8378 GetJsonValue<std::vector<std::string>>(json, kNamespace ));
8479 ICEBERG_ASSIGN_OR_RAISE (
8580 request.properties ,
86- (GetJsonValueOrDefault<std::unordered_map<std::string, std::string>>(json,
87- kProperties )));
81+ GetJsonValueOrDefault<decltype (request.properties )>(json, kProperties ));
8882 return request;
8983}
9084
91- // UpdateNamespacePropertiesRequest
9285nlohmann::json ToJson (const UpdateNamespacePropertiesRequest& request) {
9386 // Initialize as an empty object so that when all optional fields are absent we return
9487 // {} instead of null
@@ -108,65 +101,10 @@ Result<UpdateNamespacePropertiesRequest> UpdateNamespacePropertiesRequestFromJso
108101 ICEBERG_ASSIGN_OR_RAISE (
109102 request.removals , GetJsonValueOrDefault<std::vector<std::string>>(json, kRemovals ));
110103 ICEBERG_ASSIGN_OR_RAISE (
111- request.updates ,
112- (GetJsonValueOrDefault<std::unordered_map<std::string, std::string>>(json,
113- kUpdates )));
104+ request.updates , GetJsonValueOrDefault<decltype (request.updates )>(json, kUpdates ));
114105 return request;
115106}
116107
117- // CreateTableRequest
118- nlohmann::json ToJson (const CreateTableRequest& request) {
119- nlohmann::json json;
120- json[kName ] = request.name ;
121- if (!request.location .empty ()) {
122- json[kLocation ] = request.location ;
123- }
124- json[kSchema ] = ToJson (*request.schema );
125- if (request.partition_spec ) {
126- json[kPartitionSpec ] = ToJson (*request.partition_spec );
127- }
128- if (request.write_order ) {
129- json[kWriteOrder ] = ToJson (*request.write_order );
130- }
131- SetOptionalField (json, kStageCreate , request.stage_create );
132- if (!request.properties .empty ()) {
133- json[kProperties ] = request.properties ;
134- }
135- return json;
136- }
137-
138- Result<CreateTableRequest> CreateTableRequestFromJson (const nlohmann::json& json) {
139- CreateTableRequest request;
140- ICEBERG_ASSIGN_OR_RAISE (request.name , GetJsonValue<std::string>(json, kName ));
141- ICEBERG_ASSIGN_OR_RAISE (request.location ,
142- GetJsonValueOrDefault<std::string>(json, kLocation , " " ));
143- ICEBERG_ASSIGN_OR_RAISE (auto schema_json, GetJsonValue<nlohmann::json>(json, kSchema ));
144- ICEBERG_ASSIGN_OR_RAISE (request.schema , SchemaFromJson (schema_json));
145- if (json.contains (kPartitionSpec )) {
146- ICEBERG_ASSIGN_OR_RAISE (auto partition_spec_json,
147- GetJsonValue<nlohmann::json>(json, kPartitionSpec ));
148- ICEBERG_ASSIGN_OR_RAISE (request.partition_spec ,
149- PartitionSpecFromJson (request.schema , partition_spec_json));
150- } else {
151- request.partition_spec = nullptr ;
152- }
153- if (json.contains (kWriteOrder )) {
154- ICEBERG_ASSIGN_OR_RAISE (auto write_order_json,
155- GetJsonValue<nlohmann::json>(json, kWriteOrder ));
156- ICEBERG_ASSIGN_OR_RAISE (request.write_order , SortOrderFromJson (write_order_json));
157- } else {
158- request.write_order = nullptr ;
159- }
160- ICEBERG_ASSIGN_OR_RAISE (request.stage_create ,
161- GetJsonValueOptional<bool >(json, kStageCreate ));
162- ICEBERG_ASSIGN_OR_RAISE (
163- request.properties ,
164- (GetJsonValueOrDefault<std::unordered_map<std::string, std::string>>(json,
165- kProperties )));
166- return request;
167- }
168-
169- // RegisterTableRequest
170108nlohmann::json ToJson (const RegisterTableRequest& request) {
171109 nlohmann::json json;
172110 json[kName ] = request.name ;
@@ -182,14 +120,11 @@ Result<RegisterTableRequest> RegisterTableRequestFromJson(const nlohmann::json&
182120 ICEBERG_ASSIGN_OR_RAISE (request.name , GetJsonValue<std::string>(json, kName ));
183121 ICEBERG_ASSIGN_OR_RAISE (request.metadata_location ,
184122 GetJsonValue<std::string>(json, kMetadataLocation ));
185- // Default to false if not present
186- ICEBERG_ASSIGN_OR_RAISE (auto overwrite_opt,
187- GetJsonValueOptional<bool >(json, kOverwrite ));
188- request.overwrite = overwrite_opt.value_or (false );
123+ ICEBERG_ASSIGN_OR_RAISE (request.overwrite ,
124+ GetJsonValueOrDefault<bool >(json, kOverwrite , false ));
189125 return request;
190126}
191127
192- // RenameTableRequest
193128nlohmann::json ToJson (const RenameTableRequest& request) {
194129 nlohmann::json json;
195130 json[kSource ] = ToJson (request.source );
@@ -222,8 +157,8 @@ nlohmann::json ToJson(const LoadTableResult& result) {
222157
223158Result<LoadTableResult> LoadTableResultFromJson (const nlohmann::json& json) {
224159 LoadTableResult result;
225- ICEBERG_ASSIGN_OR_RAISE (result.metadata_location , GetJsonValueOrDefault<std::string>(
226- json, kMetadataLocation , " " ));
160+ ICEBERG_ASSIGN_OR_RAISE (result.metadata_location ,
161+ GetJsonValueOrDefault<std::string>( json, kMetadataLocation ));
227162 ICEBERG_ASSIGN_OR_RAISE (auto metadata_json,
228163 GetJsonValue<nlohmann::json>(json, kMetadata ));
229164 ICEBERG_ASSIGN_OR_RAISE (result.metadata , TableMetadataFromJson (metadata_json));
@@ -233,7 +168,6 @@ Result<LoadTableResult> LoadTableResultFromJson(const nlohmann::json& json) {
233168 return result;
234169}
235170
236- // ListNamespacesResponse
237171nlohmann::json ToJson (const ListNamespacesResponse& response) {
238172 nlohmann::json json;
239173 if (!response.next_page_token .empty ()) {
@@ -251,7 +185,7 @@ Result<ListNamespacesResponse> ListNamespacesResponseFromJson(
251185 const nlohmann::json& json) {
252186 ListNamespacesResponse response;
253187 ICEBERG_ASSIGN_OR_RAISE (response.next_page_token ,
254- GetJsonValueOrDefault<std::string>(json, kNextPageToken , " " ));
188+ GetJsonValueOrDefault<std::string>(json, kNextPageToken ));
255189 ICEBERG_ASSIGN_OR_RAISE (auto namespaces_json,
256190 GetJsonValue<nlohmann::json>(json, kNamespaces ));
257191 for (const auto & ns_json : namespaces_json) {
@@ -261,7 +195,6 @@ Result<ListNamespacesResponse> ListNamespacesResponseFromJson(
261195 return response;
262196}
263197
264- // CreateNamespaceResponse
265198nlohmann::json ToJson (const CreateNamespaceResponse& response) {
266199 nlohmann::json json;
267200 json[kNamespace ] = response.namespace_ .levels ;
@@ -278,12 +211,10 @@ Result<CreateNamespaceResponse> CreateNamespaceResponseFromJson(
278211 GetJsonValue<std::vector<std::string>>(json, kNamespace ));
279212 ICEBERG_ASSIGN_OR_RAISE (
280213 response.properties ,
281- (GetJsonValueOrDefault<std::unordered_map<std::string, std::string>>(json,
282- kProperties )));
214+ GetJsonValueOrDefault<decltype (response.properties )>(json, kProperties ));
283215 return response;
284216}
285217
286- // GetNamespaceResponse
287218nlohmann::json ToJson (const GetNamespaceResponse& response) {
288219 nlohmann::json json;
289220 json[kNamespace ] = response.namespace_ .levels ;
@@ -299,12 +230,10 @@ Result<GetNamespaceResponse> GetNamespaceResponseFromJson(const nlohmann::json&
299230 GetJsonValue<std::vector<std::string>>(json, kNamespace ));
300231 ICEBERG_ASSIGN_OR_RAISE (
301232 response.properties ,
302- (GetJsonValueOrDefault<std::unordered_map<std::string, std::string>>(json,
303- kProperties )));
233+ GetJsonValueOrDefault<decltype (response.properties )>(json, kProperties ));
304234 return response;
305235}
306236
307- // UpdateNamespacePropertiesResponse
308237nlohmann::json ToJson (const UpdateNamespacePropertiesResponse& response) {
309238 nlohmann::json json;
310239 json[kUpdated ] = response.updated ;
@@ -327,7 +256,6 @@ Result<UpdateNamespacePropertiesResponse> UpdateNamespacePropertiesResponseFromJ
327256 return response;
328257}
329258
330- // ListTablesResponse
331259nlohmann::json ToJson (const ListTablesResponse& response) {
332260 nlohmann::json json;
333261 if (!response.next_page_token .empty ()) {
@@ -344,7 +272,7 @@ nlohmann::json ToJson(const ListTablesResponse& response) {
344272Result<ListTablesResponse> ListTablesResponseFromJson (const nlohmann::json& json) {
345273 ListTablesResponse response;
346274 ICEBERG_ASSIGN_OR_RAISE (response.next_page_token ,
347- GetJsonValueOrDefault<std::string>(json, kNextPageToken , " " ));
275+ GetJsonValueOrDefault<std::string>(json, kNextPageToken ));
348276 ICEBERG_ASSIGN_OR_RAISE (auto identifiers_json,
349277 GetJsonValue<nlohmann::json>(json, kIdentifiers ));
350278 for (const auto & id_json : identifiers_json) {
0 commit comments