-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Rework expression conversion #11490
Rework expression conversion #11490
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<JsonElement> operator()(const JsonPrimitive::value value) const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const ref? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not seeing where I can add an additional |
||
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; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,23 @@ namespace android { | |
namespace gson { | ||
|
||
|
||
jni::Object<JsonObject> JsonObject::New(jni::JNIEnv& env, const std::unordered_map<std::string, mapbox::geometry::value>& values) { | ||
static auto constructor = JsonObject::javaClass.GetConstructor(env); | ||
static auto addMethod = JsonObject::javaClass.GetMethod<void (jni::String, jni::Object<JsonElement>)>(env, "add"); | ||
|
||
jni::Object<JsonObject> jsonObject = JsonObject::javaClass.New(env, constructor); | ||
|
||
for (auto &item : values) { | ||
jni::Object<JsonElement> jsonElement = JsonElement::New(env, item.second); | ||
jni::String key = jni::Make<jni::String>(env, item.first); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete local references (jsonElement and key) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const ref |
||
jsonObject.Call(env, addMethod, key, jsonElement); | ||
jni::DeleteLocalRef(env, jsonElement); | ||
jni::DeleteLocalRef(env, key); | ||
} | ||
|
||
return jsonObject; | ||
} | ||
|
||
template <typename F> // void (jni::String, jni::Object<gson::JsonElement>) | ||
static void iterateEntrySet(jni::JNIEnv& env, jni::Object<JsonObject> jsonObject, F callback) { | ||
// Get Set<Map.Entry<String, JsonElement>> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete local references |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete local reference |
||
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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a loop like this, it's important to clean up the temporary local references (
jni:DeleteLocalRef(jsonElement)
)