diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2017-05-05 16:31:03 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2017-05-08 13:48:57 -0700 |
commit | baaae99a03202641ae5b8024e57b691fe61a6688 (patch) | |
tree | 459045e56197696aa1312594f1e452b03931775d /src/mbgl/style/conversion | |
parent | e34d8b5c802186008e49364668e4a7bd5668d0fd (diff) | |
download | qtlocation-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')
-rw-r--r-- | src/mbgl/style/conversion/geojson.cpp | 31 | ||||
-rw-r--r-- | src/mbgl/style/conversion/json.hpp | 28 |
2 files changed, 40 insertions, 19 deletions
diff --git a/src/mbgl/style/conversion/geojson.cpp b/src/mbgl/style/conversion/geojson.cpp index 0fba6e5b4d..8103e9014a 100644 --- a/src/mbgl/style/conversion/geojson.cpp +++ b/src/mbgl/style/conversion/geojson.cpp @@ -1,33 +1,26 @@ #include <mbgl/style/conversion/geojson.hpp> - +#include <mbgl/style/conversion/json.hpp> #include <mbgl/util/rapidjson.hpp> -#include <string> -#include <sstream> +#include <mapbox/geojson.hpp> +#include <mapbox/geojson/rapidjson.hpp> namespace mbgl { namespace style { namespace conversion { -template <> -optional<GeoJSON> convertGeoJSON(const std::string& string, Error& error) { - rapidjson::GenericDocument<rapidjson::UTF8<>, 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> Converter<GeoJSON>::operator()(const std::string& value, Error& error) const { + return convertJSON<GeoJSON>(value, error); +} - optional<GeoJSON> geoJSON = conversion::convertGeoJSON<JSValue>(d, error); - if (!geoJSON) { - error = { error.message }; +template <> +optional<GeoJSON> Converter<GeoJSON>::operator()(const JSValue& value, Error& error) const { + try { + return mapbox::geojson::convert(value); + } catch (const std::exception& ex) { + error = { ex.what() }; return {}; } - - return geoJSON; } } // namespace conversion 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 |