summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2016-01-27 23:28:04 -0800
committerJohn Firebaugh <john.firebaugh@gmail.com>2016-01-28 17:37:23 -0800
commitd90d374203415b5d7336d8902ad4f1fb0f9ec923 (patch)
tree28d561d4330da1c9a7668d399e63351eda01fa5b
parentb738087080b924061c4e6ce4c8b60ae4573f4f10 (diff)
downloadqtlocation-mapboxgl-d90d374203415b5d7336d8902ad4f1fb0f9ec923.tar.gz
[core] Move rapidjson document creation into StyleParser
-rw-r--r--src/mbgl/style/style.cpp12
-rw-r--r--src/mbgl/style/style_parser.cpp13
-rw-r--r--src/mbgl/style/style_parser.hpp2
-rw-r--r--test/miscellaneous/style_parser.cpp7
4 files changed, 15 insertions, 19 deletions
diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp
index 18b0d7f3fd..1ffd75bf69 100644
--- a/src/mbgl/style/style.cpp
+++ b/src/mbgl/style/style.cpp
@@ -23,9 +23,6 @@
#include <csscolorparser/csscolorparser.hpp>
-#include <rapidjson/document.h>
-#include <rapidjson/error/en.h>
-
#include <algorithm>
namespace mbgl {
@@ -46,15 +43,8 @@ void Style::setJSON(const std::string& json, const std::string&) {
sources.clear();
layers.clear();
- rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> doc;
- doc.Parse<0>((const char *const)json.c_str());
- if (doc.HasParseError()) {
- Log::Error(Event::ParseStyle, "Error parsing style JSON at %i: %s", doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError()));
- return;
- }
-
StyleParser parser;
- parser.parse(doc);
+ parser.parse(json);
for (auto& source : parser.sources) {
addSource(std::move(source));
diff --git a/src/mbgl/style/style_parser.cpp b/src/mbgl/style/style_parser.cpp
index 4c1fe593fc..63ba3d6fe8 100644
--- a/src/mbgl/style/style_parser.cpp
+++ b/src/mbgl/style/style_parser.cpp
@@ -11,6 +11,9 @@
#include <mapbox/geojsonvt.hpp>
#include <mapbox/geojsonvt/convert.hpp>
+#include <rapidjson/document.h>
+#include <rapidjson/error/en.h>
+
#include <algorithm>
namespace mbgl {
@@ -96,7 +99,15 @@ void parseTileJSONMember(const JSValue& value, std::array<float, N>& target, con
StyleParser::~StyleParser() = default;
-void StyleParser::parse(const JSValue& document) {
+void StyleParser::parse(const std::string& json) {
+ rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> document;
+ document.Parse<0>((const char *const)json.c_str());
+
+ if (document.HasParseError()) {
+ Log::Error(Event::ParseStyle, "Error parsing style JSON at %i: %s", document.GetErrorOffset(), rapidjson::GetParseError_En(document.GetParseError()));
+ return;
+ }
+
if (document.HasMember("version")) {
int version = document["version"].GetInt();
if (version != 8) {
diff --git a/src/mbgl/style/style_parser.hpp b/src/mbgl/style/style_parser.hpp
index d3bb401d44..58c4d736f2 100644
--- a/src/mbgl/style/style_parser.hpp
+++ b/src/mbgl/style/style_parser.hpp
@@ -20,7 +20,7 @@ class StyleParser {
public:
~StyleParser();
- void parse(const JSValue&);
+ void parse(const std::string&);
std::string spriteURL;
std::string glyphURL;
diff --git a/test/miscellaneous/style_parser.cpp b/test/miscellaneous/style_parser.cpp
index ab5e984752..46d0dab5d8 100644
--- a/test/miscellaneous/style_parser.cpp
+++ b/test/miscellaneous/style_parser.cpp
@@ -27,16 +27,11 @@ TEST_P(StyleParserTest, ParseStyle) {
ASSERT_FALSE(infoDoc.HasParseError());
ASSERT_TRUE(infoDoc.IsObject());
- rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> styleDoc;
- styleDoc.Parse<0>(util::read_file(base + ".style.json").c_str());
- ASSERT_FALSE(styleDoc.HasParseError());
- ASSERT_TRUE(styleDoc.IsObject());
-
FixtureLogObserver* observer = new FixtureLogObserver();
Log::setObserver(std::unique_ptr<Log::Observer>(observer));
StyleParser parser;
- parser.parse(styleDoc);
+ parser.parse(util::read_file(base + ".style.json"));
for (auto it = infoDoc.MemberBegin(), end = infoDoc.MemberEnd(); it != end; it++) {
const std::string name { it->name.GetString(), it->name.GetStringLength() };