diff options
| author | Tobrun <tobrun@mapbox.com> | 2018-04-10 11:09:42 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-04-10 11:09:42 +0200 |
| commit | 44ce5ab08792df5f38560016ed3d41712e8025d0 (patch) | |
| tree | 83ff99ff7b811e4f54165058df01eb101750c33e /platform/android/src/gson/json_primitive.cpp | |
| parent | 351ac83c75955c7e44e49a42156ece83b759612b (diff) | |
| download | qtlocation-mapboxgl-44ce5ab08792df5f38560016ed3d41712e8025d0.tar.gz | |
Rework expression conversion (#11490)
* [android] - rework expression conversion
Diffstat (limited to 'platform/android/src/gson/json_primitive.cpp')
| -rw-r--r-- | platform/android/src/gson/json_primitive.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/platform/android/src/gson/json_primitive.cpp b/platform/android/src/gson/json_primitive.cpp index 58d0b45fe7..4e171c4845 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<JsonPrimitive> operator()(const mapbox::geometry::null_value_t) const { + return jni::Object<JsonPrimitive>(); + } + + /** + * Create a primitive containing a string value + */ + jni::Object<JsonPrimitive> operator()(const std::string value) const { + static auto constructor = JsonPrimitive::javaClass.GetConstructor<jni::String>(env); + auto jvalue = jni::Make<jni::String>(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<JsonPrimitive> operator()(const double value) const { + static auto constructor = JsonPrimitive::javaClass.GetConstructor<jni::Object<java::lang::Number>>(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<JsonPrimitive> operator()(const int64_t value) const { + static auto constructor = JsonPrimitive::javaClass.GetConstructor<jni::Object<java::lang::Number>>(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<JsonPrimitive> operator()(const uint64_t value) const { + static auto constructor = JsonPrimitive::javaClass.GetConstructor<jni::Object<java::lang::Number>>(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<JsonPrimitive> operator()(const bool value) const { + static auto constructor = JsonPrimitive::javaClass.GetConstructor<jni::Object<java::lang::Boolean>>(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> 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> jsonPrimitive) { value value; if (jsonPrimitive) { |
