@@ -65,9 +65,7 @@ constexpr std::string_view kIdentifiers = "identifiers";
6565nlohmann::json ToJson (const CreateNamespaceRequest& request) {
6666 nlohmann::json json;
6767 json[kNamespace ] = request.namespace_ .levels ;
68- if (!request.properties .empty ()) {
69- json[kProperties ] = request.properties ;
70- }
68+ SetContainerField (json, kProperties , request.properties );
7169 return json;
7270}
7371
@@ -83,15 +81,9 @@ Result<CreateNamespaceRequest> CreateNamespaceRequestFromJson(
8381}
8482
8583nlohmann::json ToJson (const UpdateNamespacePropertiesRequest& request) {
86- // Initialize as an empty object so that when all optional fields are absent we return
87- // {} instead of null
8884 nlohmann::json json = nlohmann::json::object ();
89- if (!request.removals .empty ()) {
90- json[kRemovals ] = request.removals ;
91- }
92- if (!request.updates .empty ()) {
93- json[kUpdates ] = request.updates ;
94- }
85+ SetContainerField (json, kRemovals , request.removals );
86+ SetContainerField (json, kUpdates , request.updates );
9587 return json;
9688}
9789
@@ -145,13 +137,9 @@ Result<RenameTableRequest> RenameTableRequestFromJson(const nlohmann::json& json
145137// LoadTableResult (used by CreateTableResponse, LoadTableResponse)
146138nlohmann::json ToJson (const LoadTableResult& result) {
147139 nlohmann::json json;
148- if (!result.metadata_location .empty ()) {
149- json[kMetadataLocation ] = result.metadata_location ;
150- }
140+ SetOptionalStringField (json, kMetadataLocation , result.metadata_location );
151141 json[kMetadata ] = ToJson (*result.metadata );
152- if (!result.config .empty ()) {
153- json[kConfig ] = result.config ;
154- }
142+ SetContainerField (json, kConfig , result.config );
155143 return json;
156144}
157145
@@ -162,17 +150,14 @@ Result<LoadTableResult> LoadTableResultFromJson(const nlohmann::json& json) {
162150 ICEBERG_ASSIGN_OR_RAISE (auto metadata_json,
163151 GetJsonValue<nlohmann::json>(json, kMetadata ));
164152 ICEBERG_ASSIGN_OR_RAISE (result.metadata , TableMetadataFromJson (metadata_json));
165- ICEBERG_ASSIGN_OR_RAISE (
166- result.config , (GetJsonValueOrDefault<std::unordered_map<std::string, std::string>>(
167- json, kConfig )));
153+ ICEBERG_ASSIGN_OR_RAISE (result.config ,
154+ GetJsonValueOrDefault<decltype (result.config )>(json, kConfig ));
168155 return result;
169156}
170157
171158nlohmann::json ToJson (const ListNamespacesResponse& response) {
172159 nlohmann::json json;
173- if (!response.next_page_token .empty ()) {
174- json[kNextPageToken ] = response.next_page_token ;
175- }
160+ SetOptionalStringField (json, kNextPageToken , response.next_page_token );
176161 nlohmann::json namespaces = nlohmann::json::array ();
177162 for (const auto & ns : response.namespaces ) {
178163 namespaces.push_back (ToJson (ns));
@@ -198,9 +183,7 @@ Result<ListNamespacesResponse> ListNamespacesResponseFromJson(
198183nlohmann::json ToJson (const CreateNamespaceResponse& response) {
199184 nlohmann::json json;
200185 json[kNamespace ] = response.namespace_ .levels ;
201- if (!response.properties .empty ()) {
202- json[kProperties ] = response.properties ;
203- }
186+ SetContainerField (json, kProperties , response.properties );
204187 return json;
205188}
206189
@@ -218,9 +201,7 @@ Result<CreateNamespaceResponse> CreateNamespaceResponseFromJson(
218201nlohmann::json ToJson (const GetNamespaceResponse& response) {
219202 nlohmann::json json;
220203 json[kNamespace ] = response.namespace_ .levels ;
221- if (!response.properties .empty ()) {
222- json[kProperties ] = response.properties ;
223- }
204+ SetContainerField (json, kProperties , response.properties );
224205 return json;
225206}
226207
@@ -238,29 +219,25 @@ nlohmann::json ToJson(const UpdateNamespacePropertiesResponse& response) {
238219 nlohmann::json json;
239220 json[kUpdated ] = response.updated ;
240221 json[kRemoved ] = response.removed ;
241- if (!response.missing .empty ()) {
242- json[kMissing ] = response.missing ;
243- }
222+ SetContainerField (json, kMissing , response.missing );
244223 return json;
245224}
246225
247226Result<UpdateNamespacePropertiesResponse> UpdateNamespacePropertiesResponseFromJson (
248227 const nlohmann::json& json) {
249228 UpdateNamespacePropertiesResponse response;
250- ICEBERG_ASSIGN_OR_RAISE (response. updated ,
251- GetJsonValue <std::vector<std::string>>(json, kUpdated ));
252- ICEBERG_ASSIGN_OR_RAISE (response. removed ,
253- GetJsonValue <std::vector<std::string>>(json, kRemoved ));
229+ ICEBERG_ASSIGN_OR_RAISE (
230+ response. updated , GetJsonValueOrDefault <std::vector<std::string>>(json, kUpdated ));
231+ ICEBERG_ASSIGN_OR_RAISE (
232+ response. removed , GetJsonValueOrDefault <std::vector<std::string>>(json, kRemoved ));
254233 ICEBERG_ASSIGN_OR_RAISE (
255234 response.missing , GetJsonValueOrDefault<std::vector<std::string>>(json, kMissing ));
256235 return response;
257236}
258237
259238nlohmann::json ToJson (const ListTablesResponse& response) {
260239 nlohmann::json json;
261- if (!response.next_page_token .empty ()) {
262- json[kNextPageToken ] = response.next_page_token ;
263- }
240+ SetOptionalStringField (json, kNextPageToken , response.next_page_token );
264241 nlohmann::json identifiers_json = nlohmann::json::array ();
265242 for (const auto & identifier : response.identifiers ) {
266243 identifiers_json.push_back (ToJson (identifier));
@@ -282,4 +259,21 @@ Result<ListTablesResponse> ListTablesResponseFromJson(const nlohmann::json& json
282259 return response;
283260}
284261
262+ #define ICEBERG_DEFINE_FROM_JSON (Model ) \
263+ template <> \
264+ Result<Model> FromJson<Model>(const nlohmann::json& json) { \
265+ return Model##FromJson (json); \
266+ }
267+
268+ ICEBERG_DEFINE_FROM_JSON (ListNamespacesResponse)
269+ ICEBERG_DEFINE_FROM_JSON (CreateNamespaceRequest)
270+ ICEBERG_DEFINE_FROM_JSON (CreateNamespaceResponse)
271+ ICEBERG_DEFINE_FROM_JSON (GetNamespaceResponse)
272+ ICEBERG_DEFINE_FROM_JSON (UpdateNamespacePropertiesRequest)
273+ ICEBERG_DEFINE_FROM_JSON (UpdateNamespacePropertiesResponse)
274+ ICEBERG_DEFINE_FROM_JSON (ListTablesResponse)
275+ ICEBERG_DEFINE_FROM_JSON (LoadTableResult)
276+ ICEBERG_DEFINE_FROM_JSON (RegisterTableRequest)
277+ ICEBERG_DEFINE_FROM_JSON (RenameTableRequest)
278+
285279} // namespace iceberg::rest
0 commit comments