Add JsonWriter
This commit is contained in:
parent
e79e3756c5
commit
de35f76917
@ -32,6 +32,7 @@ target_link_libraries(
|
||||
install(
|
||||
FILES
|
||||
json.hpp
|
||||
json_err.hpp
|
||||
json_read.hpp
|
||||
json_write.hpp
|
||||
newwizard.hpp
|
||||
|
20
src/nostalgia/studio/lib/json_err.hpp
Normal file
20
src/nostalgia/studio/lib/json_err.hpp
Normal 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,
|
||||
};
|
||||
|
||||
}
|
||||
}
|
@ -11,16 +11,12 @@
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include "json_err.hpp"
|
||||
#include "json_operator.hpp"
|
||||
|
||||
namespace nostalgia {
|
||||
namespace studio {
|
||||
|
||||
enum {
|
||||
JSON_ERR_FIELD_MISSING,
|
||||
JSON_ERR_UNEXPECTED_TYPE,
|
||||
};
|
||||
|
||||
class JsonReader {
|
||||
|
||||
private:
|
||||
@ -73,7 +69,7 @@ ox::Error JsonReader::op(QString fieldName, QVector<T> *dest) {
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
int read(QString json, T *dest) {
|
||||
int readJson(QString json, T *dest) {
|
||||
auto obj = QJsonDocument::fromJson(json.toUtf8()).object();
|
||||
JsonReader rdr(obj);
|
||||
return ioOp(&rdr, dest);
|
||||
|
@ -51,36 +51,37 @@ int ioOp(T *io, TestStruct *obj) {
|
||||
|
||||
int main(int argc, char **args) {
|
||||
int err = 0;
|
||||
auto json =
|
||||
"{"
|
||||
" \"Bool\": true,"
|
||||
" \"Int\": 42,"
|
||||
" \"Double\": 42.42,"
|
||||
" \"String\": \"Test String\","
|
||||
" \"Struct\": {"
|
||||
" \"Bool\": true,"
|
||||
" \"Int\": 42,"
|
||||
" \"Double\": 42.42,"
|
||||
" \"String\": \"Test String\""
|
||||
" }"
|
||||
"}";
|
||||
TestStruct ts;
|
||||
read(json, &ts);
|
||||
QString json;
|
||||
TestStruct ts = {
|
||||
.Bool = true,
|
||||
.Int = 42,
|
||||
.Double = 42.42,
|
||||
.String = "Test String",
|
||||
.Struct = {
|
||||
.Bool = true,
|
||||
.Int = 42,
|
||||
.Double = 42.42,
|
||||
.String = "Test String"
|
||||
}
|
||||
};
|
||||
TestStruct tsOut;
|
||||
err |= writeJson(&json, &ts);
|
||||
err |= readJson(json, &tsOut);
|
||||
|
||||
cout << ts.Bool << endl;
|
||||
cout << ts.Int << endl;
|
||||
cout << ts.Double << endl;
|
||||
cout << ts.String.toStdString() << endl;
|
||||
cout << tsOut.Bool << endl;
|
||||
cout << tsOut.Int << endl;
|
||||
cout << tsOut.Double << endl;
|
||||
cout << tsOut.String.toStdString() << endl;
|
||||
|
||||
err |= !(ts.Bool) << 0;
|
||||
err |= !(ts.Int == 42) << 1;
|
||||
err |= !(ts.Double == 42.42) << 2;
|
||||
err |= !(ts.String == "Test String") << 3;
|
||||
err |= !(tsOut.Bool) << 0;
|
||||
err |= !(tsOut.Int == 42) << 1;
|
||||
err |= !(tsOut.Double == 42.42) << 2;
|
||||
err |= !(tsOut.String == "Test String") << 3;
|
||||
|
||||
err |= !(ts.Struct.Bool) << 4;
|
||||
err |= !(ts.Struct.Int == 42) << 5;
|
||||
err |= !(ts.Struct.Double == 42.42) << 6;
|
||||
err |= !(ts.Struct.String == "Test String") << 7;
|
||||
err |= !(tsOut.Struct.Bool) << 4;
|
||||
err |= !(tsOut.Struct.Int == 42) << 5;
|
||||
err |= !(tsOut.Struct.Double == 42.42) << 6;
|
||||
err |= !(tsOut.Struct.String == "Test String") << 7;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
@ -11,6 +11,28 @@
|
||||
namespace nostalgia {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,66 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include "json_err.hpp"
|
||||
#include "json_operator.hpp"
|
||||
|
||||
namespace nostalgia {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ int run(int argc, char **args) {
|
||||
if (file.exists()) {
|
||||
file.open(QIODevice::ReadOnly);
|
||||
QTextStream in(&file);
|
||||
read(in.readAll(), &config);
|
||||
readJson(in.readAll(), &config);
|
||||
}
|
||||
|
||||
QApplication app(argc, args);
|
||||
|
Loading…
Reference in New Issue
Block a user