Add JsonWriter

This commit is contained in:
Gary Talent 2017-05-13 02:08:49 -05:00
parent e79e3756c5
commit de35f76917
7 changed files with 131 additions and 34 deletions

View File

@ -32,6 +32,7 @@ target_link_libraries(
install( install(
FILES FILES
json.hpp json.hpp
json_err.hpp
json_read.hpp json_read.hpp
json_write.hpp json_write.hpp
newwizard.hpp newwizard.hpp

View File

@ -0,0 +1,20 @@
/*
* Copyright 2016-2017 gtalent2@gmail.com
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
namespace nostalgia {
namespace studio {
enum {
JSON_ERR_FIELD_MISSING = 1,
JSON_ERR_UNEXPECTED_TYPE = 2,
};
}
}

View File

@ -11,16 +11,12 @@
#include <QJsonArray> #include <QJsonArray>
#include <QJsonObject> #include <QJsonObject>
#include <QJsonDocument> #include <QJsonDocument>
#include "json_err.hpp"
#include "json_operator.hpp" #include "json_operator.hpp"
namespace nostalgia { namespace nostalgia {
namespace studio { namespace studio {
enum {
JSON_ERR_FIELD_MISSING,
JSON_ERR_UNEXPECTED_TYPE,
};
class JsonReader { class JsonReader {
private: private:
@ -73,7 +69,7 @@ ox::Error JsonReader::op(QString fieldName, QVector<T> *dest) {
}; };
template<typename T> template<typename T>
int read(QString json, T *dest) { int readJson(QString json, T *dest) {
auto obj = QJsonDocument::fromJson(json.toUtf8()).object(); auto obj = QJsonDocument::fromJson(json.toUtf8()).object();
JsonReader rdr(obj); JsonReader rdr(obj);
return ioOp(&rdr, dest); return ioOp(&rdr, dest);

View File

@ -51,36 +51,37 @@ int ioOp(T *io, TestStruct *obj) {
int main(int argc, char **args) { int main(int argc, char **args) {
int err = 0; int err = 0;
auto json = QString json;
"{" TestStruct ts = {
" \"Bool\": true," .Bool = true,
" \"Int\": 42," .Int = 42,
" \"Double\": 42.42," .Double = 42.42,
" \"String\": \"Test String\"," .String = "Test String",
" \"Struct\": {" .Struct = {
" \"Bool\": true," .Bool = true,
" \"Int\": 42," .Int = 42,
" \"Double\": 42.42," .Double = 42.42,
" \"String\": \"Test String\"" .String = "Test String"
" }" }
"}"; };
TestStruct ts; TestStruct tsOut;
read(json, &ts); err |= writeJson(&json, &ts);
err |= readJson(json, &tsOut);
cout << ts.Bool << endl; cout << tsOut.Bool << endl;
cout << ts.Int << endl; cout << tsOut.Int << endl;
cout << ts.Double << endl; cout << tsOut.Double << endl;
cout << ts.String.toStdString() << endl; cout << tsOut.String.toStdString() << endl;
err |= !(ts.Bool) << 0; err |= !(tsOut.Bool) << 0;
err |= !(ts.Int == 42) << 1; err |= !(tsOut.Int == 42) << 1;
err |= !(ts.Double == 42.42) << 2; err |= !(tsOut.Double == 42.42) << 2;
err |= !(ts.String == "Test String") << 3; err |= !(tsOut.String == "Test String") << 3;
err |= !(ts.Struct.Bool) << 4; err |= !(tsOut.Struct.Bool) << 4;
err |= !(ts.Struct.Int == 42) << 5; err |= !(tsOut.Struct.Int == 42) << 5;
err |= !(ts.Struct.Double == 42.42) << 6; err |= !(tsOut.Struct.Double == 42.42) << 6;
err |= !(ts.Struct.String == "Test String") << 7; err |= !(tsOut.Struct.String == "Test String") << 7;
return err; return err;
} }

View File

@ -11,6 +11,28 @@
namespace nostalgia { namespace nostalgia {
namespace studio { namespace studio {
JsonWriter::JsonWriter(QJsonObject &obj): m_dest(obj) {
}
ox::Error JsonWriter::op(QString fieldName, int *src) {
m_dest[fieldName] = *src;
return 0;
}
ox::Error JsonWriter::op(QString fieldName, bool *src) {
m_dest[fieldName] = *src;
return 0;
}
ox::Error JsonWriter::op(QString fieldName, double *src) {
m_dest[fieldName] = *src;
return 0;
}
ox::Error JsonWriter::op(QString fieldName, QString *src) {
m_dest[fieldName] = *src;
return 0;
}
} }
} }

View File

@ -8,9 +8,66 @@
#pragma once #pragma once
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include "json_err.hpp"
#include "json_operator.hpp"
namespace nostalgia { namespace nostalgia {
namespace studio { namespace studio {
class JsonWriter {
private:
QJsonObject &m_dest;
public:
JsonWriter(QJsonObject &obj);
ox::Error op(QString fieldName, int *src);
ox::Error op(QString fieldName, bool *src);
ox::Error op(QString fieldName, double *src);
ox::Error op(QString fieldName, QString *src);
template<typename T>
ox::Error op(QString fieldName, T *src);
template<typename T>
ox::Error op(QString fieldName, QVector<T> *src);
};
template<typename T>
ox::Error JsonWriter::op(QString fieldName, T *src) {
auto obj = QJsonObject();
auto reader = JsonWriter(obj);
auto err = ioOp(&reader, src);
m_dest[fieldName] = obj;
return err;
};
template<typename T>
ox::Error JsonWriter::op(QString fieldName, QVector<T> *src) {
ox::Error err = 0;
auto &a = m_dest[fieldName] = QJsonArray();
for (int i = 0; i < src->size(); i++) {
err |= op(a[i], &src->at(i));
}
return err;
};
template<typename T>
int writeJson(QString *json, T *src) {
auto obj = QJsonObject();
JsonWriter rdr(obj);
auto err = ioOp(&rdr, src);
*json = QJsonDocument(obj).toJson();
return err;
}
} }
} }

View File

@ -27,7 +27,7 @@ int run(int argc, char **args) {
if (file.exists()) { if (file.exists()) {
file.open(QIODevice::ReadOnly); file.open(QIODevice::ReadOnly);
QTextStream in(&file); QTextStream in(&file);
read(in.readAll(), &config); readJson(in.readAll(), &config);
} }
QApplication app(argc, args); QApplication app(argc, args);