#include "feature.hpp" #include "geometry.hpp" #include "../gson/json_object.hpp" #include namespace mbgl { namespace android { namespace geojson { using namespace gson; mbgl::Feature Feature::convert(jni::JNIEnv& env, const jni::Object& jFeature) { static auto& javaClass = jni::Class::Singleton(env); static auto id = javaClass.GetMethod(env, "id"); static auto geometry = javaClass.GetMethod ()>(env, "geometry"); static auto properties = javaClass.GetMethod ()>(env, "properties"); auto jId = jFeature.Call(env, id); return mbgl::Feature { Geometry::convert(env, jFeature.Call(env, geometry)), JsonObject::convert(env, jFeature.Call(env, properties)), jId ? std::experimental::optional(jni::Make(env, jId)) : std::experimental::nullopt }; } /** * Turn feature identifier into std::string */ class FeatureIdVisitor { public: template std::string operator()(const T& i) const { return std::to_string(i); } std::string operator()(const std::string& i) const { return i; } std::string operator()(const std::nullptr_t&) const { return ""; } }; jni::Local> convertFeature(jni::JNIEnv& env, const mbgl::Feature& value) { static auto& javaClass = jni::Class::Singleton(env); static auto method = javaClass.GetStaticMethod (jni::Object, jni::Object, jni::String)>(env, "fromGeometry"); return javaClass.Call(env, method, Geometry::New(env, value.geometry), JsonObject::New(env, value.properties), jni::Make(env, value.id ? value.id.value().match(FeatureIdVisitor()) : "")); } jni::Local>> Feature::convert(jni::JNIEnv& env, const std::vector& value) { auto features = jni::Array>::New(env, value.size()); for (size_t i = 0; i < value.size(); i = i + 1) { features.Set(env, i, convertFeature(env, value.at(i))); } return features; } void Feature::registerNative(jni::JNIEnv& env) { jni::Class::Singleton(env); } } // namespace geojson } // namespace android } // namespace mbgl