summaryrefslogtreecommitdiff
path: root/src/mbgl/style/conversion/json.hpp
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2017-05-05 16:31:03 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2017-05-08 13:48:57 -0700
commitbaaae99a03202641ae5b8024e57b691fe61a6688 (patch)
tree459045e56197696aa1312594f1e452b03931775d /src/mbgl/style/conversion/json.hpp
parente34d8b5c802186008e49364668e4a7bd5668d0fd (diff)
downloadqtlocation-mapboxgl-baaae99a03202641ae5b8024e57b691fe61a6688.tar.gz
[core, android] Factor JSON string conversions
This adds a `convertJSON` template, to be used like: Error error optional<Foo> foo = convertJSON<Foo>(string, error); Internally, it parses the string with RapidJSON and then calls `convert<Foo>(parsed, error)`. While here, rationalize GeoJSON converters and fix error handling for Tileset conversion in OfflineDownload.
Diffstat (limited to 'src/mbgl/style/conversion/json.hpp')
-rw-r--r--src/mbgl/style/conversion/json.hpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/mbgl/style/conversion/json.hpp b/src/mbgl/style/conversion/json.hpp
new file mode 100644
index 0000000000..0817ac09df
--- /dev/null
+++ b/src/mbgl/style/conversion/json.hpp
@@ -0,0 +1,28 @@
+#include <mbgl/style/conversion.hpp>
+#include <mbgl/style/rapidjson_conversion.hpp>
+
+#include <string>
+#include <sstream>
+
+namespace mbgl {
+namespace style {
+namespace conversion {
+
+template <class T, class...Args>
+optional<T> convertJSON(const std::string& json, Error& error, Args&&...args) {
+ JSDocument document;
+ document.Parse<0>(json.c_str());
+
+ if (document.HasParseError()) {
+ std::stringstream message;
+ message << document.GetErrorOffset() << " - " << rapidjson::GetParseError_En(document.GetParseError());
+ error = { message.str() };
+ return {};
+ }
+
+ return convert<T, JSValue>(document, error, std::forward<Args>(args)...);
+}
+
+} // namespace conversion
+} // namespace style
+} // namespace mbgl