summaryrefslogtreecommitdiff
path: root/platform/android/src/gson/json_array.cpp
blob: d91e323ac9efd35dcb9bca3ef4ff89ec9fc85819 (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
#include "json_array.hpp"

#include "json_element.hpp"

namespace mbgl {
namespace android {
namespace gson {

std::vector<mapbox::geometry::value> JsonArray::convert(jni::JNIEnv &env, jni::Object<JsonArray> jsonArray) {
    std::vector<mapbox::geometry::value> values;

    if (jsonArray) {
        static auto getMethod = JsonArray::javaClass.GetMethod<jni::Object<JsonElement> (jni::jint)>(env, "get");
        static auto sizeMethod = JsonArray::javaClass.GetMethod<jni::jint ()>(env, "size");

        int size = jsonArray.Call(env, sizeMethod);
        values.reserve(uint(size));

        for (int i = 0; i < size; i++) {
            auto entry = jsonArray.Call(env, getMethod, i);
            if (entry) {
                values.push_back(JsonElement::convert(env, entry));
            }
            jni::DeleteLocalRef(env, entry);
        }
    }

    return values;
}

void JsonArray::registerNative(jni::JNIEnv &env) {
    // Lookup the class
    javaClass = *jni::Class<JsonArray>::Find(env).NewGlobalRef(env).release();
}

jni::Class<JsonArray> JsonArray::javaClass;

} // namespace gson
} // namespace android
} // namespace mbgl