Skip to content
Snippets Groups Projects
json.cpp 2.01 KiB
Newer Older
akiraohgaki's avatar
akiraohgaki committed
/**
 * A library for Qt app
 *
 * LICENSE: The GNU Lesser General Public License, version 3.0
 *
 * @author      Akira Ohgaki <akiraohgaki@gmail.com>
 * @copyright   Akira Ohgaki
 * @license     https://opensource.org/licenses/LGPL-3.0  The GNU Lesser General Public License, version 3.0
 * @link        https://github.com/akiraohgaki/qtlibs
 */

#include "json.h"

#include <QJsonDocument>
#include <QJsonParseError>

namespace qtlibs {

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
    QJsonDocument doc(object);
    setJson(doc.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
    QJsonDocument doc(array);
    setJson(doc.toJson());
akiraohgaki's avatar
akiraohgaki committed
}

QByteArray Json::toJson()
{
    QJsonDocument doc = QJsonDocument::fromJson(json());
    return doc.toJson();
}

QJsonObject Json::toObject()
{
    QJsonDocument doc = QJsonDocument::fromJson(json());
    return doc.object();
}

QJsonArray Json::toArray()
{
    QJsonDocument doc = QJsonDocument::fromJson(json());
    return doc.array();
}

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()
{
    QJsonDocument doc = QJsonDocument::fromJson(json());
    return doc.isObject();
}

bool Json::isArray()
{
    QJsonDocument doc = QJsonDocument::fromJson(json());
    return doc.isArray();
}

akiraohgaki's avatar
akiraohgaki committed
} // namespace qtlibs