summaryrefslogtreecommitdiff
path: root/platform/qt/src/qt_geojson.hpp
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <thiago@mapbox.com>2016-07-08 17:19:54 +0300
committerThiago Marcos P. Santos <thiago@mapbox.com>2016-07-11 17:01:32 +0300
commit5ad22bd672c84328c1274f7413959642d2a92576 (patch)
tree9a408da03da152f94d5ded4f009c747f9ac24a29 /platform/qt/src/qt_geojson.hpp
parentabeee2087c151ccc492b2aff60b54dc6f8ec9fd1 (diff)
downloadqtlocation-mapboxgl-5ad22bd672c84328c1274f7413959642d2a92576.tar.gz
[Qt] Implement a GeoJSON coverter from QByteArray
Diffstat (limited to 'platform/qt/src/qt_geojson.hpp')
-rw-r--r--platform/qt/src/qt_geojson.hpp45
1 files changed, 43 insertions, 2 deletions
diff --git a/platform/qt/src/qt_geojson.hpp b/platform/qt/src/qt_geojson.hpp
index cc0b15a6cd..ff7ee2eada 100644
--- a/platform/qt/src/qt_geojson.hpp
+++ b/platform/qt/src/qt_geojson.hpp
@@ -1,12 +1,53 @@
+#pragma once
+
+#include <mapbox/geojson.hpp>
#include <mbgl/style/conversion/geojson.hpp>
+#include <mbgl/util/rapidjson.hpp>
+
+#include <sstream>
+#include <string>
+
+#include <QByteArray>
+#include <QVariant>
namespace mbgl {
namespace style {
namespace conversion {
template <>
-Result<GeoJSON> convertGeoJSON(const QVariant&) {
- return Error { "not implemented" };
+Result<GeoJSON> convertGeoJSON(const QVariant& value) {
+ if (value.type() != QVariant::ByteArray) {
+ return Error { "JSON data must be in QByteArray" };
+ }
+
+ auto data = value.toByteArray();
+
+ rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> d;
+ if (data.endsWith(char(0))) {
+ d.Parse<0>(value.toByteArray().data());
+ } else {
+ d.Parse<0>(value.toByteArray().constData());
+ }
+
+ // Needed to keep the error message alive
+ // when we go out of this scope.
+ static std::string error;
+
+ if (d.HasParseError()) {
+ std::stringstream message;
+ message << d.GetErrorOffset() << " - " << rapidjson::GetParseError_En(d.GetParseError());
+
+ error = message.str();
+ return Error { error.c_str() };
+ }
+
+ conversion::Result<GeoJSON> geoJSON = conversion::convertGeoJSON<JSValue>(d);
+ if (!geoJSON) {
+ error = geoJSON.error().message;
+ return Error { error.c_str() };
+ }
+
+ return geoJSON;
}
} // namespace conversion