[core] Use convertJSON instead of RapidJSON directly
This commit is contained in:
@@ -1,20 +1,14 @@
|
||||
#include <benchmark/benchmark.h>
|
||||
|
||||
#include <mbgl/style/function/source_function.hpp>
|
||||
|
||||
#include <mbgl/style/rapidjson_conversion.hpp>
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/style/conversion/json.hpp>
|
||||
#include <mbgl/style/conversion/function.hpp>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
|
||||
using namespace mbgl;
|
||||
using namespace mbgl::style;
|
||||
|
||||
static rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> createFunctionJSON(size_t stopCount) {
|
||||
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> doc;
|
||||
|
||||
static std::string createFunctionJSON(size_t stopCount) {
|
||||
std::string stops = "[";
|
||||
for (size_t i = 0; i < stopCount; i++) {
|
||||
std::string value = std::to_string(24.0f / stopCount * i);
|
||||
@@ -22,9 +16,7 @@ static rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> cr
|
||||
stops += "[" + value + ", " + value + "]";
|
||||
}
|
||||
stops += "]";
|
||||
|
||||
doc.Parse<0>(R"({"type": "exponential", "base": 2, "stops": )" + stops + "}");
|
||||
return doc;
|
||||
return R"({"type": "exponential", "base": 2, "stops": )" + stops + "}";
|
||||
}
|
||||
|
||||
static void Parse_CameraFunction(benchmark::State& state) {
|
||||
@@ -35,7 +27,7 @@ static void Parse_CameraFunction(benchmark::State& state) {
|
||||
state.PauseTiming();
|
||||
auto doc = createFunctionJSON(stopCount);
|
||||
state.ResumeTiming();
|
||||
optional<CameraFunction<float>> result = conversion::convert<CameraFunction<float>, JSValue>(doc, error);
|
||||
optional<CameraFunction<float>> result = conversion::convertJSON<CameraFunction<float>>(doc, error);
|
||||
if (!result) {
|
||||
state.SkipWithError(error.message.c_str());
|
||||
}
|
||||
@@ -47,7 +39,7 @@ static void Evaluate_CameraFunction(benchmark::State& state) {
|
||||
size_t stopCount = state.range(0);
|
||||
auto doc = createFunctionJSON(stopCount);
|
||||
conversion::Error error;
|
||||
optional<CameraFunction<float>> function = conversion::convert<CameraFunction<float>, JSValue>(doc, error);
|
||||
optional<CameraFunction<float>> function = conversion::convertJSON<CameraFunction<float>>(doc, error);
|
||||
if (!function) {
|
||||
state.SkipWithError(error.message.c_str());
|
||||
}
|
||||
|
||||
@@ -5,19 +5,14 @@
|
||||
#include <mbgl/style/function/composite_exponential_stops.hpp>
|
||||
#include <mbgl/style/function/composite_function.hpp>
|
||||
|
||||
#include <mbgl/style/rapidjson_conversion.hpp>
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/style/conversion/json.hpp>
|
||||
#include <mbgl/style/conversion/function.hpp>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
|
||||
using namespace mbgl;
|
||||
using namespace mbgl::style;
|
||||
|
||||
static rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> createFunctionJSON(size_t stopCount) {
|
||||
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> doc;
|
||||
|
||||
static std::string createFunctionJSON(size_t stopCount) {
|
||||
std::string stops = "[";
|
||||
for (size_t outerStop = 0; outerStop < stopCount; outerStop++) {
|
||||
for (size_t innerStop = 0; innerStop < stopCount; innerStop++) {
|
||||
@@ -29,9 +24,7 @@ static rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> cr
|
||||
}
|
||||
}
|
||||
stops += "]";
|
||||
|
||||
doc.Parse<0>(R"({"type": "exponential", "base": 2, "stops": )" + stops + R"(, "property": "x"})");
|
||||
return doc;
|
||||
return R"({"type": "exponential", "base": 2, "stops": )" + stops + R"(, "property": "x"})";
|
||||
}
|
||||
|
||||
static void Parse_CompositeFunction(benchmark::State& state) {
|
||||
@@ -42,7 +35,7 @@ static void Parse_CompositeFunction(benchmark::State& state) {
|
||||
state.PauseTiming();
|
||||
auto doc = createFunctionJSON(stopCount);
|
||||
state.ResumeTiming();
|
||||
optional<CompositeFunction<float>> result = conversion::convert<style::CompositeFunction<float>, JSValue>(doc, error);
|
||||
optional<CompositeFunction<float>> result = conversion::convertJSON<style::CompositeFunction<float>>(doc, error);
|
||||
if (!result) {
|
||||
state.SkipWithError(error.message.c_str());
|
||||
}
|
||||
@@ -54,7 +47,7 @@ static void Evaluate_CompositeFunction(benchmark::State& state) {
|
||||
size_t stopCount = state.range(0);
|
||||
auto doc = createFunctionJSON(stopCount);
|
||||
conversion::Error error;
|
||||
optional<CompositeFunction<float>> function = conversion::convert<CompositeFunction<float>, JSValue>(doc, error);
|
||||
optional<CompositeFunction<float>> function = conversion::convertJSON<CompositeFunction<float>>(doc, error);
|
||||
if (!function) {
|
||||
state.SkipWithError(error.message.c_str());
|
||||
}
|
||||
|
||||
@@ -4,19 +4,14 @@
|
||||
|
||||
#include <mbgl/style/function/source_function.hpp>
|
||||
|
||||
#include <mbgl/style/rapidjson_conversion.hpp>
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/style/conversion/json.hpp>
|
||||
#include <mbgl/style/conversion/function.hpp>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
|
||||
using namespace mbgl;
|
||||
using namespace mbgl::style;
|
||||
|
||||
static rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> createFunctionJSON(size_t stopCount) {
|
||||
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> doc;
|
||||
|
||||
static std::string createFunctionJSON(size_t stopCount) {
|
||||
std::string stops = "[";
|
||||
for (size_t i = 0; i < stopCount; i++) {
|
||||
std::string value = std::to_string(100.0f / stopCount * i);
|
||||
@@ -24,9 +19,7 @@ static rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> cr
|
||||
stops += "[" + value + ", " + value + "]";
|
||||
}
|
||||
stops += "]";
|
||||
|
||||
doc.Parse<0>(R"({"type": "exponential", "base": 2, "stops": )" + stops + R"(, "property": "x"})");
|
||||
return doc;
|
||||
return R"({"type": "exponential", "base": 2, "stops": )" + stops + R"(, "property": "x"})";
|
||||
}
|
||||
|
||||
static void Parse_SourceFunction(benchmark::State& state) {
|
||||
@@ -37,7 +30,7 @@ static void Parse_SourceFunction(benchmark::State& state) {
|
||||
state.PauseTiming();
|
||||
auto doc = createFunctionJSON(stopCount);
|
||||
state.ResumeTiming();
|
||||
optional<SourceFunction<float>> result = conversion::convert<SourceFunction<float>, JSValue>(doc, error);
|
||||
optional<SourceFunction<float>> result = conversion::convertJSON<SourceFunction<float>>(doc, error);
|
||||
if (!result) {
|
||||
state.SkipWithError(error.message.c_str());
|
||||
}
|
||||
@@ -49,7 +42,7 @@ static void Evaluate_SourceFunction(benchmark::State& state) {
|
||||
size_t stopCount = state.range(0);
|
||||
auto doc = createFunctionJSON(stopCount);
|
||||
conversion::Error error;
|
||||
optional<SourceFunction<float>> function = conversion::convert<SourceFunction<float>, JSValue>(doc, error);
|
||||
optional<SourceFunction<float>> function = conversion::convertJSON<SourceFunction<float>>(doc, error);
|
||||
if (!function) {
|
||||
state.SkipWithError(error.message.c_str());
|
||||
}
|
||||
|
||||
@@ -2,20 +2,16 @@
|
||||
|
||||
#include <mbgl/style/filter.hpp>
|
||||
#include <mbgl/style/filter_evaluator.hpp>
|
||||
#include <mbgl/style/rapidjson_conversion.hpp>
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/style/conversion/json.hpp>
|
||||
#include <mbgl/style/conversion/filter.hpp>
|
||||
#include <mbgl/tile/geometry_tile_data.hpp>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
using namespace mbgl;
|
||||
|
||||
style::Filter parse(const char* expression) {
|
||||
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> doc;
|
||||
doc.Parse<0>(expression);
|
||||
style::conversion::Error error;
|
||||
return *style::conversion::convert<style::Filter, JSValue>(doc, error);
|
||||
return *style::conversion::convertJSON<style::Filter>(expression, error);
|
||||
}
|
||||
|
||||
static void Parse_Filter(benchmark::State& state) {
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
#include <mbgl/test/util.hpp>
|
||||
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/style/rapidjson_conversion.hpp>
|
||||
#include <mbgl/style/conversion/json.hpp>
|
||||
#include <mbgl/style/conversion/constant.hpp>
|
||||
#include <mbgl/style/conversion/function.hpp>
|
||||
#include <mbgl/util/rapidjson.hpp>
|
||||
|
||||
using namespace mbgl;
|
||||
using namespace mbgl::style;
|
||||
@@ -13,10 +11,8 @@ using namespace mbgl::style::conversion;
|
||||
TEST(StyleConversion, Function) {
|
||||
Error error;
|
||||
|
||||
auto parseFunction = [&](const std::string& src) {
|
||||
JSDocument doc;
|
||||
doc.Parse<0>(src);
|
||||
return convert<CameraFunction<float>, JSValue>(doc, error);
|
||||
auto parseFunction = [&](const std::string& json) {
|
||||
return convertJSON<CameraFunction<float>>(json, error);
|
||||
};
|
||||
|
||||
auto fn1 = parseFunction(R"({"stops":[]})");
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include <mbgl/test/util.hpp>
|
||||
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/style/conversion/json.hpp>
|
||||
#include <mbgl/style/conversion/geojson_options.hpp>
|
||||
#include <mbgl/test/conversion_stubs.hpp>
|
||||
|
||||
#include <mbgl/util/logging.hpp>
|
||||
|
||||
@@ -10,26 +9,22 @@ using namespace mbgl::style;
|
||||
using namespace mbgl::style::conversion;
|
||||
|
||||
TEST(GeoJSONOptions, Basic) {
|
||||
ValueMap map;
|
||||
Value raw(map);
|
||||
Error error;
|
||||
mbgl::optional<GeoJSONOptions> converted = convert<GeoJSONOptions>(raw, error);
|
||||
mbgl::optional<GeoJSONOptions> converted = convertJSON<GeoJSONOptions>("{}", error);
|
||||
ASSERT_TRUE((bool) converted);
|
||||
}
|
||||
|
||||
TEST(GeoJSONOptions, ErrorHandling) {
|
||||
ValueMap map {{"maxzoom", std::string{"should not be a string"}}};
|
||||
Value raw(map);
|
||||
Error error;
|
||||
mbgl::optional<GeoJSONOptions> converted = convert<GeoJSONOptions>(raw, error);
|
||||
mbgl::optional<GeoJSONOptions> converted = convertJSON<GeoJSONOptions>(R"JSON({
|
||||
"maxzoom": "should not be a string"
|
||||
})JSON", error);
|
||||
ASSERT_FALSE((bool) converted);
|
||||
}
|
||||
|
||||
TEST(GeoJSONOptions, RetainsDefaults) {
|
||||
ValueMap map;
|
||||
Value raw(map);
|
||||
Error error;
|
||||
GeoJSONOptions converted = *convert<GeoJSONOptions>(raw, error);
|
||||
GeoJSONOptions converted = *convertJSON<GeoJSONOptions>("{}", error);
|
||||
GeoJSONOptions defaults;
|
||||
|
||||
// GeoJSON-VT
|
||||
@@ -44,22 +39,16 @@ TEST(GeoJSONOptions, RetainsDefaults) {
|
||||
ASSERT_EQ(converted.clusterMaxZoom, defaults.clusterMaxZoom);
|
||||
}
|
||||
|
||||
|
||||
TEST(GeoJSONOptions, FullConversion) {
|
||||
ValueMap map {
|
||||
// GeoJSON-VT
|
||||
{"maxzoom", 1.0f},
|
||||
{"buffer", 2.0f},
|
||||
{"tolerance", 3.0f},
|
||||
|
||||
// Supercluster
|
||||
{"cluster", true},
|
||||
{"clusterRadius", 4.0f},
|
||||
{"clusterMaxZoom", 5.0f}
|
||||
};
|
||||
Value raw(map);
|
||||
Error error;
|
||||
GeoJSONOptions converted = *convert<GeoJSONOptions>(raw, error);
|
||||
GeoJSONOptions converted = *convertJSON<GeoJSONOptions>(R"JSON({
|
||||
"maxzoom": 1,
|
||||
"buffer": 2,
|
||||
"tolerance": 3,
|
||||
"cluster": true,
|
||||
"clusterRadius": 4,
|
||||
"clusterMaxZoom": 5
|
||||
})JSON", error);
|
||||
|
||||
// GeoJSON-VT
|
||||
ASSERT_EQ(converted.minzoom, 0);
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
#include <mbgl/test/util.hpp>
|
||||
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/style/rapidjson_conversion.hpp>
|
||||
#include <mbgl/style/conversion/json.hpp>
|
||||
#include <mbgl/style/conversion/layer.hpp>
|
||||
#include <mbgl/style/layers/background_layer_impl.hpp>
|
||||
#include <mbgl/util/rapidjson.hpp>
|
||||
|
||||
using namespace mbgl;
|
||||
using namespace mbgl::style;
|
||||
@@ -12,10 +10,8 @@ using namespace mbgl::style::conversion;
|
||||
using namespace std::literals::chrono_literals;
|
||||
|
||||
std::unique_ptr<Layer> parseLayer(const std::string& src) {
|
||||
JSDocument doc;
|
||||
doc.Parse<0>(src);
|
||||
Error error;
|
||||
return std::move(*convert<std::unique_ptr<Layer>, JSValue>(doc, error));
|
||||
return std::move(*convertJSON<std::unique_ptr<Layer>>(src, error));
|
||||
}
|
||||
|
||||
TEST(StyleConversion, LayerTransition) {
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
#include <mbgl/test/util.hpp>
|
||||
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/style/rapidjson_conversion.hpp>
|
||||
#include <mbgl/style/conversion/json.hpp>
|
||||
#include <mbgl/style/conversion/constant.hpp>
|
||||
#include <mbgl/style/conversion/light.hpp>
|
||||
#include <mbgl/style/position.hpp>
|
||||
#include <mbgl/util/rapidjson.hpp>
|
||||
#include <mbgl/util/color.hpp>
|
||||
#include <mbgl/util/chrono.hpp>
|
||||
|
||||
@@ -19,9 +18,7 @@ TEST(StyleConversion, Light) {
|
||||
Error error;
|
||||
|
||||
auto parseLight = [&](const std::string& src) {
|
||||
JSDocument doc;
|
||||
doc.Parse<0>(src);
|
||||
return convert<Light>(doc, error);
|
||||
return convertJSON<Light>(src, error);
|
||||
};
|
||||
|
||||
{
|
||||
|
||||
@@ -4,20 +4,15 @@
|
||||
|
||||
#include <mbgl/style/filter.hpp>
|
||||
#include <mbgl/style/filter_evaluator.hpp>
|
||||
#include <mbgl/style/rapidjson_conversion.hpp>
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/style/conversion/json.hpp>
|
||||
#include <mbgl/style/conversion/filter.hpp>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
using namespace mbgl;
|
||||
using namespace mbgl::style;
|
||||
|
||||
Filter parse(const char * expression) {
|
||||
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> doc;
|
||||
doc.Parse<0>(expression);
|
||||
conversion::Error error;
|
||||
optional<Filter> filter = conversion::convert<Filter, JSValue>(doc, error);
|
||||
optional<Filter> filter = conversion::convertJSON<Filter>(expression, error);
|
||||
EXPECT_TRUE(bool(filter));
|
||||
return *filter;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user