2323#include < format>
2424#include < limits>
2525#include < map>
26+ #include < optional>
2627#include < regex>
2728#include < string>
2829#include < unordered_set>
@@ -313,6 +314,16 @@ Result<Integer> GetJsonInteger(const nlohmann::json& json, std::string_view key)
313314 return IntegerFromJson<Integer>(value, std::format (" '{}'" , key));
314315}
315316
317+ template <std::signed_integral Integer>
318+ Result<std::optional<Integer>> GetJsonIntegerOptional (const nlohmann::json& json,
319+ std::string_view key) {
320+ if (!json.contains (key) || json.at (key).is_null ()) {
321+ return std::nullopt ;
322+ }
323+ ICEBERG_ASSIGN_OR_RAISE (auto value, GetJsonInteger<Integer>(json, key));
324+ return value;
325+ }
326+
316327template <std::signed_integral Integer>
317328Result<std::vector<Integer>> IntegerVectorFromJson (const nlohmann::json& json,
318329 std::string_view key) {
@@ -818,7 +829,7 @@ Result<std::unique_ptr<Type>> StructTypeFromJson(const nlohmann::json& json) {
818829
819830Result<std::unique_ptr<Type>> ListTypeFromJson (const nlohmann::json& json) {
820831 ICEBERG_ASSIGN_OR_RAISE (auto element_type, TypeFromJson (json[kElement ]));
821- ICEBERG_ASSIGN_OR_RAISE (auto element_id, GetJsonValue <int32_t >(json, kElementId ));
832+ ICEBERG_ASSIGN_OR_RAISE (auto element_id, GetJsonInteger <int32_t >(json, kElementId ));
822833 ICEBERG_ASSIGN_OR_RAISE (auto element_required,
823834 GetJsonValue<bool >(json, kElementRequired ));
824835
@@ -834,8 +845,8 @@ Result<std::unique_ptr<Type>> MapTypeFromJson(const nlohmann::json& json) {
834845 ICEBERG_ASSIGN_OR_RAISE (
835846 auto value_type, GetJsonValue<nlohmann::json>(json, kValue ).and_then (TypeFromJson));
836847
837- ICEBERG_ASSIGN_OR_RAISE (auto key_id, GetJsonValue <int32_t >(json, kKeyId ));
838- ICEBERG_ASSIGN_OR_RAISE (auto value_id, GetJsonValue <int32_t >(json, kValueId ));
848+ ICEBERG_ASSIGN_OR_RAISE (auto key_id, GetJsonInteger <int32_t >(json, kKeyId ));
849+ ICEBERG_ASSIGN_OR_RAISE (auto value_id, GetJsonInteger <int32_t >(json, kValueId ));
839850 ICEBERG_ASSIGN_OR_RAISE (auto value_required, GetJsonValue<bool >(json, kValueRequired ));
840851
841852 SchemaField key_field (key_id, std::string (MapType::kKeyName ), std::move (key_type),
@@ -996,7 +1007,7 @@ Status ValidateTimestamptzDefaultIsUtc(const Type& type, const nlohmann::json& v
9961007Result<std::unique_ptr<SchemaField>> FieldFromJson (const nlohmann::json& json) {
9971008 ICEBERG_ASSIGN_OR_RAISE (
9981009 auto type, GetJsonValue<nlohmann::json>(json, kType ).and_then (TypeFromJson));
999- ICEBERG_ASSIGN_OR_RAISE (auto field_id, GetJsonValue <int32_t >(json, kId ));
1010+ ICEBERG_ASSIGN_OR_RAISE (auto field_id, GetJsonInteger <int32_t >(json, kId ));
10001011 ICEBERG_ASSIGN_OR_RAISE (auto name, GetJsonValue<std::string>(json, kName ));
10011012 ICEBERG_ASSIGN_OR_RAISE (auto required, GetJsonValue<bool >(json, kRequired ));
10021013 ICEBERG_ASSIGN_OR_RAISE (auto doc, GetJsonValueOrDefault<std::string>(json, kDoc ));
@@ -1029,7 +1040,7 @@ Result<std::unique_ptr<SchemaField>> FieldFromJson(const nlohmann::json& json) {
10291040
10301041Result<std::unique_ptr<Schema>> SchemaFromJson (const nlohmann::json& json) {
10311042 ICEBERG_ASSIGN_OR_RAISE (auto schema_id_opt,
1032- GetJsonValueOptional <int32_t >(json, kSchemaId ));
1043+ GetJsonIntegerOptional <int32_t >(json, kSchemaId ));
10331044 ICEBERG_ASSIGN_OR_RAISE (auto type, TypeFromJson (json));
10341045
10351046 if (type->type_id () != TypeId::kStruct ) [[unlikely]] {
@@ -1043,9 +1054,11 @@ Result<std::unique_ptr<Schema>> SchemaFromJson(const nlohmann::json& json) {
10431054 fields.emplace_back (std::move (field));
10441055 }
10451056
1046- ICEBERG_ASSIGN_OR_RAISE (
1047- auto identifier_field_ids,
1048- GetJsonValueOrDefault<std::vector<int32_t >>(json, kIdentifierFieldIds ));
1057+ std::vector<int32_t > identifier_field_ids;
1058+ if (json.contains (kIdentifierFieldIds ) && !json.at (kIdentifierFieldIds ).is_null ()) {
1059+ ICEBERG_ASSIGN_OR_RAISE (identifier_field_ids,
1060+ IntegerVectorFromJson<int32_t >(json, kIdentifierFieldIds ));
1061+ }
10491062
10501063 return Schema::Make (std::move (fields), schema_id_opt.value_or (Schema::kInitialSchemaId ),
10511064 std::move (identifier_field_ids));
@@ -1078,14 +1091,14 @@ Result<std::string> ToJsonString(const PartitionSpec& partition_spec) {
10781091
10791092Result<std::unique_ptr<PartitionField>> PartitionFieldFromJson (
10801093 const nlohmann::json& json, bool allow_field_id_missing) {
1081- ICEBERG_ASSIGN_OR_RAISE (auto source_id, GetJsonValue <int32_t >(json, kSourceId ));
1094+ ICEBERG_ASSIGN_OR_RAISE (auto source_id, GetJsonInteger <int32_t >(json, kSourceId ));
10821095 int32_t field_id;
1083- if (allow_field_id_missing) {
1096+ if (allow_field_id_missing &&
1097+ (!json.contains (kFieldId ) || json.at (kFieldId ).is_null ())) {
10841098 // Partition field id in v1 is not tracked, so we use -1 to indicate that.
1085- ICEBERG_ASSIGN_OR_RAISE (field_id, GetJsonValueOrDefault<int32_t >(
1086- json, kFieldId , SchemaField::kInvalidFieldId ));
1099+ field_id = SchemaField::kInvalidFieldId ;
10871100 } else {
1088- ICEBERG_ASSIGN_OR_RAISE (field_id, GetJsonValue <int32_t >(json, kFieldId ));
1101+ ICEBERG_ASSIGN_OR_RAISE (field_id, GetJsonInteger <int32_t >(json, kFieldId ));
10891102 }
10901103 ICEBERG_ASSIGN_OR_RAISE (
10911104 auto transform,
@@ -1098,7 +1111,7 @@ Result<std::unique_ptr<PartitionField>> PartitionFieldFromJson(
10981111Result<std::unique_ptr<PartitionSpec>> PartitionSpecFromJson (
10991112 const std::shared_ptr<Schema>& schema, const nlohmann::json& json,
11001113 int32_t default_spec_id) {
1101- ICEBERG_ASSIGN_OR_RAISE (auto spec_id, GetJsonValue <int32_t >(json, kSpecId ));
1114+ ICEBERG_ASSIGN_OR_RAISE (auto spec_id, GetJsonInteger <int32_t >(json, kSpecId ));
11021115 ICEBERG_ASSIGN_OR_RAISE (auto fields, GetJsonValue<nlohmann::json>(json, kFields ));
11031116
11041117 std::vector<PartitionField> partition_fields;
@@ -1121,7 +1134,7 @@ Result<std::unique_ptr<PartitionSpec>> PartitionSpecFromJson(
11211134}
11221135
11231136Result<std::unique_ptr<PartitionSpec>> PartitionSpecFromJson (const nlohmann::json& json) {
1124- ICEBERG_ASSIGN_OR_RAISE (auto spec_id, GetJsonValue <int32_t >(json, kSpecId ));
1137+ ICEBERG_ASSIGN_OR_RAISE (auto spec_id, GetJsonInteger <int32_t >(json, kSpecId ));
11251138 ICEBERG_ASSIGN_OR_RAISE (auto fields, GetJsonValue<nlohmann::json>(json, kFields ));
11261139
11271140 std::vector<PartitionField> partition_fields;
0 commit comments