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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#pragma once
#include <mbgl/util/rapidjson.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mapbox/geojson.hpp>
#include <mapbox/geojson/rapidjson.hpp>
namespace mbgl {
namespace style {
namespace conversion {
template <>
class ConversionTraits<const JSValue*> {
public:
static bool isUndefined(const JSValue* value) {
return value->IsNull();
}
static bool isArray(const JSValue* value) {
return value->IsArray();
}
static std::size_t arrayLength(const JSValue* value) {
return value->Size();
}
static const JSValue* arrayMember(const JSValue* value, std::size_t i) {
return &(*value)[rapidjson::SizeType(i)];
}
static bool isObject(const JSValue* value) {
return value->IsObject();
}
static optional<const JSValue*> objectMember(const JSValue* value, const char * name) {
if (!value->HasMember(name)) {
return optional<const JSValue*>();
}
const JSValue* const& member = &(*value)[name];
return {member};
}
template <class Fn>
static optional<Error> eachMember(const JSValue* value, Fn&& fn) {
assert(value->IsObject());
for (const auto& property : value->GetObject()) {
optional<Error> result =
fn({ property.name.GetString(), property.name.GetStringLength() }, &property.value);
if (result) {
return result;
}
}
return {};
}
static optional<bool> toBool(const JSValue* value) {
if (!value->IsBool()) {
return {};
}
return value->GetBool();
}
static optional<float> toNumber(const JSValue* value) {
if (!value->IsNumber()) {
return {};
}
return value->GetDouble();
}
static optional<double> toDouble(const JSValue* value) {
if (!value->IsNumber()) {
return {};
}
return value->GetDouble();
}
static optional<std::string> toString(const JSValue* value) {
if (!value->IsString()) {
return {};
}
return {{ value->GetString(), value->GetStringLength() }};
}
static optional<Value> toValue(const JSValue* value) {
switch (value->GetType()) {
case rapidjson::kNullType:
case rapidjson::kFalseType:
return { false };
case rapidjson::kTrueType:
return { true };
case rapidjson::kStringType:
return { std::string { value->GetString(), value->GetStringLength() } };
case rapidjson::kNumberType:
if (value->IsUint64()) return { value->GetUint64() };
if (value->IsInt64()) return { value->GetInt64() };
return { value->GetDouble() };
default:
return {};
}
}
static optional<GeoJSON> toGeoJSON(const JSValue* value, Error& error) {
try {
return mapbox::geojson::convert(*value);
} catch (const std::exception& ex) {
error = { ex.what() };
return {};
}
}
};
template <class T, class...Args>
optional<T> convert(const JSValue& value, Error& error, Args&&...args) {
return convert<T>(Convertible(&value), error, std::forward<Args>(args)...);
}
} // namespace conversion
} // namespace style
} // namespace mbgl
|