|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/puffin/puffin_json_internal.h" |
| 21 | + |
| 22 | +#include <nlohmann/json.hpp> |
| 23 | + |
| 24 | +#include "iceberg/util/json_util_internal.h" |
| 25 | +#include "iceberg/util/macros.h" |
| 26 | + |
| 27 | +namespace iceberg::puffin { |
| 28 | + |
| 29 | +namespace { |
| 30 | +constexpr std::string_view kBlobs = "blobs"; |
| 31 | +constexpr std::string_view kProperties = "properties"; |
| 32 | +constexpr std::string_view kType = "type"; |
| 33 | +constexpr std::string_view kFields = "fields"; |
| 34 | +constexpr std::string_view kSnapshotId = "snapshot-id"; |
| 35 | +constexpr std::string_view kSequenceNumber = "sequence-number"; |
| 36 | +constexpr std::string_view kOffset = "offset"; |
| 37 | +constexpr std::string_view kLength = "length"; |
| 38 | +constexpr std::string_view kCompressionCodec = "compression-codec"; |
| 39 | +} // namespace |
| 40 | + |
| 41 | +nlohmann::json ToJson(const BlobMetadata& blob_metadata) { |
| 42 | + nlohmann::json json; |
| 43 | + json[kType] = blob_metadata.type; |
| 44 | + json[kFields] = blob_metadata.input_fields; |
| 45 | + json[kSnapshotId] = blob_metadata.snapshot_id; |
| 46 | + json[kSequenceNumber] = blob_metadata.sequence_number; |
| 47 | + json[kOffset] = blob_metadata.offset; |
| 48 | + json[kLength] = blob_metadata.length; |
| 49 | + |
| 50 | + SetOptionalStringField(json, kCompressionCodec, blob_metadata.compression_codec); |
| 51 | + SetContainerField(json, kProperties, blob_metadata.properties); |
| 52 | + |
| 53 | + return json; |
| 54 | +} |
| 55 | + |
| 56 | +Result<BlobMetadata> BlobMetadataFromJson(const nlohmann::json& json) { |
| 57 | + BlobMetadata blob_metadata; |
| 58 | + |
| 59 | + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.type, GetJsonValue<std::string>(json, kType)); |
| 60 | + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.input_fields, |
| 61 | + GetJsonValue<std::vector<int32_t>>(json, kFields)); |
| 62 | + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.snapshot_id, |
| 63 | + GetJsonValue<int64_t>(json, kSnapshotId)); |
| 64 | + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.sequence_number, |
| 65 | + GetJsonValue<int64_t>(json, kSequenceNumber)); |
| 66 | + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.offset, GetJsonValue<int64_t>(json, kOffset)); |
| 67 | + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.length, GetJsonValue<int64_t>(json, kLength)); |
| 68 | + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.compression_codec, |
| 69 | + GetJsonValueOrDefault<std::string>(json, kCompressionCodec)); |
| 70 | + ICEBERG_ASSIGN_OR_RAISE(blob_metadata.properties, |
| 71 | + FromJsonMap<std::string>(json, kProperties)); |
| 72 | + |
| 73 | + return blob_metadata; |
| 74 | +} |
| 75 | + |
| 76 | +nlohmann::json ToJson(const FileMetadata& file_metadata) { |
| 77 | + nlohmann::json json; |
| 78 | + |
| 79 | + nlohmann::json blobs_json = nlohmann::json::array(); |
| 80 | + for (const auto& blob : file_metadata.blobs) { |
| 81 | + blobs_json.push_back(ToJson(blob)); |
| 82 | + } |
| 83 | + json[kBlobs] = std::move(blobs_json); |
| 84 | + |
| 85 | + SetContainerField(json, kProperties, file_metadata.properties); |
| 86 | + |
| 87 | + return json; |
| 88 | +} |
| 89 | + |
| 90 | +Result<FileMetadata> FileMetadataFromJson(const nlohmann::json& json) { |
| 91 | + FileMetadata file_metadata; |
| 92 | + |
| 93 | + ICEBERG_ASSIGN_OR_RAISE(auto blobs_json, GetJsonValue<nlohmann::json>(json, kBlobs)); |
| 94 | + if (!blobs_json.is_array()) { |
| 95 | + return JsonParseError("Cannot parse blobs from non-array: {}", |
| 96 | + SafeDumpJson(blobs_json)); |
| 97 | + } |
| 98 | + |
| 99 | + for (const auto& blob_json : blobs_json) { |
| 100 | + ICEBERG_ASSIGN_OR_RAISE(auto blob, BlobMetadataFromJson(blob_json)); |
| 101 | + file_metadata.blobs.push_back(std::move(blob)); |
| 102 | + } |
| 103 | + |
| 104 | + ICEBERG_ASSIGN_OR_RAISE(file_metadata.properties, |
| 105 | + FromJsonMap<std::string>(json, kProperties)); |
| 106 | + |
| 107 | + return file_metadata; |
| 108 | +} |
| 109 | + |
| 110 | +std::string ToJsonString(const FileMetadata& file_metadata, bool pretty) { |
| 111 | + auto json = ToJson(file_metadata); |
| 112 | + return pretty ? json.dump(2) : json.dump(); |
| 113 | +} |
| 114 | + |
| 115 | +Result<FileMetadata> FileMetadataFromJsonString(std::string_view json_string) { |
| 116 | + if (json_string.empty()) { |
| 117 | + return JsonParseError("Cannot parse empty JSON string"); |
| 118 | + } |
| 119 | + try { |
| 120 | + auto json = nlohmann::json::parse(json_string); |
| 121 | + return FileMetadataFromJson(json); |
| 122 | + } catch (const nlohmann::json::parse_error& e) { |
| 123 | + return JsonParseError("Failed to parse JSON: {}", e.what()); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +} // namespace iceberg::puffin |
0 commit comments