JsonHelper.h 6.08 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Don Gagne's avatar
Don Gagne committed
10 11 12 13
#ifndef JsonHelper_H
#define JsonHelper_H

#include <QJsonObject>
14
#include <QVariantList>
Don Gagne's avatar
Don Gagne committed
15 16
#include <QGeoCoordinate>

17 18
class QmlObjectListModel;

Don Gagne's avatar
Don Gagne committed
19 20 21
class JsonHelper
{
public:
22 23 24 25 26
    /// Determines is the specified data is a json file
    ///     @param jsonDoc Returned json document if json file
    /// @return true: file is json, false: file is not json
    static bool isJsonFile(const QByteArray& bytes, QJsonDocument& jsonDoc);

27 28 29
    /// Validates the standard parts of a QGC json file:
    ///     jsonFileTypeKey - Required and checked to be equal to expectedFileType
    ///     jsonVersionKey - Required and checked to be below supportedMajorVersion, supportedMinorVersion
Don Gagne's avatar
Don Gagne committed
30
    ///     jsonGroundStationKey - Required and checked to be string type
31
    /// @return false: validation failed
Don Gagne's avatar
Don Gagne committed
32
    static bool validateQGCJsonFile(const QJsonObject&  jsonObject,             ///< root json object
33
                                    const QString&      expectedFileType,       ///< correct file type for file
Don Gagne's avatar
Don Gagne committed
34 35 36
                                    int                 minSupportedVersion,    ///< minimum supported version
                                    int                 maxSupportedVersion,    ///< maximum supported major version
                                    int                 &version,               ///< returned file version
37 38
                                    QString&            errorString);           ///< returned error string if validation fails

Don Gagne's avatar
Don Gagne committed
39
    static bool validateRequiredKeys(const QJsonObject& jsonObject, const QStringList& keys, QString& errorString);
40
    static bool validateKeyTypes(const QJsonObject& jsonObject, const QStringList& keys, const QList<QJsonValue::Type>& types, QString& errorString);
41

Don Gagne's avatar
Don Gagne committed
42 43 44 45 46 47 48 49
    typedef struct {
        const char*         key;        ///< json key name
        QJsonValue::Type    type;       ///< type of key
        bool                required;   ///< true: key must be present
    } KeyValidateInfo;

    static bool validateKeys(const QJsonObject& jsonObject, const QList<KeyValidateInfo>& keyInfo, QString& errorString);

50 51 52 53 54 55 56
    /// Loads a QGeoCoordinate
    /// @return false: validation failed
    static bool loadGeoCoordinate(const QJsonValue& jsonValue,          ///< json value to load from
                                  bool              altitudeRequired,   ///< true: altitude must be specified
                                  QGeoCoordinate&   coordinate,         ///< returned QGeoCordinate
                                  QString&          errorString);       ///< returned error string if load failure

57 58 59 60 61 62
    /// Loads a polygon from an array
    static bool loadPolygon(const QJsonArray&   polygonArray,   ///< Array of coordinates
                            QmlObjectListModel& list,           ///< Empty list to add vertices to
                            QObject*            parent,         ///< parent for newly allocated QGCQGeoCoordinates
                            QString&            errorString);   ///< returned error string if load failure

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    /// Saves a QGeoCoordinate
    static void saveGeoCoordinate(const QGeoCoordinate& coordinate,     ///< QGeoCoordinate to save
                                  bool                  writeAltitude,  ///< true: write altitude to json
                                  QJsonValue&           jsonValue);     ///< json value to save to

    /// Loads a list of QGeoCoordinates from a json array
    /// @return false: validation failed
    static bool loadGeoCoordinateArray(const QJsonValue&    jsonValue,              ///< json value which contains points
                                       bool                 altitudeRequired,       ///< true: altitude field must be specified
                                       QVariantList&        rgVarPoints,            ///< returned points
                                       QString&             errorString);           ///< returned error string if load failure
    static bool loadGeoCoordinateArray(const QJsonValue&        jsonValue,          ///< json value which contains points
                                       bool                     altitudeRequired,   ///< true: altitude field must be specified
                                       QList<QGeoCoordinate>&   rgPoints,           ///< returned points
                                       QString&                 errorString);       ///< returned error string if load failure

    /// Saves a list of QGeoCoordinates to a json array
    static void saveGeoCoordinateArray(const QVariantList&  rgVarPoints,            ///< points to save
                                       bool                 writeAltitude,          ///< true: write altitide value
                                       QJsonValue&          jsonValue);             ///< json value to save to
    static void saveGeoCoordinateArray(const QList<QGeoCoordinate>& rgPoints,       ///< points to save
                                       bool                         writeAltitude,  ///< true: write altitide value
                                       QJsonValue&                  jsonValue);     ///< json value to save to

87 88 89 90
    /// Saves a polygon to a json array
    static void savePolygon(QmlObjectListModel& list,           ///< List which contains vertices
                            QJsonArray&         polygonArray);  ///< Array to save into

91
    static bool parseEnum(const QJsonObject& jsonObject, QStringList& enumStrings, QStringList& enumValues, QString& errorString);
Don Gagne's avatar
Don Gagne committed
92

93

94 95 96 97 98 99
    static const char* jsonVersionKey;
    static const char* jsonGroundStationKey;
    static const char* jsonGroundStationValue;
    static const char* jsonFileTypeKey;

private:
Don Gagne's avatar
Don Gagne committed
100 101
    static QString _jsonValueTypeToString(QJsonValue::Type type);

Don Gagne's avatar
Don Gagne committed
102 103
    static const char*  _enumStringsJsonKey;
    static const char*  _enumValuesJsonKey;
Don Gagne's avatar
Don Gagne committed
104 105 106
};

#endif