summaryrefslogtreecommitdiff
path: root/platform/qt/src/qt_geojson.hpp
blob: 07813623fd5ead1ed0e753000fa5e5e62696d669 (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
#pragma once

#include <mapbox/geojson.hpp>
#include <mbgl/style/conversion/geojson.hpp>
#include <mbgl/util/rapidjson.hpp>

#include <QByteArray>
#include <QVariant>

#include <sstream>
#include <string>

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());
    }

    if (d.HasParseError()) {
        std::stringstream message;
        message << d.GetErrorOffset() << " - " << rapidjson::GetParseError_En(d.GetParseError());

        return Error { message.str() };
    }

    conversion::Result<GeoJSON> geoJSON = conversion::convertGeoJSON<JSValue>(d);
    if (!geoJSON) {
        return Error { geoJSON.error().message };
    }

    return geoJSON;
}

} // namespace conversion
} // namespace style
} // namespace mbgl