summaryrefslogtreecommitdiff
path: root/src/mbgl/style/rapidjson_conversion.hpp
blob: 93577ac8b2f035488efcd1292fb600bd14e25c80 (plain)
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
#pragma once

#include <mbgl/util/rapidjson.hpp>

namespace mbgl {
namespace style {
namespace conversion {

inline bool isArray(const JSValue& value) {
    return value.IsArray();
}

inline std::size_t arrayLength(const JSValue& value) {
    return value.Size();
}

inline const JSValue& arrayMember(const JSValue& value, std::size_t i) {
    return value[rapidjson::SizeType(i)];
}

inline bool isObject(const JSValue& value) {
    return value.IsObject();
}

inline const JSValue* objectMember(const JSValue& value, const char * name) {
    if (!value.HasMember(name)) {
        return nullptr;
    }
    return &value[name];
}

inline optional<bool> toBool(const JSValue& value) {
    if (!value.IsBool()) {
        return {};
    }
    return value.GetBool();
}

inline optional<float> toNumber(const JSValue& value) {
    if (!value.IsNumber()) {
        return {};
    }
    return value.GetDouble();
}

inline optional<std::string> toString(const JSValue& value) {
    if (!value.IsString()) {
        return {};
    }
    return {{ value.GetString(), value.GetStringLength() }};
}

} // namespace conversion
} // namespace style
} // namespace mbgl