Skip to content
Snippets Groups Projects
qtlib_json.cpp 1.72 KiB
Newer Older
akiraohgaki's avatar
akiraohgaki committed
/**
akiraohgaki's avatar
akiraohgaki committed
 * qtlib
akiraohgaki's avatar
akiraohgaki committed
 *
 * @author      Akira Ohgaki <akiraohgaki@gmail.com>
 * @copyright   Akira Ohgaki
akiraohgaki's avatar
akiraohgaki committed
 * @license     https://opensource.org/licenses/LGPL-3.0
akiraohgaki's avatar
akiraohgaki committed
 * @link        https://github.com/akiraohgaki/qtlib
akiraohgaki's avatar
akiraohgaki committed
 */

akiraohgaki's avatar
akiraohgaki committed
#include "qtlib_json.h"
akiraohgaki's avatar
akiraohgaki committed

#include <QJsonDocument>
#include <QJsonParseError>

akiraohgaki's avatar
akiraohgaki committed
namespace qtlib {
akiraohgaki's avatar
akiraohgaki committed

akiraohgaki's avatar
akiraohgaki committed
Json::Json(const QByteArray &json, QObject *parent)
    : QObject(parent), json_(json)
akiraohgaki's avatar
akiraohgaki committed
{}

akiraohgaki's avatar
akiraohgaki committed
Json::Json(const QJsonObject &object, QObject *parent)
    : QObject(parent)
akiraohgaki's avatar
akiraohgaki committed
{
akiraohgaki's avatar
akiraohgaki committed
    fromObject(object);
akiraohgaki's avatar
akiraohgaki committed
}

akiraohgaki's avatar
akiraohgaki committed
Json::Json(const QJsonArray &array, QObject *parent)
    : QObject(parent)
akiraohgaki's avatar
akiraohgaki committed
{
akiraohgaki's avatar
akiraohgaki committed
    fromArray(array);
akiraohgaki's avatar
akiraohgaki committed
}

akiraohgaki's avatar
akiraohgaki committed
Json::Json(const Json &other, QObject *parent)
    : QObject(parent)
akiraohgaki's avatar
akiraohgaki committed
{
akiraohgaki's avatar
akiraohgaki committed
    setJson(other.json());
}

Json &Json::operator =(const Json &other)
{
    setJson(other.json());
    return *this;
akiraohgaki's avatar
akiraohgaki committed
}

QByteArray Json::json() const
{
    return json_;
}

void Json::setJson(const QByteArray &json)
{
    json_ = json;
}

akiraohgaki's avatar
akiraohgaki committed
void Json::fromObject(const QJsonObject &object)
akiraohgaki's avatar
akiraohgaki committed
{
akiraohgaki's avatar
akiraohgaki committed
    setJson(QJsonDocument(object).toJson());
akiraohgaki's avatar
akiraohgaki committed
}

akiraohgaki's avatar
akiraohgaki committed
void Json::fromArray(const QJsonArray &array)
akiraohgaki's avatar
akiraohgaki committed
{
akiraohgaki's avatar
akiraohgaki committed
    setJson(QJsonDocument(array).toJson());
akiraohgaki's avatar
akiraohgaki committed
}

QByteArray Json::toJson()
{
akiraohgaki's avatar
akiraohgaki committed
    return QJsonDocument::fromJson(json()).toJson();
akiraohgaki's avatar
akiraohgaki committed
}

QJsonObject Json::toObject()
{
akiraohgaki's avatar
akiraohgaki committed
    return QJsonDocument::fromJson(json()).object();
akiraohgaki's avatar
akiraohgaki committed
}

QJsonArray Json::toArray()
{
akiraohgaki's avatar
akiraohgaki committed
    return QJsonDocument::fromJson(json()).array();
akiraohgaki's avatar
akiraohgaki committed
}

akiraohgaki's avatar
akiraohgaki committed
bool Json::isValid()
{
    QJsonParseError parseError;
    QJsonDocument::fromJson(json(), &parseError);
    if (parseError.error == QJsonParseError::NoError) {
        return true;
    }
    return false;
}

bool Json::isObject()
{
akiraohgaki's avatar
akiraohgaki committed
    return QJsonDocument::fromJson(json()).isObject();
akiraohgaki's avatar
akiraohgaki committed
}

bool Json::isArray()
{
akiraohgaki's avatar
akiraohgaki committed
    return QJsonDocument::fromJson(json()).isArray();
akiraohgaki's avatar
akiraohgaki committed
}

akiraohgaki's avatar
akiraohgaki committed
} // namespace qtlib