1// 2// serde_json.mm 3// 4// Copyright © 2024 Apple Inc. All rights reserved. 5// 6// Please refer to the license found in the LICENSE file in the root directory of the source tree. 7 8#import <serde_json.h> 9 10#import <asset.h> 11#import <objc_json_serde.h> 12#import <model_metadata.h> 13 14namespace { 15struct FileInfoKeys { 16 constexpr static std::string_view kRelativePath = "relativePath"; 17 constexpr static std::string_view kSizeKey = "sizeInBytes"; 18 constexpr static std::string_view kLastModificationTimeIntervalKey = "lastModificationTimeInterval"; 19}; 20 21struct PackageInfoKeys { 22 constexpr static std::string_view kNameKey = "name"; 23 constexpr static std::string_view kFileInfosKey = "fileInfos"; 24}; 25 26struct ModelAssetKeys { 27 constexpr static std::string_view kIdentifierKey = "identifier"; 28 constexpr static std::string_view kPathKey = "path"; 29 constexpr static std::string_view kPackageInfoKey = "packageInfo"; 30}; 31 32struct ModelMetadataKeys { 33 constexpr static std::string_view kIdentifierKey = "identifier"; 34 constexpr static std::string_view kInputNamesKey = "inputNames"; 35 constexpr static std::string_view kOutputNamesKey = "outputNames"; 36}; 37} 38 39namespace executorchcoreml { 40namespace serde { 41namespace json { 42 43template <> 44struct Converter<executorchcoreml::FileInfo> { 45 static id to_json(const executorchcoreml::FileInfo& file_info) { 46 return @{ 47 to_string(FileInfoKeys::kRelativePath) : to_json_value(file_info.relative_path), 48 to_string(FileInfoKeys::kSizeKey) : to_json_value(file_info.size_in_bytes), 49 to_string(FileInfoKeys::kLastModificationTimeIntervalKey) :to_json_value(file_info.last_modification_time_interval) 50 }; 51 } 52 53 static void from_json(id json, executorchcoreml::FileInfo& file_info) { 54 NSDictionary<NSString *, id> *json_dict = SAFE_CAST(json, NSDictionary); 55 if (!json_dict) { 56 return; 57 } 58 59 from_json_value(json_dict[to_string(FileInfoKeys::kRelativePath)], file_info.relative_path); 60 from_json_value(json_dict[to_string(FileInfoKeys::kSizeKey)], file_info.size_in_bytes); 61 from_json_value(json_dict[to_string(FileInfoKeys::kLastModificationTimeIntervalKey)], file_info.last_modification_time_interval); 62 } 63}; 64 65template <> 66struct Converter<executorchcoreml::PackageInfo> { 67 static id to_json(const executorchcoreml::PackageInfo& package_info) { 68 return @{ 69 to_string(PackageInfoKeys::kNameKey) : to_json_value(package_info.name), 70 to_string(PackageInfoKeys::kFileInfosKey) : to_json_value(package_info.file_infos) 71 }; 72 } 73 74 static void from_json(id json, executorchcoreml::PackageInfo& package_info) { 75 NSDictionary<NSString *, id> *json_dict = SAFE_CAST(json, NSDictionary); 76 if (!json_dict) { 77 return; 78 } 79 80 from_json_value(json_dict[to_string(PackageInfoKeys::kNameKey)], package_info.name); 81 from_json_value(json_dict[to_string(PackageInfoKeys::kFileInfosKey)], package_info.file_infos); 82 } 83}; 84 85template <> 86struct Converter<executorchcoreml::Asset> { 87 static id to_json(const executorchcoreml::Asset& asset) { 88 return @{ 89 to_string(ModelAssetKeys::kIdentifierKey) : to_json_value(asset.identifier), 90 to_string(ModelAssetKeys::kPathKey) : to_json_value(asset.path), 91 to_string(ModelAssetKeys::kPackageInfoKey) : to_json_value(asset.package_info) 92 }; 93 } 94 95 static void from_json(id json, executorchcoreml::Asset& asset) { 96 NSDictionary<NSString *, id> *json_dict = SAFE_CAST(json, NSDictionary); 97 if (!json_dict) { 98 return; 99 } 100 101 from_json_value(json_dict[to_string(ModelAssetKeys::kIdentifierKey)], asset.identifier); 102 from_json_value(json_dict[to_string(ModelAssetKeys::kPathKey)], asset.path); 103 from_json_value(json_dict[to_string(ModelAssetKeys::kPackageInfoKey)], asset.package_info); 104 } 105}; 106 107template <> 108struct Converter<executorchcoreml::ModelMetadata> { 109 static id to_json(const executorchcoreml::ModelMetadata& metadata) { 110 return @{ 111 to_string(ModelMetadataKeys::kIdentifierKey) : to_json_value(metadata.identifier), 112 to_string(ModelMetadataKeys::kInputNamesKey) : to_json_value(metadata.input_names), 113 to_string(ModelMetadataKeys::kOutputNamesKey) :to_json_value(metadata.output_names) 114 }; 115 } 116 117 static void from_json(id json, executorchcoreml::ModelMetadata& metadata) { 118 NSDictionary<NSString *, id> *json_dict = SAFE_CAST(json, NSDictionary); 119 if (!json_dict) { 120 return; 121 } 122 123 from_json_value(json_dict[to_string(ModelMetadataKeys::kIdentifierKey)], metadata.identifier); 124 from_json_value(json_dict[to_string(ModelMetadataKeys::kInputNamesKey)], metadata.input_names); 125 from_json_value(json_dict[to_string(ModelMetadataKeys::kOutputNamesKey)], metadata.output_names); 126 } 127}; 128 129std::string to_json_string(const Asset& asset) { 130 id json = Converter<Asset>::to_json(asset); 131 return to_json_string(json); 132} 133 134void from_json_string(const std::string& json_string, Asset& asset) { 135 id json = to_json_object(json_string); 136 Converter<Asset>::from_json(json, asset); 137} 138 139std::string to_json_string(const ModelMetadata& metdata) { 140 id json = Converter<ModelMetadata>::to_json(metdata); 141 return to_json_string(json); 142} 143 144void from_json_string(const std::string& json_string, ModelMetadata& metadata) { 145 id json = to_json_object(json_string); 146 Converter<ModelMetadata>::from_json(json, metadata); 147} 148 149} // namespace json 150} // namespace serde 151} // namespace executorchcoreml 152