diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2019-03-06 12:41:27 +0100 |
---|---|---|
committer | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-05-06 19:20:17 +0300 |
commit | cac83398cf230dfaa5559b94bd7c75b7dcf03940 (patch) | |
tree | 509ad513c9e443d51caf339068cf0d195518e5c4 /src/mbgl/util/rapidjson.cpp | |
parent | e69efb996d32c86f00b08878a61c6f0a47c81932 (diff) | |
download | qtlocation-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.cpp | 12 |
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 |