Files
MapLibreParticleLab/platform/android/src/gson/json_element.cpp
Tobrun 44ce5ab087 Rework expression conversion (#11490)
* [android] - rework expression conversion
2018-04-10 11:09:42 +02:00

90 lines
3.2 KiB
C++

#include "json_element.hpp"
#include "json_array.hpp"
#include "json_object.hpp"
#include "json_primitive.hpp"
#include <mapbox/geometry/feature.hpp>
#include <mapbox/variant.hpp>
namespace mbgl {
namespace android {
namespace gson {
/**
* Turn mapbox::geometry::value into Java Gson JsonElement
*/
class JsonElementEvaluator {
public:
jni::JNIEnv& env;
jni::Object<JsonElement> operator()(const JsonPrimitive::value value) const {
return jni::Cast(env, JsonPrimitive::New(env, value), JsonElement::javaClass);
}
jni::Object<JsonElement> operator()(const std::vector<mapbox::geometry::value> &values) const {
return jni::Cast(env, JsonArray::New(env, values), JsonElement::javaClass);
}
jni::Object<JsonElement> operator()(const std::unordered_map<std::string, mapbox::geometry::value> &values) const {
return jni::Cast(env, JsonObject::New(env, values), JsonElement::javaClass);
}
};
jni::Object<JsonElement> JsonElement::New(jni::JNIEnv& env, const mapbox::geometry::value& value) {
JsonElementEvaluator evaluator { env } ;
return mapbox::geometry::value::visit(value, evaluator);
}
mapbox::geometry::value JsonElement::convert(jni::JNIEnv &env, jni::Object<JsonElement> jsonElement) {
mapbox::geometry::value value;
if (jsonElement) {
if (isJsonPrimitive(env, jsonElement)) {
auto primitive = JsonPrimitive::convert(env, jni::Cast(env, jsonElement, JsonPrimitive::javaClass));
value = mapbox::util::apply_visitor([](auto t) { return mapbox::geometry::value { t }; }, primitive);
} else if (isJsonObject(env, jsonElement)) {
mapbox::geometry::property_map map = JsonObject::convert(env, jni::Cast(env, jsonElement, JsonObject::javaClass));
value = mapbox::util::recursive_wrapper<std::unordered_map<std::string, mapbox::geometry::value>> { map } ;
} else if (isJsonArray(env, jsonElement)) {
value = JsonArray::convert(env, jni::Cast(env, jsonElement, JsonArray::javaClass));
} else {
value = mapbox::geometry::null_value;
}
}
return value;
}
bool JsonElement::isJsonObject(JNIEnv& env, jni::Object<JsonElement> jsonElement) {
static auto method = JsonElement::javaClass.GetMethod<jni::jboolean ()>(env, "isJsonObject");
return jsonElement.Call(env, method);
}
bool JsonElement::isJsonArray(JNIEnv& env, jni::Object<JsonElement> jsonElement) {
static auto method = JsonElement::javaClass.GetMethod<jni::jboolean ()>(env, "isJsonArray");
return jsonElement.Call(env, method);
}
bool JsonElement::isJsonPrimitive(JNIEnv& env, jni::Object<JsonElement> jsonElement) {
static auto method = JsonElement::javaClass.GetMethod<jni::jboolean ()>(env, "isJsonPrimitive");
return jsonElement.Call(env, method);
}
bool JsonElement::isJsonNull(JNIEnv& env, jni::Object<JsonElement> jsonElement) {
static auto method = JsonElement::javaClass.GetMethod<jni::jboolean ()>(env, "isJsonNull");
return jsonElement.Call(env, method);
}
void JsonElement::registerNative(jni::JNIEnv &env) {
// Lookup the class
javaClass = *jni::Class<JsonElement>::Find(env).NewGlobalRef(env).release();
}
jni::Class<JsonElement> JsonElement::javaClass;
} // namespace gson
} // namespace android
} // namespace mbgl