summaryrefslogtreecommitdiff
path: root/src/mbgl/util/rapidjson.cpp
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2019-03-06 12:41:27 +0100
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2019-05-06 19:20:17 +0300
commitcac83398cf230dfaa5559b94bd7c75b7dcf03940 (patch)
tree509ad513c9e443d51caf339068cf0d195518e5c4 /src/mbgl/util/rapidjson.cpp
parente69efb996d32c86f00b08878a61c6f0a47c81932 (diff)
downloadqtlocation-mapboxgl-upstream/json-parse-line-col.tar.gz
[core] show line and col instead of offset when JSON parsing failsupstream/json-parse-line-col
Diffstat (limited to 'src/mbgl/util/rapidjson.cpp')
-rw-r--r--src/mbgl/util/rapidjson.cpp12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/mbgl/util/rapidjson.cpp b/src/mbgl/util/rapidjson.cpp
index 32a3c0b929..0504f8c002 100644
--- a/src/mbgl/util/rapidjson.cpp
+++ b/src/mbgl/util/rapidjson.cpp
@@ -1,11 +1,17 @@
#include <mbgl/util/rapidjson.hpp>
#include <mbgl/util/string.hpp>
+#include <algorithm>
+
namespace mbgl {
-std::string formatJSONParseError(const JSDocument& doc) {
- return std::string{ rapidjson::GetParseError_En(doc.GetParseError()) } + " at offset " +
- util::toString(doc.GetErrorOffset());
+std::string formatJSONParseError(const std::string& json, const JSDocument& doc) {
+ const size_t offset = std::min(doc.GetErrorOffset(), json.length());
+ const size_t lastBreak = json.find_last_of('\n', offset);
+ const size_t col = offset - (lastBreak == std::string::npos ? 0 : lastBreak);
+ const size_t line = lastBreak == std::string::npos ? 1 : std::count(json.data(), json.data() + lastBreak, '\n') + 2;
+ return "on line " + util::toString(line) + " col " + util::toString(col) + ": " +
+ rapidjson::GetParseError_En(doc.GetParseError());
}
} // namespace mbgl