diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index 18b0d7f3f..1ffd75bf6 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -23,9 +23,6 @@ #include -#include -#include - #include namespace mbgl { @@ -46,15 +43,8 @@ void Style::setJSON(const std::string& json, const std::string&) { sources.clear(); layers.clear(); - rapidjson::GenericDocument, 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 4c1fe593f..63ba3d6fe 100644 --- a/src/mbgl/style/style_parser.cpp +++ b/src/mbgl/style/style_parser.cpp @@ -11,6 +11,9 @@ #include #include +#include +#include + #include namespace mbgl { @@ -96,7 +99,15 @@ void parseTileJSONMember(const JSValue& value, std::array& target, con StyleParser::~StyleParser() = default; -void StyleParser::parse(const JSValue& document) { +void StyleParser::parse(const std::string& json) { + rapidjson::GenericDocument, 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 d3bb401d4..58c4d736f 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 ab5e98475..46d0dab5d 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::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(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() };