blob: ff7ee2eadab765bb1df07e3842d7b4ce37b35e3a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#pragma once
#include <mapbox/geojson.hpp>
#include <mbgl/style/conversion/geojson.hpp>
#include <mbgl/util/rapidjson.hpp>
#include <sstream>
#include <string>
#include <QByteArray>
#include <QVariant>
namespace mbgl {
namespace style {
namespace conversion {
template <>
Result<GeoJSON> convertGeoJSON(const QVariant& value) {
if (value.type() != QVariant::ByteArray) {
return Error { "JSON data must be in QByteArray" };
}
auto data = value.toByteArray();
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> d;
if (data.endsWith(char(0))) {
d.Parse<0>(value.toByteArray().data());
} else {
d.Parse<0>(value.toByteArray().constData());
}
// Needed to keep the error message alive
// when we go out of this scope.
static std::string error;
if (d.HasParseError()) {
std::stringstream message;
message << d.GetErrorOffset() << " - " << rapidjson::GetParseError_En(d.GetParseError());
error = message.str();
return Error { error.c_str() };
}
conversion::Result<GeoJSON> geoJSON = conversion::convertGeoJSON<JSValue>(d);
if (!geoJSON) {
error = geoJSON.error().message;
return Error { error.c_str() };
}
return geoJSON;
}
} // namespace conversion
} // namespace style
} // namespace mbgl
|