diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/CircleLayer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/CircleLayer.java index 8f1c00f1b54..94929398d5e 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/CircleLayer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/CircleLayer.java @@ -99,7 +99,7 @@ public CircleLayer withFilter(Expression filter) { @Nullable public Expression getFilter() { Expression expression = null; - JsonArray array = nativeGetFilter(); + JsonArray array = (JsonArray) nativeGetFilter(); if (array != null) { expression = Expression.Converter.convert(array); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/FillExtrusionLayer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/FillExtrusionLayer.java index 34f062b4d6b..29e2b49d766 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/FillExtrusionLayer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/FillExtrusionLayer.java @@ -99,7 +99,7 @@ public FillExtrusionLayer withFilter(Expression filter) { @Nullable public Expression getFilter() { Expression expression = null; - JsonArray array = nativeGetFilter(); + JsonArray array = (JsonArray) nativeGetFilter(); if (array != null) { expression = Expression.Converter.convert(array); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/FillLayer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/FillLayer.java index 1865349c42f..ed780811c06 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/FillLayer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/FillLayer.java @@ -99,7 +99,7 @@ public FillLayer withFilter(Expression filter) { @Nullable public Expression getFilter() { Expression expression = null; - JsonArray array = nativeGetFilter(); + JsonArray array = (JsonArray) nativeGetFilter(); if (array != null) { expression = Expression.Converter.convert(array); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/HeatmapLayer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/HeatmapLayer.java index a8d07bdbb49..42b42107104 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/HeatmapLayer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/HeatmapLayer.java @@ -99,7 +99,7 @@ public HeatmapLayer withFilter(Expression filter) { @Nullable public Expression getFilter() { Expression expression = null; - JsonArray array = nativeGetFilter(); + JsonArray array = (JsonArray) nativeGetFilter(); if (array != null) { expression = Expression.Converter.convert(array); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java index 26fc9d32e12..411fbe44357 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java @@ -3,6 +3,7 @@ import android.support.annotation.NonNull; import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.mapbox.mapboxsdk.style.expressions.Expression; /** @@ -72,7 +73,7 @@ public void setMaxZoom(float zoom) { protected native void nativeSetFilter(Object[] filter); - protected native JsonArray nativeGetFilter(); + protected native JsonElement nativeGetFilter(); protected native void nativeSetSourceLayer(String sourceLayer); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/LineLayer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/LineLayer.java index 9dbd3aeb19b..5e6e6d38e76 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/LineLayer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/LineLayer.java @@ -99,7 +99,7 @@ public LineLayer withFilter(Expression filter) { @Nullable public Expression getFilter() { Expression expression = null; - JsonArray array = nativeGetFilter(); + JsonArray array = (JsonArray) nativeGetFilter(); if (array != null) { expression = Expression.Converter.convert(array); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/SymbolLayer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/SymbolLayer.java index bc1e02a89fd..6a2e131d7db 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/SymbolLayer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/SymbolLayer.java @@ -99,7 +99,7 @@ public SymbolLayer withFilter(Expression filter) { @Nullable public Expression getFilter() { Expression expression = null; - JsonArray array = nativeGetFilter(); + JsonArray array = (JsonArray) nativeGetFilter(); if (array != null) { expression = Expression.Converter.convert(array); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/layer.java.ejs b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/layer.java.ejs index f3941aacf6a..851a85f3d62 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/layer.java.ejs +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/layer.java.ejs @@ -119,7 +119,7 @@ public class <%- camelize(type) %>Layer extends Layer { @Nullable public Expression getFilter() { Expression expression = null; - JsonArray array = nativeGetFilter(); + JsonArray array = (JsonArray) nativeGetFilter(); if (array != null) { expression = Expression.Converter.convert(array); } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java index c42f2644782..f32aa5faf89 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java @@ -294,6 +294,13 @@ public void onDataLoaded(@NonNull FeatureCollection featureCollection) { Expression textFieldExpressionResult = symbolLayer.getTextField().getExpression(); Expression textColorExpressionResult = symbolLayer.getTextColor().getExpression(); + // log expressions + Timber.e(iconImageExpressionResult.toString()); + Timber.e(iconSizeExpressionResult.toString()); + Timber.e(textSizeExpressionResult.toString()); + Timber.e(textFieldExpressionResult.toString()); + Timber.e(textColorExpressionResult.toString()); + // reset expressions symbolLayer.setProperties( iconImage(iconImageExpressionResult), diff --git a/platform/android/config.cmake b/platform/android/config.cmake index 80d0810564e..77074dc82c0 100644 --- a/platform/android/config.cmake +++ b/platform/android/config.cmake @@ -131,7 +131,6 @@ add_library(mbgl-android STATIC platform/android/src/conversion/constant.hpp platform/android/src/conversion/conversion.hpp platform/android/src/style/conversion/function.hpp - platform/android/src/style/conversion/gson.hpp platform/android/src/style/conversion/property_value.hpp platform/android/src/style/conversion/types.hpp platform/android/src/style/conversion/types_string_values.hpp @@ -213,6 +212,8 @@ add_library(mbgl-android STATIC platform/android/src/map_renderer_runnable.hpp # Java core classes + platform/android/src/java/lang.cpp + platform/android/src/java/lang.hpp platform/android/src/java/util.cpp platform/android/src/java/util.hpp diff --git a/platform/android/src/gson/json_array.cpp b/platform/android/src/gson/json_array.cpp index d91e323ac9e..e8852d77e9f 100644 --- a/platform/android/src/gson/json_array.cpp +++ b/platform/android/src/gson/json_array.cpp @@ -6,7 +6,22 @@ namespace mbgl { namespace android { namespace gson { -std::vector JsonArray::convert(jni::JNIEnv &env, jni::Object jsonArray) { +jni::Object JsonArray::New(jni::JNIEnv& env, const std::vector& values){ + static auto constructor = JsonArray::javaClass.GetConstructor(env); + static auto addMethod = JsonArray::javaClass.GetMethod)>(env, "add"); + + auto jsonArray = JsonArray::javaClass.New(env, constructor); + + for (const auto &v : values) { + auto jsonElement = JsonElement::New(env, v); + jsonArray.Call(env, addMethod, jsonElement); + jni::DeleteLocalRef(env, jsonElement); + } + + return jsonArray; +} + +std::vector JsonArray::convert(jni::JNIEnv& env, const jni::Object jsonArray) { std::vector values; if (jsonArray) { @@ -28,7 +43,7 @@ std::vector JsonArray::convert(jni::JNIEnv &env, jni::O return values; } -void JsonArray::registerNative(jni::JNIEnv &env) { +void JsonArray::registerNative(jni::JNIEnv& env) { // Lookup the class javaClass = *jni::Class::Find(env).NewGlobalRef(env).release(); } diff --git a/platform/android/src/gson/json_array.hpp b/platform/android/src/gson/json_array.hpp index 8571ad5dba0..c9ae98692f1 100644 --- a/platform/android/src/gson/json_array.hpp +++ b/platform/android/src/gson/json_array.hpp @@ -13,6 +13,8 @@ class JsonArray : private mbgl::util::noncopyable { public: static constexpr auto Name() { return "com/google/gson/JsonArray"; }; + static jni::Object New(jni::JNIEnv&, const std::vector&); + static std::vector convert(JNIEnv&, jni::Object); static jni::Class javaClass; diff --git a/platform/android/src/gson/json_element.cpp b/platform/android/src/gson/json_element.cpp index 060b1e0fe26..5eaaf531f44 100644 --- a/platform/android/src/gson/json_element.cpp +++ b/platform/android/src/gson/json_element.cpp @@ -11,6 +11,34 @@ namespace mbgl { namespace android { namespace gson { +/** + * Turn mapbox::geometry::value into Java Gson JsonElement + */ +class JsonElementEvaluator { +public: + + jni::JNIEnv& env; + + jni::Object operator()(const JsonPrimitive::value value) const { + return jni::Cast(env, JsonPrimitive::New(env, value), JsonElement::javaClass); + } + + jni::Object operator()(const std::vector &values) const { + return jni::Cast(env, JsonArray::New(env, values), JsonElement::javaClass); + } + + jni::Object operator()(const std::unordered_map &values) const { + return jni::Cast(env, JsonObject::New(env, values), JsonElement::javaClass); + } + +}; + + +jni::Object 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) { mapbox::geometry::value value; diff --git a/platform/android/src/gson/json_element.hpp b/platform/android/src/gson/json_element.hpp index 7619350617b..d850caa5265 100644 --- a/platform/android/src/gson/json_element.hpp +++ b/platform/android/src/gson/json_element.hpp @@ -13,6 +13,8 @@ class JsonElement : private mbgl::util::noncopyable { public: static constexpr auto Name() { return "com/google/gson/JsonElement"; }; + static jni::Object New(jni::JNIEnv&, const mapbox::geometry::value&); + static mapbox::geometry::value convert(JNIEnv&, jni::Object); static bool isJsonObject(JNIEnv&, jni::Object); diff --git a/platform/android/src/gson/json_object.cpp b/platform/android/src/gson/json_object.cpp index a704dae9ddf..61b55f8b9e2 100644 --- a/platform/android/src/gson/json_object.cpp +++ b/platform/android/src/gson/json_object.cpp @@ -9,6 +9,23 @@ namespace android { namespace gson { +jni::Object JsonObject::New(jni::JNIEnv& env, const std::unordered_map& values) { + static auto constructor = JsonObject::javaClass.GetConstructor(env); + static auto addMethod = JsonObject::javaClass.GetMethod)>(env, "add"); + + jni::Object jsonObject = JsonObject::javaClass.New(env, constructor); + + for (auto &item : values) { + jni::Object jsonElement = JsonElement::New(env, item.second); + jni::String key = jni::Make(env, item.first); + jsonObject.Call(env, addMethod, key, jsonElement); + jni::DeleteLocalRef(env, jsonElement); + jni::DeleteLocalRef(env, key); + } + + return jsonObject; +} + template // void (jni::String, jni::Object) static void iterateEntrySet(jni::JNIEnv& env, jni::Object jsonObject, F callback) { // Get Set> diff --git a/platform/android/src/gson/json_object.hpp b/platform/android/src/gson/json_object.hpp index aba8e404155..4bc61e51a26 100644 --- a/platform/android/src/gson/json_object.hpp +++ b/platform/android/src/gson/json_object.hpp @@ -13,6 +13,8 @@ class JsonObject : private mbgl::util::noncopyable { public: static constexpr auto Name() { return "com/google/gson/JsonObject"; }; + static jni::Object New(jni::JNIEnv&, const std::unordered_map&); + static mapbox::geometry::property_map convert(JNIEnv&, jni::Object); static jni::Class javaClass; diff --git a/platform/android/src/gson/json_primitive.cpp b/platform/android/src/gson/json_primitive.cpp index 58d0b45fe7b..4e171c48450 100644 --- a/platform/android/src/gson/json_primitive.cpp +++ b/platform/android/src/gson/json_primitive.cpp @@ -1,9 +1,89 @@ #include "json_primitive.hpp" +#include "../java/lang.hpp" namespace mbgl { namespace android { namespace gson { +/** + * Turn mapbox::geometry::value into Java Gson JsonPrimitives + */ +class JsonPrimitiveEvaluator { +public: + + jni::JNIEnv& env; + + /** + * Create a null primitive + */ + jni::Object operator()(const mapbox::geometry::null_value_t) const { + return jni::Object(); + } + + /** + * Create a primitive containing a string value + */ + jni::Object operator()(const std::string value) const { + static auto constructor = JsonPrimitive::javaClass.GetConstructor(env); + auto jvalue = jni::Make(env, value); + auto jsonPrimitive = JsonPrimitive::javaClass.New(env, constructor, jvalue); + jni::DeleteLocalRef(env, jvalue); + return jsonPrimitive; + } + + /** + * Create a primitive containing a number value with type double + */ + jni::Object operator()(const double value) const { + static auto constructor = JsonPrimitive::javaClass.GetConstructor>(env); + auto boxedValue = java::lang::Double::valueOf(env, value); + auto number = jni::Cast(env, boxedValue, java::lang::Number::javaClass); + auto jsonPrimitive = JsonPrimitive::javaClass.New(env, constructor, number); + jni::DeleteLocalRef(env, boxedValue); + return jsonPrimitive; + } + + /** + * Create a primitive containing a number value with type long + */ + jni::Object operator()(const int64_t value) const { + static auto constructor = JsonPrimitive::javaClass.GetConstructor>(env); + auto boxedValue = java::lang::Long::valueOf(env, value); + auto number = jni::Cast(env, boxedValue, java::lang::Number::javaClass); + auto jsonPrimitive = JsonPrimitive::javaClass.New(env, constructor, number); + jni::DeleteLocalRef(env, boxedValue); + return jsonPrimitive; + } + + /** + * Create a primitive containing a number value with type long + */ + jni::Object operator()(const uint64_t value) const { + static auto constructor = JsonPrimitive::javaClass.GetConstructor>(env); + auto boxedValue = java::lang::Long::valueOf(env, value); + auto number = jni::Cast(env, boxedValue, java::lang::Number::javaClass); + auto jsonPrimitive = JsonPrimitive::javaClass.New(env, constructor, number); + jni::DeleteLocalRef(env, boxedValue); + return jsonPrimitive; + } + + /** + * Create a primitive containing a boolean value + */ + jni::Object operator()(const bool value) const { + static auto constructor = JsonPrimitive::javaClass.GetConstructor>(env); + auto boxedValue = java::lang::Boolean::valueOf(env, value); + auto jsonPrimitive = JsonPrimitive::javaClass.New(env, constructor, boxedValue); + jni::DeleteLocalRef(env, boxedValue); + return jsonPrimitive; + } +}; + +jni::Object JsonPrimitive::New(jni::JNIEnv &env, const value& value) { + JsonPrimitiveEvaluator evaluator { env }; + return value::visit(value, evaluator); +} + JsonPrimitive::value JsonPrimitive::convert(jni::JNIEnv &env, jni::Object jsonPrimitive) { value value; if (jsonPrimitive) { diff --git a/platform/android/src/gson/json_primitive.hpp b/platform/android/src/gson/json_primitive.hpp index 5fc8a2b485b..c418e0ebe89 100644 --- a/platform/android/src/gson/json_primitive.hpp +++ b/platform/android/src/gson/json_primitive.hpp @@ -15,6 +15,8 @@ class JsonPrimitive : private mbgl::util::noncopyable { static constexpr auto Name() { return "com/google/gson/JsonPrimitive"; }; + static jni::Object New(jni::JNIEnv&, const value&); + static value convert(JNIEnv&, jni::Object); static bool isBoolean(JNIEnv&, jni::Object); diff --git a/platform/android/src/java/lang.cpp b/platform/android/src/java/lang.cpp new file mode 100644 index 00000000000..3c95737169e --- /dev/null +++ b/platform/android/src/java/lang.cpp @@ -0,0 +1,76 @@ +#include "lang.hpp" + +namespace mbgl { +namespace android { +namespace java { +namespace lang { + +// Float + +jni::Object Float::valueOf(JNIEnv &env, jfloat value) { + static auto method = javaClass.GetStaticMethod (jni::jfloat)>(env, "valueOf"); + return javaClass.Call(env, method, value); +} + +void Float::registerNative(jni::JNIEnv &env) { + // Lookup the class + javaClass = *jni::Class::Find(env).NewGlobalRef(env).release(); +} + +jni::Class Float::javaClass; + +// Long + +jni::Object Long::valueOf(JNIEnv &env, jlong value) { + static auto method = javaClass.GetStaticMethod (jni::jlong)>(env, "valueOf"); + return javaClass.Call(env, method, value); +} + +void Long::registerNative(jni::JNIEnv &env) { + // Lookup the class + javaClass = *jni::Class::Find(env).NewGlobalRef(env).release(); +} + +jni::Class Long::javaClass; + +// Double + +jni::Object Double::valueOf(JNIEnv &env, jdouble value) { + static auto method = javaClass.GetStaticMethod (jni::jdouble)>(env, "valueOf"); + return javaClass.Call(env, method, value); +} + +void Double::registerNative(jni::JNIEnv &env) { + // Lookup the class + javaClass = *jni::Class::Find(env).NewGlobalRef(env).release(); +} + +jni::Class Double::javaClass; + +// Boolean + +jni::Object Boolean::valueOf(JNIEnv &env, jboolean value) { + static auto method = javaClass.GetStaticMethod (jni::jboolean)>(env, "valueOf"); + return javaClass.Call(env, method, value); +} + +void Boolean::registerNative(jni::JNIEnv &env) { + // Lookup the class + javaClass = *jni::Class::Find(env).NewGlobalRef(env).release(); +} + +jni::Class Boolean::javaClass; + +// Number + +void Number::registerNative(jni::JNIEnv &env) { + // Lookup the class + javaClass = *jni::Class::Find(env).NewGlobalRef(env).release(); +} + +jni::Class Number::javaClass; + +} // namespace lang +} // namespace java +} // namespace android +} // namespace mbgl diff --git a/platform/android/src/java/lang.hpp b/platform/android/src/java/lang.hpp index dcf81a9d0c5..981e3b14b74 100644 --- a/platform/android/src/java/lang.hpp +++ b/platform/android/src/java/lang.hpp @@ -1,18 +1,64 @@ #pragma once +#include +#include + namespace mbgl { namespace android { namespace java { namespace lang { -class Float { +class Float : private mbgl::util::noncopyable { public: static constexpr auto Name() { return "java/lang/Float"; }; + + static jni::Object valueOf(JNIEnv&, jfloat); + + static jni::Class javaClass; + + static void registerNative(jni::JNIEnv&); +}; + +class Double : private mbgl::util::noncopyable { +public: + static constexpr auto Name() { return "java/lang/Double"; }; + + static jni::Object valueOf(JNIEnv&, jdouble); + + static jni::Class javaClass; + + static void registerNative(jni::JNIEnv&); +}; + +class Long : private mbgl::util::noncopyable { +public: + static constexpr auto Name() { return "java/lang/Long"; }; + + static jni::Object valueOf(JNIEnv&, jlong); + + static jni::Class javaClass; + + static void registerNative(jni::JNIEnv&); +}; + +class Boolean : private mbgl::util::noncopyable { +public: + static constexpr auto Name() { return "java/lang/Boolean"; }; + + static jni::Object valueOf(JNIEnv&, jboolean); + + static jni::Class javaClass; + + static void registerNative(jni::JNIEnv&); }; -class Number { +class Number : private mbgl::util::noncopyable { public: static constexpr auto Name() { return "java/lang/Number"; }; + + static jni::Class javaClass; + + static void registerNative(jni::JNIEnv&); }; } // namespace lang diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp index c8c20e939a4..2f6ed96ab0e 100755 --- a/platform/android/src/jni.cpp +++ b/platform/android/src/jni.cpp @@ -47,6 +47,7 @@ #include "snapshotter/map_snapshotter.hpp" #include "snapshotter/map_snapshot.hpp" #include "text/local_glyph_rasterizer_jni.hpp" +#include "java/lang.hpp" namespace mbgl { namespace android { @@ -112,6 +113,11 @@ void registerNatives(JavaVM *vm) { java::util::registerNative(env); PointF::registerNative(env); RectF::registerNative(env); + java::lang::Number::registerNative(env); + java::lang::Float::registerNative(env); + java::lang::Boolean::registerNative(env); + java::lang::Double::registerNative(env); + java::lang::Long::registerNative(env); // GeoJSON geojson::Feature::registerNative(env); diff --git a/platform/android/src/style/conversion/function.hpp b/platform/android/src/style/conversion/function.hpp index 510efd3c942..d6669b45087 100644 --- a/platform/android/src/style/conversion/function.hpp +++ b/platform/android/src/style/conversion/function.hpp @@ -7,7 +7,8 @@ #include "../../java/lang.hpp" #include -#include "gson.hpp" +#include "../../gson/json_element.hpp" + #include #include @@ -16,41 +17,32 @@ namespace android { namespace conversion { template -struct Converter> { +struct Converter, mbgl::style::CameraFunction> { - Result operator()(jni::JNIEnv& env, const mbgl::style::CameraFunction& value) const { + Result> operator()(jni::JNIEnv& env, const mbgl::style::CameraFunction& value) const { // Convert expressions mbgl::Value expressionValue = value.getExpression().serialize(); - JsonEvaluator jsonEvaluator{env}; - jni::jobject* converted = apply_visitor(jsonEvaluator, expressionValue); - - return converted; + return gson::JsonElement::New(env, expressionValue); } }; template -struct Converter> { +struct Converter, mbgl::style::SourceFunction> { - Result operator()(jni::JNIEnv& env, const mbgl::style::SourceFunction& value) const { + Result> operator()(jni::JNIEnv& env, const mbgl::style::SourceFunction& value) const { // Convert expressions mbgl::Value expressionValue = value.getExpression().serialize(); - JsonEvaluator jsonEvaluator{env}; - jni::jobject* converted = apply_visitor(jsonEvaluator, expressionValue); - - return converted; + return gson::JsonElement::New(env, expressionValue); } }; template -struct Converter> { +struct Converter, mbgl::style::CompositeFunction> { - Result operator()(jni::JNIEnv& env, const mbgl::style::CompositeFunction& value) const { + Result> operator()(jni::JNIEnv& env, const mbgl::style::CompositeFunction& value) const { // Convert expressions mbgl::Value expressionValue = value.getExpression().serialize(); - JsonEvaluator jsonEvaluator{env}; - jni::jobject* converted = apply_visitor(jsonEvaluator, expressionValue); - - return converted; + return gson::JsonElement::New(env, expressionValue); } }; diff --git a/platform/android/src/style/conversion/gson.hpp b/platform/android/src/style/conversion/gson.hpp deleted file mode 100644 index 02c3fdd30dd..00000000000 --- a/platform/android/src/style/conversion/gson.hpp +++ /dev/null @@ -1,114 +0,0 @@ -#pragma once - -#include "../../conversion/constant.hpp" -#include "../../conversion/collection.hpp" - -#include -#include -#include "../../jni/local_object.hpp" -#include "mapbox/geometry/feature.hpp" - -namespace mbgl { -namespace android { -namespace conversion { - -/** - * Turn mapbox::geometry::value type into Java Json - */ -class JsonEvaluator { -public: - - jni::JNIEnv& env; - - jni::jobject* operator()(const mapbox::geometry::null_value_t) const { - return (jni::jobject*) nullptr;; - } - - jni::jobject* operator()(const bool& value) const { - static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonPrimitive")).release(); - static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "(Ljava/lang/Boolean;)V"); - - // Create JsonPrimitive - jni::LocalObject converted = jni::NewLocalObject(env, *convert(env, value)); - jni::jobject* object = &jni::NewObject(env, *javaClass, *constructor, *converted); - - return object; - } - - template - jni::jobject* operator()(const Number& value) const { - static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonPrimitive")).release(); - static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "(Ljava/lang/Number;)V"); - - // Create JsonPrimitive - jni::LocalObject converted = jni::NewLocalObject(env, *convert(env, value)); - jni::jobject* object = &jni::NewObject(env, *javaClass, *constructor, converted.get()); - - return object; - } - - jni::jobject* operator()(const std::string value) const { - static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonPrimitive")).release(); - static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "(Ljava/lang/String;)V"); - - // Create JsonPrimitive - jni::LocalObject converted = jni::NewLocalObject(env, *convert(env, value)); - jni::jobject* object = &jni::NewObject(env, *javaClass, *constructor, converted.get()); - - return object; - } - - jni::jobject* operator()(const std::vector values) const { - static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonArray")).release(); - static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "()V");; - static jni::jmethodID* add = &jni::GetMethodID(env, *javaClass, "add", "(Lcom/google/gson/JsonElement;)V"); - - // Create json array - jni::jobject* jarray = &jni::NewObject(env, *javaClass, *constructor); - - // Add values - for (const auto &v : values) { - jni::LocalObject converted = jni::NewLocalObject(env, mapbox::geometry::value::visit(v, *this)); - jni::CallMethod(env, jarray, *add, converted.get()); - } - - return jarray; - } - - jni::jobject* operator()(const std::unordered_map value) const { - static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonObject")).release(); - static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "()V");; - static jni::jmethodID* add = &jni::GetMethodID(env, *javaClass, "add", "(Ljava/lang/String;Lcom/google/gson/JsonElement;)V"); - - // Create json object - jni::jobject* jsonObject = &jni::NewObject(env, *javaClass, *constructor); - - // Add items - for (auto &item : value) { - jni::LocalObject converted = jni::NewLocalObject(env, mbgl::Value::visit(item.second, *this)); - jni::LocalObject key = jni::NewLocalObject(env, *convert(env, item.first)); - jni::CallMethod(env, jsonObject, *add, key.get(), converted.get()); - } - - return jsonObject; - } - -private: - -}; - -/** - * mapbox::geometry::value -> Java Json - */ -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mapbox::geometry::value& value) const { - JsonEvaluator evaluator { env } ; - jni::jobject* converted = mapbox::geometry::value::visit(value, evaluator); - return { converted }; - } -}; - -} -} -} diff --git a/platform/android/src/style/conversion/property_value.hpp b/platform/android/src/style/conversion/property_value.hpp index 4ed25eac252..902d1e80b27 100644 --- a/platform/android/src/style/conversion/property_value.hpp +++ b/platform/android/src/style/conversion/property_value.hpp @@ -7,7 +7,6 @@ #include "../../conversion/constant.hpp" #include "types.hpp" #include "function.hpp" -#include "gson.hpp" namespace mbgl { namespace android { @@ -32,15 +31,15 @@ class PropertyValueEvaluator { } jni::jobject* operator()(const mbgl::style::CameraFunction &value) const { - return *convert>(env, value); + return *convert, mbgl::style::CameraFunction>(env, value); } jni::jobject* operator()(const mbgl::style::SourceFunction &value) const { - return *convert>(env, value); + return *convert, mbgl::style::SourceFunction>(env, value); } jni::jobject* operator()(const mbgl::style::CompositeFunction &value) const { - return *convert>(env, value); + return *convert, mbgl::style::CompositeFunction>(env, value); } private: diff --git a/platform/android/src/style/layers/layer.cpp b/platform/android/src/style/layers/layer.cpp index a2f4087fce5..6fe6e3cb29e 100644 --- a/platform/android/src/style/layers/layer.cpp +++ b/platform/android/src/style/layers/layer.cpp @@ -26,7 +26,6 @@ // C++ -> Java conversion #include "../conversion/property_value.hpp" #include -#include "../conversion/gson.hpp" #include @@ -153,20 +152,19 @@ namespace android { } }; - jni::Object Layer::getFilter(jni::JNIEnv& env) { + jni::Object Layer::getFilter(jni::JNIEnv& env) { using namespace mbgl::style; using namespace mbgl::style::conversion; Filter filter = layer.accept(GetFilterEvaluator()); - jni::jobject* converted = nullptr; + jni::Object converted; if (filter.is()) { ExpressionFilter filterExpression = filter.get(); mbgl::Value expressionValue = filterExpression.expression.get()->serialize(); - conversion::JsonEvaluator jsonEvaluator{env}; - converted = apply_visitor(jsonEvaluator, expressionValue); + converted = gson::JsonElement::New(env, expressionValue); } - return jni::Object(converted); + return converted; } struct SetSourceLayerEvaluator { diff --git a/platform/android/src/style/layers/layer.hpp b/platform/android/src/style/layers/layer.hpp index 19713a1baa6..2486b0dfa6c 100644 --- a/platform/android/src/style/layers/layer.hpp +++ b/platform/android/src/style/layers/layer.hpp @@ -5,6 +5,7 @@ #include #include "../../gson/json_array.hpp" #include "../value.hpp" +#include "../../gson/json_element.hpp" #include @@ -68,7 +69,7 @@ class Layer : private mbgl::util::noncopyable { void setFilter(jni::JNIEnv&, jni::Array>); - jni::Object getFilter(jni::JNIEnv&); + jni::Object getFilter(jni::JNIEnv&); void setSourceLayer(jni::JNIEnv&, jni::String);