Loading...
Searching...
No Matches
jsonstreamwriter.cpp
Go to the documentation of this file.
/**
* \file pappsomspp/processing/cbor/jsonstreamwriter.cpp
* \date 19/07/2025
* \author Olivier Langella
* \brief PAPPSO JSON stream writer
*/
/*******************************************************************************
* Copyright (c) 2025 Olivier Langella <Olivier.Langella@universite-paris-saclay.fr>.
*
* This file is part of PAPPSOms-tools.
*
* PAPPSOms-tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PAPPSOms-tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PAPPSOms-tools. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include "jsonstreamwriter.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <qjsonvalue.h>
pappso::cbor::JsonStreamWriter::JsonStreamWriter(QIODevice *device) : QTextStream(device)
{
}
pappso::cbor::JsonStreamWriter::~JsonStreamWriter()
{
}
void
pappso::cbor::JsonStreamWriter::endArray()
{
*this << "]" << Qt::endl;
m_isStart = false;
m_nextIsValue = false;
}
void
pappso::cbor::JsonStreamWriter::endMap()
{
*this << "}" << Qt::endl;
m_isStart = false;
m_nextIsValue = false;
}
void
pappso::cbor::JsonStreamWriter::startArray()
{
if(!m_nextIsValue)
comma();
*this << Qt::endl << "[";
m_isStart = true;
}
void
pappso::cbor::JsonStreamWriter::startMap()
{
if(!m_nextIsValue)
comma();
*this << Qt::endl << "{";
m_isStart = true;
}
void
pappso::cbor::JsonStreamWriter::writeCborMap(const QCborMap &cbor_map)
{
QJsonDocument doc;
doc.setObject(QCborValue(cbor_map).toJsonValue().toObject());
*this << doc.toJson();
}
void
pappso::cbor::JsonStreamWriter::writeCborArray(const QCborArray &cbor_array)
{
QJsonDocument doc;
doc.setArray(QCborValue(cbor_array).toJsonValue().toArray());
*this << doc.toJson();
}
void
pappso::cbor::JsonStreamWriter::appendKey(const QString &key)
{
if(!m_isStart)
comma();
*this << "\"" << key << "\":";
m_isStart = false;
m_nextIsValue = true;
}
void
pappso::cbor::JsonStreamWriter::appendValue(const QString &value)
{
*this << "\"" << value << "\"";
m_nextIsValue = false;
}
void
pappso::cbor::JsonStreamWriter::appendValue(std::size_t integer_size_value)
{
*this << integer_size_value;
m_nextIsValue = false;
}
void
pappso::cbor::JsonStreamWriter::writeArray(QStringList &str_list)
{
*this << "[\"" << str_list.join("\", \"") << "\"]" << Qt::endl;
}
void
pappso::cbor::JsonStreamWriter::comma()
{
*this << "," << Qt::endl;
m_isStart = false;
}
pappso::cbor::JsonStreamWriter::comma
void comma()
Definition jsonstreamwriter.cpp:127
pappso::cbor::JsonStreamWriter::appendValue
void appendValue(const QString &value)
writes String value to JSON text
Definition jsonstreamwriter.cpp:106
pappso::cbor::JsonStreamWriter::endArray
void endArray()
ends an array in JSON output simply print "]"
Definition jsonstreamwriter.cpp:44
pappso::cbor::JsonStreamWriter::m_nextIsValue
bool m_nextIsValue
Definition jsonstreamwriter.h:115
pappso::cbor::JsonStreamWriter::endMap
void endMap()
ends an object in JSON output simply print "}"
Definition jsonstreamwriter.cpp:52
pappso::cbor::JsonStreamWriter::writeCborArray
void writeCborArray(const QCborArray &cbor_array)
converts a CBOR array (list) into JSON text in output
Definition jsonstreamwriter.cpp:86
pappso::cbor::JsonStreamWriter::JsonStreamWriter
JsonStreamWriter(QIODevice *device)
build a QTextStream for JSON writer
Definition jsonstreamwriter.cpp:35
pappso::cbor::JsonStreamWriter::m_isStart
bool m_isStart
Definition jsonstreamwriter.h:114
pappso::cbor::JsonStreamWriter::startMap
void startMap()
start an object in JSON output simply print "{"
Definition jsonstreamwriter.cpp:68
pappso::cbor::JsonStreamWriter::writeArray
void writeArray(QStringList &str_list)
convert a string list to a JSON array
Definition jsonstreamwriter.cpp:121
pappso::cbor::JsonStreamWriter::appendKey
void appendKey(const QString &key)
writes a key (dictionary entry) to JSON text
Definition jsonstreamwriter.cpp:96
pappso::cbor::JsonStreamWriter::~JsonStreamWriter
virtual ~JsonStreamWriter()
Definition jsonstreamwriter.cpp:39
pappso::cbor::JsonStreamWriter::writeCborMap
void writeCborMap(const QCborMap &cbor_map)
converts a CBOR map (object) into JSON text in output
Definition jsonstreamwriter.cpp:77
pappso::cbor::JsonStreamWriter::startArray
void startArray()
starts an array in JSON output simply print "["
Definition jsonstreamwriter.cpp:60
jsonstreamwriter.h