| 50 | } |
| 51 | |
| 52 | NameDiffInfoMap DeserializeResponse(string const & response, LocalMapsInfo::NameVersionMap const & nameVersionMap) |
| 53 | { |
| 54 | if (response.empty()) |
| 55 | { |
| 56 | LOG(LERROR, ("Diff response shouldn't be empty.")); |
| 57 | return {}; |
| 58 | } |
| 59 | |
| 60 | base::Json const json(response.c_str()); |
| 61 | if (json.get() == nullptr) |
| 62 | return {}; |
| 63 | |
| 64 | auto const root = json_object_get(json.get(), kMwmsKey); |
| 65 | if (root == nullptr || !json_is_array(root)) |
| 66 | return {}; |
| 67 | |
| 68 | auto const count = json_array_size(root); |
| 69 | if (count == 0 || count != nameVersionMap.size()) |
| 70 | { |
| 71 | LOG(LERROR, ("Diff list size in response must be equal to mwm list size in request.")); |
| 72 | return {}; |
| 73 | } |
| 74 | |
| 75 | NameDiffInfoMap diffs; |
| 76 | |
| 77 | for (size_t i = 0; i < count; ++i) |
| 78 | { |
| 79 | auto const node = json_array_get(root, i); |
| 80 | |
| 81 | if (!node) |
| 82 | { |
| 83 | LOG(LERROR, ("Incorrect server response.")); |
| 84 | return {}; |
| 85 | } |
| 86 | |
| 87 | string name; |
| 88 | FromJSONObject(node, kNameKey, name); |
| 89 | int64_t size; |
| 90 | FromJSONObject(node, kSizeKey, size); |
| 91 | // Invalid size. The diff is not available. |
| 92 | if (size < 0) |
| 93 | continue; |
| 94 | |
| 95 | if (nameVersionMap.find(name) == nameVersionMap.end()) |
| 96 | { |
| 97 | LOG(LERROR, ("Incorrect country name in response:", name)); |
| 98 | return {}; |
| 99 | } |
| 100 | |
| 101 | DiffInfo info(size, nameVersionMap.at(name)); |
| 102 | diffs.emplace(std::move(name), std::move(info)); |
| 103 | } |
| 104 | |
| 105 | return diffs; |
| 106 | } |
| 107 | |
| 108 | NameDiffInfoMap Load(LocalMapsInfo const & info) |
| 109 | { |