2626#include < nlohmann/json.hpp>
2727
2828#include " iceberg/json_internal.h"
29- #include " iceberg/partition_spec.h"
3029#include " iceberg/result.h"
31- #include " iceberg/schema.h"
32- #include " iceberg/sort_order.h"
3330#include " iceberg/table_identifier.h"
3431#include " iceberg/table_metadata.h"
3532#include " iceberg/util/json_util_internal.h"
@@ -74,11 +71,8 @@ nlohmann::json TableIdentifierToJson(const TableIdentifier& identifier) {
7471Result<TableIdentifier> TableIdentifierFromJson (const nlohmann::json& json) {
7572 TableIdentifier identifier;
7673
77- if (json.contains (kNamespace )) {
78- ICEBERG_ASSIGN_OR_RAISE (identifier.ns .levels ,
79- GetJsonValue<std::vector<std::string>>(json, kNamespace ));
80- }
81-
74+ ICEBERG_ASSIGN_OR_RAISE (identifier.ns .levels ,
75+ GetJsonValue<std::vector<std::string>>(json, kNamespace ));
8276 ICEBERG_ASSIGN_OR_RAISE (identifier.name , GetJsonValue<std::string>(json, kName ));
8377
8478 return identifier;
@@ -101,6 +95,65 @@ Result<ListNamespaceResponse> ListNamespaceResponseFromJson(const nlohmann::json
10195 return response;
10296}
10397
98+ nlohmann::json ToJson (const CreateNamespaceRequest& request) {
99+ nlohmann::json json;
100+ json[kNamespaces ] = request.namespaces ;
101+ SetOptionalField (json, kProperties , request.properties );
102+ return json;
103+ }
104+
105+ Result<CreateNamespaceRequest> CreateNamespaceRequestFromJson (
106+ const nlohmann::json& json) {
107+ CreateNamespaceRequest request;
108+
109+ ICEBERG_ASSIGN_OR_RAISE (request.namespaces ,
110+ GetJsonValue<std::vector<std::string>>(json, kNamespaces ));
111+ using MapType = std::unordered_map<std::string, std::string>;
112+ ICEBERG_ASSIGN_OR_RAISE (request.properties ,
113+ GetJsonValueOptional<MapType>(json, kProperties ));
114+
115+ return request;
116+ }
117+
118+ nlohmann::json ToJson (const CreateNamespaceResponse& response) {
119+ nlohmann::json json;
120+ json[kNamespaces ] = response.namespaces ;
121+ SetOptionalField (json, kProperties , response.properties );
122+ return json;
123+ }
124+
125+ Result<CreateNamespaceResponse> CreateNamespaceResponseFromJson (
126+ const nlohmann::json& json) {
127+ CreateNamespaceResponse response;
128+
129+ ICEBERG_ASSIGN_OR_RAISE (response.namespaces ,
130+ GetJsonValue<std::vector<std::string>>(json, kNamespaces ));
131+ using MapType = std::unordered_map<std::string, std::string>;
132+ ICEBERG_ASSIGN_OR_RAISE (response.properties ,
133+ GetJsonValueOptional<MapType>(json, kProperties ));
134+
135+ return response;
136+ }
137+
138+ nlohmann::json ToJson (const GetNamespacePropertiesResponse& response) {
139+ nlohmann::json json;
140+ json[kNamespaces ] = response.namespaces ;
141+ json[kProperties ] = response.properties ;
142+ return json;
143+ }
144+
145+ Result<GetNamespacePropertiesResponse> GetNamespacePropertiesResponseFromJson (
146+ const nlohmann::json& json) {
147+ GetNamespacePropertiesResponse response;
148+
149+ ICEBERG_ASSIGN_OR_RAISE (response.namespaces ,
150+ GetJsonValue<std::vector<std::string>>(json, kNamespaces ));
151+ using MapType = std::unordered_map<std::string, std::string>;
152+ ICEBERG_ASSIGN_OR_RAISE (response.properties , GetJsonValue<MapType>(json, kProperties ));
153+
154+ return response;
155+ }
156+
104157nlohmann::json ToJson (const UpdateNamespacePropsRequest& request) {
105158 nlohmann::json json;
106159 SetOptionalField (json, kRemovals , request.removals );
@@ -156,64 +209,13 @@ nlohmann::json ToJson(const ListTableResponse& response) {
156209Result<ListTableResponse> ListTableResponseFromJson (const nlohmann::json& json) {
157210 ListTableResponse response;
158211
159- ICEBERG_ASSIGN_OR_RAISE (auto identifiers_json,
160- GetJsonValue<nlohmann::json>(json, kIdentifiers ));
161-
162- for (const auto & id_json : identifiers_json) {
212+ for (const auto & id_json : json[kIdentifiers ]) {
163213 ICEBERG_ASSIGN_OR_RAISE (auto identifier, TableIdentifierFromJson (id_json));
164214 response.identifiers .push_back (std::move (identifier));
165215 }
166216 return response;
167217}
168218
169- nlohmann::json ToJson (const RenameTableRequest& request) {
170- nlohmann::json json;
171- json[kSource ] = TableIdentifierToJson (request.source );
172- json[kDestination ] = TableIdentifierToJson (request.destination );
173- return json;
174- }
175-
176- Result<RenameTableRequest> RenameTableRequestFromJson (const nlohmann::json& json) {
177- RenameTableRequest request;
178-
179- ICEBERG_ASSIGN_OR_RAISE (auto source_json, GetJsonValue<nlohmann::json>(json, kSource ));
180- ICEBERG_ASSIGN_OR_RAISE (request.source , TableIdentifierFromJson (source_json));
181-
182- ICEBERG_ASSIGN_OR_RAISE (auto dest_json,
183- GetJsonValue<nlohmann::json>(json, kDestination ));
184- ICEBERG_ASSIGN_OR_RAISE (request.destination , TableIdentifierFromJson (dest_json));
185-
186- return request;
187- }
188-
189- nlohmann::json ToJson (const LoadTableResponse& response) {
190- nlohmann::json json;
191-
192- SetOptionalField (json, kMetadataLocation , response.metadata_location );
193- json[kMetadata ] = iceberg::ToJson (response.metadata );
194- SetOptionalField (json, kConfig , response.config );
195-
196- return json;
197- }
198-
199- Result<LoadTableResponse> LoadTableResponseFromJson (const nlohmann::json& json) {
200- LoadTableResponse response;
201-
202- ICEBERG_ASSIGN_OR_RAISE (response.metadata_location ,
203- GetJsonValueOptional<std::string>(json, kMetadataLocation ));
204-
205- ICEBERG_ASSIGN_OR_RAISE (auto metadata_json,
206- GetJsonValue<nlohmann::json>(json, kMetadata ));
207- ICEBERG_ASSIGN_OR_RAISE (auto metadata_ptr,
208- iceberg::TableMetadataFromJson (metadata_json));
209- response.metadata = std::move (*metadata_ptr);
210-
211- using MapType = std::unordered_map<std::string, std::string>;
212- ICEBERG_ASSIGN_OR_RAISE (response.config , GetJsonValueOptional<MapType>(json, kConfig ));
213-
214- return response;
215- }
216-
217219nlohmann::json ToJson (const CreateTableRequest& request) {
218220 nlohmann::json json;
219221 json[kName ] = request.name ;
@@ -236,14 +238,11 @@ nlohmann::json ToJson(const CreateTableRequest& request) {
236238Result<CreateTableRequest> CreateTableRequestFromJson (const nlohmann::json& json) {
237239 CreateTableRequest request;
238240
239- // Parse required fields
240241 ICEBERG_ASSIGN_OR_RAISE (request.name , GetJsonValue<std::string>(json, kName ));
241242
242- // Parse optional location
243243 ICEBERG_ASSIGN_OR_RAISE (request.location ,
244244 GetJsonValueOptional<std::string>(json, kLocation ));
245245
246- // Parse required schema
247246 ICEBERG_ASSIGN_OR_RAISE (auto schema_json, GetJsonValue<nlohmann::json>(json, kSchema ));
248247 ICEBERG_ASSIGN_OR_RAISE (auto schema_ptr, iceberg::SchemaFromJson (schema_json));
249248 request.schema = std::move (schema_ptr);
@@ -297,4 +296,52 @@ Result<RegisterTableRequest> RegisterTableRequestFromJson(const nlohmann::json&
297296 return request;
298297}
299298
299+ nlohmann::json ToJson (const RenameTableRequest& request) {
300+ nlohmann::json json;
301+ json[kSource ] = TableIdentifierToJson (request.source );
302+ json[kDestination ] = TableIdentifierToJson (request.destination );
303+ return json;
304+ }
305+
306+ Result<RenameTableRequest> RenameTableRequestFromJson (const nlohmann::json& json) {
307+ RenameTableRequest request;
308+
309+ ICEBERG_ASSIGN_OR_RAISE (auto source_json, GetJsonValue<nlohmann::json>(json, kSource ));
310+ ICEBERG_ASSIGN_OR_RAISE (request.source , TableIdentifierFromJson (source_json));
311+
312+ ICEBERG_ASSIGN_OR_RAISE (auto dest_json,
313+ GetJsonValue<nlohmann::json>(json, kDestination ));
314+ ICEBERG_ASSIGN_OR_RAISE (request.destination , TableIdentifierFromJson (dest_json));
315+
316+ return request;
317+ }
318+
319+ nlohmann::json ToJson (const LoadTableResponse& response) {
320+ nlohmann::json json;
321+
322+ SetOptionalField (json, kMetadataLocation , response.metadata_location );
323+ json[kMetadata ] = iceberg::ToJson (response.metadata );
324+ SetOptionalField (json, kConfig , response.config );
325+
326+ return json;
327+ }
328+
329+ Result<LoadTableResponse> LoadTableResponseFromJson (const nlohmann::json& json) {
330+ LoadTableResponse response;
331+
332+ ICEBERG_ASSIGN_OR_RAISE (response.metadata_location ,
333+ GetJsonValueOptional<std::string>(json, kMetadataLocation ));
334+
335+ ICEBERG_ASSIGN_OR_RAISE (auto metadata_json,
336+ GetJsonValue<nlohmann::json>(json, kMetadata ));
337+ ICEBERG_ASSIGN_OR_RAISE (auto metadata_ptr,
338+ iceberg::TableMetadataFromJson (metadata_json));
339+ response.metadata = std::move (*metadata_ptr);
340+
341+ using MapType = std::unordered_map<std::string, std::string>;
342+ ICEBERG_ASSIGN_OR_RAISE (response.config , GetJsonValueOptional<MapType>(json, kConfig ));
343+
344+ return response;
345+ }
346+
300347} // namespace iceberg::rest
0 commit comments