[android] Simplify JSON conversion
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
#include "json_array.hpp"
|
||||
|
||||
#include "json_element.hpp"
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace gson {
|
||||
|
||||
jni::Object<JsonArray> JsonArray::New(jni::JNIEnv& env, const std::vector<mapbox::geometry::value>& values){
|
||||
jni::Object<JsonArray> JsonArray::New(jni::JNIEnv& env, const std::vector<mbgl::Value>& values){
|
||||
static auto javaClass = jni::Class<JsonArray>::Singleton(env);
|
||||
static auto constructor = javaClass.GetConstructor(env);
|
||||
static auto addMethod = javaClass.GetMethod<void (jni::Object<JsonElement>)>(env, "add");
|
||||
@@ -20,8 +19,8 @@ jni::Object<JsonArray> JsonArray::New(jni::JNIEnv& env, const std::vector<mapbox
|
||||
return jsonArray;
|
||||
}
|
||||
|
||||
std::vector<mapbox::geometry::value> JsonArray::convert(jni::JNIEnv& env, const jni::Object<JsonArray> jsonArray) {
|
||||
std::vector<mapbox::geometry::value> values;
|
||||
std::vector<mbgl::Value> JsonArray::convert(jni::JNIEnv& env, const jni::Object<JsonArray> jsonArray) {
|
||||
std::vector<mbgl::Value> values;
|
||||
|
||||
if (jsonArray) {
|
||||
static auto javaClass = jni::Class<JsonArray>::Singleton(env);
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
#include "json_element.hpp"
|
||||
|
||||
#include <mapbox/geometry.hpp>
|
||||
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
@@ -14,8 +12,8 @@ class JsonArray : public JsonElement {
|
||||
public:
|
||||
static constexpr auto Name() { return "com/google/gson/JsonArray"; };
|
||||
|
||||
static jni::Object<JsonArray> New(jni::JNIEnv&, const std::vector<mapbox::geometry::value>&);
|
||||
static std::vector<mapbox::geometry::value> convert(JNIEnv&, jni::Object<JsonArray>);
|
||||
static jni::Object<JsonArray> New(jni::JNIEnv&, const std::vector<mbgl::Value>&);
|
||||
static std::vector<mbgl::Value> convert(JNIEnv&, jni::Object<JsonArray>);
|
||||
|
||||
static void registerNative(jni::JNIEnv&);
|
||||
};
|
||||
|
||||
@@ -1,87 +1,87 @@
|
||||
#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::Object<JsonElement> JsonElement::New(jni::JNIEnv& env, const mbgl::Value& value) {
|
||||
static auto primitive = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto stringConstructor = primitive.GetConstructor<jni::String>(env);
|
||||
static auto numberConstructor = primitive.GetConstructor<jni::Number>(env);
|
||||
static auto booleanConstructor = primitive.GetConstructor<jni::Boolean>(env);
|
||||
|
||||
jni::JNIEnv& env;
|
||||
|
||||
jni::Object<JsonElement> operator()(const JsonPrimitive::value value) const {
|
||||
return JsonPrimitive::New(env, value);
|
||||
}
|
||||
|
||||
jni::Object<JsonElement> operator()(const std::vector<mapbox::geometry::value> &values) const {
|
||||
return JsonArray::New(env, values);
|
||||
}
|
||||
|
||||
jni::Object<JsonElement> operator()(const std::unordered_map<std::string, mapbox::geometry::value> &values) const {
|
||||
return JsonObject::New(env, values);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
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)) {
|
||||
static auto primitiveClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
auto primitive = JsonPrimitive::convert(env, jni::Cast(env, primitiveClass, jsonElement));
|
||||
value = mapbox::util::apply_visitor([](auto t) { return mapbox::geometry::value { t }; }, primitive);
|
||||
} else if (isJsonObject(env, jsonElement)) {
|
||||
static auto objectClass = jni::Class<JsonObject>::Singleton(env);
|
||||
mapbox::geometry::property_map map = JsonObject::convert(env, jni::Cast(env, objectClass, jsonElement));
|
||||
value = mapbox::util::recursive_wrapper<std::unordered_map<std::string, mapbox::geometry::value>> { map } ;
|
||||
} else if (isJsonArray(env, jsonElement)) {
|
||||
static auto arrayClass = jni::Class<JsonArray>::Singleton(env);
|
||||
value = JsonArray::convert(env, jni::Cast(env, arrayClass, jsonElement));
|
||||
} else {
|
||||
value = mapbox::geometry::null_value;
|
||||
return value.match(
|
||||
[&] (const mbgl::NullValue&) {
|
||||
return jni::Object<JsonElement>();
|
||||
},
|
||||
[&] (const std::string& value) {
|
||||
return primitive.New(env, stringConstructor,
|
||||
*jni::SeizeLocal(env, jni::Make<jni::String>(env, value)));
|
||||
},
|
||||
[&] (const double value) {
|
||||
return primitive.New(env, numberConstructor,
|
||||
*jni::SeizeLocal(env, jni::Number(jni::Box(env, value))));
|
||||
},
|
||||
[&] (const int64_t value) {
|
||||
return primitive.New(env, numberConstructor,
|
||||
*jni::SeizeLocal(env, jni::Number(jni::Box(env, value))));
|
||||
},
|
||||
[&] (const uint64_t value) {
|
||||
return primitive.New(env, numberConstructor,
|
||||
*jni::SeizeLocal(env, jni::Number(jni::Box(env, int64_t(value))))); // TODO: should use BigInteger
|
||||
},
|
||||
[&] (const bool value) {
|
||||
return primitive.New(env, booleanConstructor,
|
||||
*jni::SeizeLocal(env, jni::Box(env, value ? jni::jni_true : jni::jni_false)));
|
||||
},
|
||||
[&] (const std::vector<mbgl::Value>& values) {
|
||||
return JsonArray::New(env, values);
|
||||
},
|
||||
[&] (const mbgl::PropertyMap& values) {
|
||||
return JsonObject::New(env, values);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
mbgl::Value JsonElement::convert(jni::JNIEnv &env, jni::Object<JsonElement> jsonElement) {
|
||||
if (!jsonElement) {
|
||||
return mbgl::NullValue();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
bool JsonElement::isJsonObject(JNIEnv& env, jni::Object<JsonElement> jsonElement) {
|
||||
static auto javaClass = jni::Class<JsonElement>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::jboolean ()>(env, "isJsonObject");
|
||||
return jsonElement.Call(env, method);
|
||||
}
|
||||
static auto elementClass = jni::Class<JsonElement>::Singleton(env);
|
||||
static auto isJsonObject = elementClass.GetMethod<jni::jboolean ()>(env, "isJsonObject");
|
||||
static auto isJsonArray = elementClass.GetMethod<jni::jboolean ()>(env, "isJsonArray");
|
||||
static auto isJsonPrimitive = elementClass.GetMethod<jni::jboolean ()>(env, "isJsonPrimitive");
|
||||
|
||||
bool JsonElement::isJsonArray(JNIEnv& env, jni::Object<JsonElement> jsonElement) {
|
||||
static auto javaClass = jni::Class<JsonElement>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::jboolean ()>(env, "isJsonArray");
|
||||
return jsonElement.Call(env, method);
|
||||
}
|
||||
static auto primitiveClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto isBoolean = primitiveClass.GetMethod<jni::jboolean ()>(env, "isBoolean");
|
||||
static auto isString = primitiveClass.GetMethod<jni::jboolean ()>(env, "isString");
|
||||
static auto isNumber = primitiveClass.GetMethod<jni::jboolean ()>(env, "isNumber");
|
||||
static auto getAsBoolean = primitiveClass.GetMethod<jni::jboolean ()>(env, "getAsBoolean");
|
||||
static auto getAsString = primitiveClass.GetMethod<jni::String ()>(env, "getAsString");
|
||||
static auto getAsDouble = primitiveClass.GetMethod<jni::jdouble ()>(env, "getAsDouble");
|
||||
|
||||
bool JsonElement::isJsonPrimitive(JNIEnv& env, jni::Object<JsonElement> jsonElement) {
|
||||
static auto javaClass = jni::Class<JsonElement>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::jboolean ()>(env, "isJsonPrimitive");
|
||||
return jsonElement.Call(env, method);
|
||||
}
|
||||
|
||||
bool JsonElement::isJsonNull(JNIEnv& env, jni::Object<JsonElement> jsonElement) {
|
||||
static auto javaClass = jni::Class<JsonElement>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::jboolean ()>(env, "isJsonNull");
|
||||
return jsonElement.Call(env, method);
|
||||
if (jsonElement.Call(env, isJsonPrimitive)) {
|
||||
auto primitive = jni::Cast(env, primitiveClass, jsonElement);
|
||||
if (primitive.Call(env, isBoolean)) {
|
||||
return bool(primitive.Call(env, getAsBoolean));
|
||||
} else if (primitive.Call(env, isNumber)) {
|
||||
return primitive.Call(env, getAsDouble); // TODO: how to differentiate types here?
|
||||
} else if (primitive.Call(env, isString)) {
|
||||
return jni::Make<std::string>(env, *jni::SeizeLocal(env, primitive.Call(env, getAsString)));
|
||||
} else {
|
||||
return mbgl::NullValue();
|
||||
}
|
||||
} else if (jsonElement.Call(env, isJsonObject)) {
|
||||
return JsonObject::convert(env, jni::Cast(env, jni::Class<JsonObject>::Singleton(env), jsonElement));
|
||||
} else if (jsonElement.Call(env, isJsonArray)) {
|
||||
return JsonArray::convert(env, jni::Cast(env, jni::Class<JsonArray>::Singleton(env), jsonElement));
|
||||
} else {
|
||||
return mbgl::NullValue();
|
||||
}
|
||||
}
|
||||
|
||||
void JsonElement::registerNative(jni::JNIEnv &env) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <mapbox/geometry.hpp>
|
||||
#include <mbgl/util/feature.hpp>
|
||||
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
@@ -12,13 +12,8 @@ class JsonElement : public jni::ObjectTag {
|
||||
public:
|
||||
static constexpr auto Name() { return "com/google/gson/JsonElement"; };
|
||||
|
||||
static jni::Object<JsonElement> New(jni::JNIEnv&, const mapbox::geometry::value&);
|
||||
static mapbox::geometry::value convert(JNIEnv&, jni::Object<JsonElement>);
|
||||
|
||||
static bool isJsonObject(JNIEnv&, jni::Object<JsonElement>);
|
||||
static bool isJsonArray(JNIEnv&, jni::Object<JsonElement>);
|
||||
static bool isJsonPrimitive(JNIEnv&, jni::Object<JsonElement>);
|
||||
static bool isJsonNull(JNIEnv&, jni::Object<JsonElement>);
|
||||
static jni::Object<JsonElement> New(jni::JNIEnv&, const mbgl::Value&);
|
||||
static mbgl::Value convert(JNIEnv&, jni::Object<JsonElement>);
|
||||
|
||||
static void registerNative(jni::JNIEnv&);
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace android {
|
||||
namespace gson {
|
||||
|
||||
|
||||
jni::Object<JsonObject> JsonObject::New(jni::JNIEnv& env, const std::unordered_map<std::string, mapbox::geometry::value>& values) {
|
||||
jni::Object<JsonObject> JsonObject::New(jni::JNIEnv& env, const mbgl::PropertyMap& values) {
|
||||
static auto javaClass = jni::Class<JsonObject>::Singleton(env);
|
||||
static auto constructor = javaClass.GetConstructor(env);
|
||||
static auto addMethod = javaClass.GetMethod<void (jni::String, jni::Object<JsonElement>)>(env, "add");
|
||||
@@ -46,8 +46,8 @@ static void iterateEntrySet(jni::JNIEnv& env, jni::Object<JsonObject> jsonObject
|
||||
}
|
||||
}
|
||||
|
||||
mapbox::geometry::property_map JsonObject::convert(jni::JNIEnv &env, jni::Object<JsonObject> jsonObject) {
|
||||
mapbox::geometry::property_map map;
|
||||
mbgl::PropertyMap JsonObject::convert(jni::JNIEnv &env, jni::Object<JsonObject> jsonObject) {
|
||||
mbgl::PropertyMap map;
|
||||
|
||||
if (jsonObject) {
|
||||
iterateEntrySet(env, jsonObject, [&map, &env](jni::String jId, jni::Object<gson::JsonElement> jsonElement) {
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
#include "json_element.hpp"
|
||||
|
||||
#include <mapbox/geometry.hpp>
|
||||
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
@@ -14,8 +12,8 @@ class JsonObject : public JsonElement {
|
||||
public:
|
||||
static constexpr auto Name() { return "com/google/gson/JsonObject"; };
|
||||
|
||||
static jni::Object<JsonObject> New(jni::JNIEnv&, const std::unordered_map<std::string, mapbox::geometry::value>&);
|
||||
static mapbox::geometry::property_map convert(JNIEnv&, jni::Object<JsonObject>);
|
||||
static jni::Object<JsonObject> New(jni::JNIEnv&, const mbgl::PropertyMap&);
|
||||
static mbgl::PropertyMap convert(JNIEnv&, jni::Object<JsonObject>);
|
||||
|
||||
static void registerNative(jni::JNIEnv&);
|
||||
};
|
||||
|
||||
@@ -4,135 +4,6 @@ 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 javaClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto constructor = javaClass.GetConstructor<jni::String>(env);
|
||||
|
||||
return javaClass.New(env, constructor,
|
||||
*jni::SeizeLocal(env, jni::Make<jni::String>(env, value)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a primitive containing a number value with type double
|
||||
*/
|
||||
jni::Object<JsonPrimitive> operator()(const double value) const {
|
||||
static auto javaClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto constructor = javaClass.GetConstructor<jni::Number>(env);
|
||||
|
||||
return javaClass.New(env, constructor,
|
||||
*jni::SeizeLocal(env, jni::Number(jni::Box(env, value))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a primitive containing a number value with type long
|
||||
*/
|
||||
jni::Object<JsonPrimitive> operator()(const int64_t value) const {
|
||||
static auto javaClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto constructor = javaClass.GetConstructor<jni::Number>(env);
|
||||
|
||||
return javaClass.New(env, constructor,
|
||||
*jni::SeizeLocal(env, jni::Number(jni::Box(env, value))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a primitive containing a number value with type long
|
||||
*/
|
||||
jni::Object<JsonPrimitive> operator()(const uint64_t value) const {
|
||||
static auto javaClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto constructor = javaClass.GetConstructor<jni::Number>(env);
|
||||
|
||||
return javaClass.New(env, constructor,
|
||||
*jni::SeizeLocal(env, jni::Number(jni::Box(env, int64_t(value))))); // TODO: should use BigInteger
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a primitive containing a boolean value
|
||||
*/
|
||||
jni::Object<JsonPrimitive> operator()(const bool value) const {
|
||||
static auto javaClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto constructor = javaClass.GetConstructor<jni::Boolean>(env);
|
||||
|
||||
return javaClass.New(env, constructor,
|
||||
*jni::SeizeLocal(env, jni::Box(env, value ? jni::jni_true : jni::jni_false)));
|
||||
}
|
||||
};
|
||||
|
||||
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) {
|
||||
if (isBoolean(env, jsonPrimitive)) {
|
||||
value = getAsBoolean(env, jsonPrimitive);
|
||||
} else if (isNumber(env, jsonPrimitive)) {
|
||||
//TODO: how to differentiate types here?
|
||||
value = getAsDouble(env, jsonPrimitive);
|
||||
} else if (isString(env, jsonPrimitive)) {
|
||||
value = getAsString(env, jsonPrimitive);
|
||||
} else {
|
||||
value = mapbox::geometry::null_value;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
bool JsonPrimitive::isBoolean(JNIEnv& env, jni::Object<JsonPrimitive> jsonPrimitive) {
|
||||
static auto javaClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::jboolean ()>(env, "isBoolean");
|
||||
return jsonPrimitive.Call(env, method);
|
||||
}
|
||||
|
||||
bool JsonPrimitive::isString(JNIEnv& env, jni::Object<JsonPrimitive> jsonPrimitive) {
|
||||
static auto javaClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::jboolean ()>(env, "isString");
|
||||
return jsonPrimitive.Call(env, method);
|
||||
}
|
||||
|
||||
bool JsonPrimitive::isNumber(JNIEnv& env, jni::Object<JsonPrimitive> jsonPrimitive) {
|
||||
static auto javaClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::jboolean ()>(env, "isNumber");
|
||||
return jsonPrimitive.Call(env, method);
|
||||
}
|
||||
|
||||
bool JsonPrimitive::getAsBoolean(JNIEnv& env, jni::Object<JsonPrimitive> jsonPrimitive) {
|
||||
static auto javaClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::jboolean ()>(env, "getAsBoolean");
|
||||
return jsonPrimitive.Call(env, method);
|
||||
}
|
||||
|
||||
std::string JsonPrimitive::getAsString(JNIEnv& env, jni::Object<JsonPrimitive> jsonPrimitive) {
|
||||
static auto javaClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::String ()>(env, "getAsString");
|
||||
return jni::Make<std::string>(env, *jni::SeizeLocal(env, jsonPrimitive.Call(env, method)));
|
||||
}
|
||||
|
||||
double JsonPrimitive::getAsDouble(JNIEnv& env, jni::Object<JsonPrimitive> jsonPrimitive) {
|
||||
static auto javaClass = jni::Class<JsonPrimitive>::Singleton(env);
|
||||
static auto method = javaClass.GetMethod<jni::jdouble ()>(env, "getAsDouble");
|
||||
return jsonPrimitive.Call(env, method);
|
||||
}
|
||||
|
||||
void JsonPrimitive::registerNative(jni::JNIEnv &env) {
|
||||
jni::Class<JsonPrimitive>::Singleton(env);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
#include "json_element.hpp"
|
||||
|
||||
#include <mapbox/geometry.hpp>
|
||||
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
@@ -12,21 +10,8 @@ namespace gson {
|
||||
|
||||
class JsonPrimitive : public JsonElement {
|
||||
public:
|
||||
using value = mapbox::util::variant<mapbox::geometry::null_value_t, bool, uint64_t, int64_t, double, std::string>;
|
||||
|
||||
static constexpr auto Name() { return "com/google/gson/JsonPrimitive"; };
|
||||
|
||||
static jni::Object<JsonPrimitive> New(jni::JNIEnv&, const value&);
|
||||
static value convert(JNIEnv&, jni::Object<JsonPrimitive>);
|
||||
|
||||
static bool isBoolean(JNIEnv&, jni::Object<JsonPrimitive>);
|
||||
static bool isString(JNIEnv&, jni::Object<JsonPrimitive>);
|
||||
static bool isNumber(JNIEnv&, jni::Object<JsonPrimitive>);
|
||||
|
||||
static bool getAsBoolean(JNIEnv&, jni::Object<JsonPrimitive>);
|
||||
static std::string getAsString(JNIEnv&, jni::Object<JsonPrimitive>);
|
||||
static double getAsDouble(JNIEnv&, jni::Object<JsonPrimitive>);
|
||||
|
||||
static void registerNative(jni::JNIEnv&);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user