summaryrefslogtreecommitdiff
path: root/src/mbgl/style/rapidjson_conversion.hpp
blob: 1d9f88efa3c734c367787914518d1fb0b574b52c (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once

#include <mbgl/util/rapidjson.hpp>
#include <mbgl/util/feature.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() }};
}

inline 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 {};
    }
}

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