libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
pappso::cbor::JsonStreamWriter Class Reference

helper class to write json text using CBOR data structures More...

#include <jsonstreamwriter.h>

Inheritance diagram for pappso::cbor::JsonStreamWriter:

Public Member Functions

 JsonStreamWriter (QIODevice *device)
 build a QTextStream for JSON writer
virtual ~JsonStreamWriter ()
void startArray ()
 starts an array in JSON output simply print "["
void endArray ()
 ends an array in JSON output simply print "]"
void startMap ()
 start an object in JSON output simply print "{"
void endMap ()
 ends an object in JSON output simply print "}"
void writeCborMap (const QCborMap &cbor_map)
 converts a CBOR map (object) into JSON text in output
void writeCborArray (const QCborArray &cbor_array)
 converts a CBOR array (list) into JSON text in output
void appendKey (const QString &key)
 writes a key (dictionary entry) to JSON text
void appendValue (const QString &value)
 writes String value to JSON text
void appendValue (std::size_t integer_size_value)
 writes an integer value to JSON text
void writeArray (QStringList &str_list)
 convert a string list to a JSON array

Protected Member Functions

void comma ()

Private Attributes

bool m_isStart = true
bool m_nextIsValue = false

Detailed Description

helper class to write json text using CBOR data structures

Definition at line 42 of file jsonstreamwriter.h.

Constructor & Destructor Documentation

◆ JsonStreamWriter()

pappso::cbor::JsonStreamWriter::JsonStreamWriter ( QIODevice * device)

build a QTextStream for JSON writer

Definition at line 35 of file jsonstreamwriter.cpp.

35 : QTextStream(device)
36{
37}

◆ ~JsonStreamWriter()

pappso::cbor::JsonStreamWriter::~JsonStreamWriter ( )
virtual

Destructor

Definition at line 39 of file jsonstreamwriter.cpp.

40{
41}

Member Function Documentation

◆ appendKey()

void pappso::cbor::JsonStreamWriter::appendKey ( const QString & key)

writes a key (dictionary entry) to JSON text

Definition at line 96 of file jsonstreamwriter.cpp.

97{
98 if(!m_isStart)
99 comma();
100 *this << "\"" << key << "\":";
101 m_isStart = false;
102 m_nextIsValue = true;
103}

References comma(), m_isStart, and m_nextIsValue.

◆ appendValue() [1/2]

void pappso::cbor::JsonStreamWriter::appendValue ( const QString & value)

writes String value to JSON text

Warning
this function does not escapes text correctly, use with caution

Definition at line 106 of file jsonstreamwriter.cpp.

107{
108 *this << "\"" << value << "\"";
109 m_nextIsValue = false;
110}

References m_nextIsValue.

◆ appendValue() [2/2]

void pappso::cbor::JsonStreamWriter::appendValue ( std::size_t integer_size_value)

writes an integer value to JSON text

Definition at line 113 of file jsonstreamwriter.cpp.

114{
115 *this << integer_size_value;
116 m_nextIsValue = false;
117}

References m_nextIsValue.

◆ comma()

void pappso::cbor::JsonStreamWriter::comma ( )
protected

Definition at line 127 of file jsonstreamwriter.cpp.

128{
129 *this << "," << Qt::endl;
130 m_isStart = false;
131}

References m_isStart.

Referenced by appendKey(), startArray(), and startMap().

◆ endArray()

void pappso::cbor::JsonStreamWriter::endArray ( )

ends an array in JSON output simply print "]"

Definition at line 44 of file jsonstreamwriter.cpp.

45{
46 *this << "]" << Qt::endl;
47 m_isStart = false;
48 m_nextIsValue = false;
49}

References m_isStart, and m_nextIsValue.

◆ endMap()

void pappso::cbor::JsonStreamWriter::endMap ( )

ends an object in JSON output simply print "}"

Definition at line 52 of file jsonstreamwriter.cpp.

53{
54 *this << "}" << Qt::endl;
55 m_isStart = false;
56 m_nextIsValue = false;
57}

References m_isStart, and m_nextIsValue.

◆ startArray()

void pappso::cbor::JsonStreamWriter::startArray ( )

starts an array in JSON output simply print "["

Definition at line 60 of file jsonstreamwriter.cpp.

61{
62 if(!m_nextIsValue)
63 comma();
64 *this << Qt::endl << "[";
65 m_isStart = true;
66}

References comma(), m_isStart, and m_nextIsValue.

◆ startMap()

void pappso::cbor::JsonStreamWriter::startMap ( )

start an object in JSON output simply print "{"

Definition at line 68 of file jsonstreamwriter.cpp.

69{
70 if(!m_nextIsValue)
71 comma();
72 *this << Qt::endl << "{";
73 m_isStart = true;
74}

References comma(), m_isStart, and m_nextIsValue.

◆ writeArray()

void pappso::cbor::JsonStreamWriter::writeArray ( QStringList & str_list)

convert a string list to a JSON array

Warning
text is not escaped correctly, use with caution

Definition at line 121 of file jsonstreamwriter.cpp.

122{
123 *this << "[\"" << str_list.join("\", \"") << "\"]" << Qt::endl;
124}

◆ writeCborArray()

void pappso::cbor::JsonStreamWriter::writeCborArray ( const QCborArray & cbor_array)

converts a CBOR array (list) into JSON text in output

Parameters
cbor_arraythe map to convert

Definition at line 86 of file jsonstreamwriter.cpp.

87{
88 QJsonDocument doc;
89 doc.setArray(QCborValue(cbor_array).toJsonValue().toArray());
90
91 *this << doc.toJson();
92}

◆ writeCborMap()

void pappso::cbor::JsonStreamWriter::writeCborMap ( const QCborMap & cbor_map)

converts a CBOR map (object) into JSON text in output

Parameters
cbor_mapthe map to convert

Definition at line 77 of file jsonstreamwriter.cpp.

78{
79 QJsonDocument doc;
80 doc.setObject(QCborValue(cbor_map).toJsonValue().toObject());
81
82 *this << doc.toJson();
83}

Member Data Documentation

◆ m_isStart

bool pappso::cbor::JsonStreamWriter::m_isStart = true
private

Definition at line 114 of file jsonstreamwriter.h.

Referenced by appendKey(), comma(), endArray(), endMap(), startArray(), and startMap().

◆ m_nextIsValue

bool pappso::cbor::JsonStreamWriter::m_nextIsValue = false
private

The documentation for this class was generated from the following files: