forked from organicmaps/organicmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservers_list.cpp
67 lines (58 loc) · 1.6 KB
/
servers_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "platform/servers_list.hpp"
#include "platform/http_request.hpp"
#include "platform/platform.hpp"
#include "base/logging.hpp"
#include "base/assert.hpp"
#include "cppjansson/cppjansson.hpp"
namespace downloader
{
std::optional<MetaConfig> ParseMetaConfig(std::string const & jsonStr)
{
MetaConfig outMetaConfig;
try
{
const base::Json root(jsonStr.c_str());
const json_t * servers;
if (json_is_object(root.get()))
{
// New format:
// {
// "servers": ["http://url1", "http://url2", "http://url3"],
// "settings": {
// "key1": "value1",
// "key2": "http://url"
// }
// }
json_t * settings = json_object_get(root.get(), "settings");
const char * key;
const json_t * value;
json_object_foreach(settings, key, value)
{
const char * valueStr = json_string_value(value);
if (key && value)
outMetaConfig.m_settings[key] = valueStr;
}
servers = json_object_get(root.get(), "servers");
}
else
{
// Old format:
// ["http://url1", "http://url2", "http://url3"]
servers = root.get();
}
for (size_t i = 0; i < json_array_size(servers); ++i)
{
char const * url = json_string_value(json_array_get(servers, i));
if (url)
outMetaConfig.m_serversList.push_back(url);
}
}
catch (base::Json::Exception const & ex)
{
LOG(LWARNING, ("Can't parse meta configuration:", ex.Msg(), jsonStr));
}
if (outMetaConfig.m_serversList.empty())
return std::nullopt;
return outMetaConfig;
}
} // namespace downloader