| 107 | } |
| 108 | |
| 109 | bool CouchbaseManifestManager::jsonToCollectionManifest( |
| 110 | const std::string& json, |
| 111 | CouchbaseManifestManager::CollectionManifest* manifest) { |
| 112 | if (manifest == nullptr) { |
| 113 | DEBUG_PRINT("Invalid input: manifest is null"); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // Clear existing data |
| 118 | manifest->uid.clear(); |
| 119 | manifest->scope_to_collection_id_map.clear(); |
| 120 | |
| 121 | if (json.empty()) { |
| 122 | DEBUG_PRINT("JSON std::string is empty"); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | // Parse JSON using RapidJSON |
| 127 | BUTIL_RAPIDJSON_NAMESPACE::Document document; |
| 128 | document.Parse(json.c_str()); |
| 129 | |
| 130 | if (document.HasParseError()) { |
| 131 | DEBUG_PRINT("Failed to parse JSON: " << document.GetParseError()); |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | if (!document.IsObject()) { |
| 136 | DEBUG_PRINT("JSON root is not an object"); |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | // Extract uid |
| 141 | if (document.HasMember("uid") && document["uid"].IsString()) { |
| 142 | manifest->uid = document["uid"].GetString(); |
| 143 | } else { |
| 144 | DEBUG_PRINT("Missing or invalid 'uid' field in JSON"); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | // Extract scopes |
| 149 | if (!document.HasMember("scopes") || !document["scopes"].IsArray()) { |
| 150 | DEBUG_PRINT("Missing or invalid 'scopes' field in JSON"); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | const BUTIL_RAPIDJSON_NAMESPACE::Value& scopes = document["scopes"]; |
| 155 | for (BUTIL_RAPIDJSON_NAMESPACE::SizeType i = 0; i < scopes.Size(); ++i) { |
| 156 | const BUTIL_RAPIDJSON_NAMESPACE::Value& scope = scopes[i]; |
| 157 | |
| 158 | if (!scope.IsObject()) { |
| 159 | DEBUG_PRINT("Scope at index " << i << " is not an object"); |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | // Extract scope name |
| 164 | if (!scope.HasMember("name") || !scope["name"].IsString()) { |
| 165 | DEBUG_PRINT("Missing or invalid 'name' field in scope at index " << i); |
| 166 | return false; |