diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index d4ea1ab5b..881aa0e67 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -302,6 +302,7 @@ set(MBGL_CORE_FILES include/mbgl/style/conversion/source.hpp include/mbgl/style/conversion/tileset.hpp include/mbgl/style/conversion/transition_options.hpp + src/mbgl/style/conversion/geojson.cpp src/mbgl/style/conversion/stringify.hpp # style/function diff --git a/platform/android/src/style/conversion/geojson.hpp b/platform/android/src/style/conversion/geojson.hpp index 6ac6abcd7..db474e854 100644 --- a/platform/android/src/style/conversion/geojson.hpp +++ b/platform/android/src/style/conversion/geojson.hpp @@ -5,45 +5,23 @@ #include #include #include -#include #include #include -#include -#include - namespace mbgl { namespace style { namespace conversion { template <> optional convertGeoJSON(const mbgl::android::Value& value, Error& error) { - // Value should be a string wrapped in an object mbgl::android::Value jsonValue = value.get("data"); if(value.isNull()) { error = { "no json data found" }; return {}; } - std::string jsonString = value.get("data").toString(); - rapidjson::GenericDocument, rapidjson::CrtAllocator> d; - d.Parse(jsonString.c_str()); - - if (d.HasParseError()) { - std::stringstream message; - message << d.GetErrorOffset() << " - " << rapidjson::GetParseError_En(d.GetParseError()); - error = { message.str() }; - return {}; - } - - optional geoJSON = conversion::convertGeoJSON(d, error); - if (!geoJSON) { - error = { error.message }; - return {}; - } - - return geoJSON; + return convertGeoJSON(value.get("data").toString(), error); } template <> diff --git a/platform/qt/src/qt_geojson.hpp b/platform/qt/src/qt_geojson.hpp index 7c50c663d..7d12660ae 100644 --- a/platform/qt/src/qt_geojson.hpp +++ b/platform/qt/src/qt_geojson.hpp @@ -2,7 +2,6 @@ #include #include -#include #include @@ -10,7 +9,6 @@ #include #include -#include #include namespace QMapbox { @@ -197,29 +195,8 @@ optional convertGeoJSON(const QVariant& value, Error& error) { return {}; } - auto data = value.toByteArray(); - - rapidjson::GenericDocument, rapidjson::CrtAllocator> d; - if (data.endsWith(char(0))) { - d.Parse<0>(value.toByteArray().data()); - } else { - d.Parse<0>(value.toByteArray().constData()); - } - - if (d.HasParseError()) { - std::stringstream message; - message << d.GetErrorOffset() << " - " << rapidjson::GetParseError_En(d.GetParseError()); - error = { message.str() }; - return {}; - } - - optional geoJSON = convertGeoJSON(d, error); - if (!geoJSON) { - error = { error.message }; - return {}; - } - - return geoJSON; + QByteArray data = value.toByteArray(); + return convertGeoJSON(std::string(data.constData(), data.size()), error); } } // namespace conversion diff --git a/src/mbgl/style/conversion/geojson.cpp b/src/mbgl/style/conversion/geojson.cpp new file mode 100644 index 000000000..0fba6e5b4 --- /dev/null +++ b/src/mbgl/style/conversion/geojson.cpp @@ -0,0 +1,35 @@ +#include + +#include + +#include +#include + +namespace mbgl { +namespace style { +namespace conversion { + +template <> +optional convertGeoJSON(const std::string& string, Error& error) { + rapidjson::GenericDocument, rapidjson::CrtAllocator> d; + d.Parse(string.c_str()); + + if (d.HasParseError()) { + std::stringstream message; + message << d.GetErrorOffset() << " - " << rapidjson::GetParseError_En(d.GetParseError()); + error = { message.str() }; + return {}; + } + + optional geoJSON = conversion::convertGeoJSON(d, error); + if (!geoJSON) { + error = { error.message }; + return {}; + } + + return geoJSON; +} + +} // namespace conversion +} // namespace style +} // namespace mbgl