@@ -315,27 +315,28 @@ Result<std::unique_ptr<SortOrder>> SortOrderFromJson(const nlohmann::json& json)
315315 return SortOrder::Make (parsed.order_id , std::move (parsed.fields ));
316316}
317317
318- nlohmann::json ToJson (const SchemaField& field) {
318+ Result< nlohmann::json> ToJson (const SchemaField& field) {
319319 nlohmann::json json;
320320 json[kId ] = field.field_id ();
321321 json[kName ] = field.name ();
322322 json[kRequired ] = !field.optional ();
323- json[kType ] = ToJson (*field.type ());
323+ ICEBERG_ASSIGN_OR_RAISE ( json[kType ], ToJson (*field.type () ));
324324 if (!field.doc ().empty ()) {
325325 json[kDoc ] = field.doc ();
326326 }
327327 return json;
328328}
329329
330- nlohmann::json ToJson (const Type& type) {
330+ Result< nlohmann::json> ToJson (const Type& type) {
331331 switch (type.type_id ()) {
332332 case TypeId::kStruct : {
333333 const auto & struct_type = internal::checked_cast<const StructType&>(type);
334334 nlohmann::json json;
335335 json[kType ] = kStruct ;
336336 nlohmann::json fields_json = nlohmann::json::array ();
337337 for (const auto & field : struct_type.fields ()) {
338- fields_json.push_back (ToJson (field));
338+ ICEBERG_ASSIGN_OR_RAISE (auto field_json, ToJson (field));
339+ fields_json.push_back (std::move (field_json));
339340 // TODO(gangwu): add default values
340341 }
341342 json[kFields ] = fields_json;
@@ -349,7 +350,7 @@ nlohmann::json ToJson(const Type& type) {
349350 const auto & element_field = list_type.fields ().front ();
350351 json[kElementId ] = element_field.field_id ();
351352 json[kElementRequired ] = !element_field.optional ();
352- json[kElement ] = ToJson (*element_field.type ());
353+ ICEBERG_ASSIGN_OR_RAISE ( json[kElement ], ToJson (*element_field.type () ));
353354 return json;
354355 }
355356 case TypeId::kMap : {
@@ -359,12 +360,12 @@ nlohmann::json ToJson(const Type& type) {
359360
360361 const auto & key_field = map_type.key ();
361362 json[kKeyId ] = key_field.field_id ();
362- json[kKey ] = ToJson (*key_field.type ());
363+ ICEBERG_ASSIGN_OR_RAISE ( json[kKey ], ToJson (*key_field.type () ));
363364
364365 const auto & value_field = map_type.value ();
365366 json[kValueId ] = value_field.field_id ();
366367 json[kValueRequired ] = !value_field.optional ();
367- json[kValue ] = ToJson (*value_field.type ());
368+ ICEBERG_ASSIGN_OR_RAISE ( json[kValue ], ToJson (*value_field.type () ));
368369 return json;
369370 }
370371 case TypeId::kBoolean :
@@ -416,8 +417,9 @@ nlohmann::json ToJson(const Type& type) {
416417 std::unreachable ();
417418}
418419
419- nlohmann::json ToJson (const Schema& schema) {
420- nlohmann::json json = ToJson (internal::checked_cast<const Type&>(schema));
420+ Result<nlohmann::json> ToJson (const Schema& schema) {
421+ ICEBERG_ASSIGN_OR_RAISE (nlohmann::json json,
422+ ToJson (internal::checked_cast<const Type&>(schema)));
421423 json[kSchemaId ] = schema.schema_id ();
422424 if (!schema.IdentifierFieldIds ().empty ()) {
423425 json[kIdentifierFieldIds ] = schema.IdentifierFieldIds ();
@@ -426,7 +428,8 @@ nlohmann::json ToJson(const Schema& schema) {
426428}
427429
428430Result<std::string> ToJsonString (const Schema& schema) {
429- return ToJsonString (ToJson (schema));
431+ ICEBERG_ASSIGN_OR_RAISE (auto json, ToJson (schema));
432+ return ToJsonString (json);
430433}
431434
432435nlohmann::json ToJson (const SnapshotRef& ref) {
@@ -966,7 +969,7 @@ Result<EncryptedKey> EncryptedKeyFromJson(const nlohmann::json& json) {
966969 };
967970}
968971
969- nlohmann::json ToJson (const TableMetadata& table_metadata) {
972+ Result< nlohmann::json> ToJson (const TableMetadata& table_metadata) {
970973 nlohmann::json json;
971974
972975 json[kFormatVersion ] = table_metadata.format_version ;
@@ -984,15 +987,22 @@ nlohmann::json ToJson(const TableMetadata& table_metadata) {
984987 if (table_metadata.format_version == 1 ) {
985988 for (const auto & schema : table_metadata.schemas ) {
986989 if (schema->schema_id () == table_metadata.current_schema_id ) {
987- json[kSchema ] = ToJson (*schema);
990+ ICEBERG_ASSIGN_OR_RAISE ( json[kSchema ], ToJson (*schema) );
988991 break ;
989992 }
990993 }
991994 }
992995
993996 // write the current schema ID and schema list
994997 json[kCurrentSchemaId ] = table_metadata.current_schema_id ;
995- json[kSchemas ] = ToJsonList (table_metadata.schemas );
998+ // ToJson(Schema) is fallible, so the shared ToJsonList helper (which assumes an
999+ // infallible ToJson) cannot be used here; build the array with an explicit loop.
1000+ nlohmann::json schemas_json = nlohmann::json::array ();
1001+ for (const auto & schema : table_metadata.schemas ) {
1002+ ICEBERG_ASSIGN_OR_RAISE (auto schema_json, ToJson (*schema));
1003+ schemas_json.push_back (std::move (schema_json));
1004+ }
1005+ json[kSchemas ] = std::move (schemas_json);
9961006
9971007 // for older readers, continue writing the default spec as "partition-spec"
9981008 if (table_metadata.format_version == 1 ) {
@@ -1042,7 +1052,8 @@ nlohmann::json ToJson(const TableMetadata& table_metadata) {
10421052}
10431053
10441054Result<std::string> ToJsonString (const TableMetadata& table_metadata) {
1045- return ToJsonString (ToJson (table_metadata));
1055+ ICEBERG_ASSIGN_OR_RAISE (auto json, ToJson (table_metadata));
1056+ return ToJsonString (json);
10461057}
10471058
10481059namespace {
@@ -1446,7 +1457,7 @@ Result<Namespace> NamespaceFromJson(const nlohmann::json& json) {
14461457 return ns;
14471458}
14481459
1449- nlohmann::json ToJson (const TableUpdate& update) {
1460+ Result< nlohmann::json> ToJson (const TableUpdate& update) {
14501461 nlohmann::json json;
14511462 switch (update.kind ()) {
14521463 case TableUpdate::Kind::kAssignUUID : {
@@ -1465,7 +1476,7 @@ nlohmann::json ToJson(const TableUpdate& update) {
14651476 const auto & u = internal::checked_cast<const table::AddSchema&>(update);
14661477 json[kAction ] = kActionAddSchema ;
14671478 if (u.schema ()) {
1468- json[kSchema ] = ToJson (*u.schema ());
1479+ ICEBERG_ASSIGN_OR_RAISE ( json[kSchema ], ToJson (*u.schema () ));
14691480 } else {
14701481 json[kSchema ] = nlohmann::json::value_t ::null;
14711482 }
0 commit comments