From de35f769175b2449f78312a657098d9f5a0eb5fc Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 13 May 2017 02:08:49 -0500 Subject: [PATCH] Add JsonWriter --- src/nostalgia/studio/lib/CMakeLists.txt | 1 + src/nostalgia/studio/lib/json_err.hpp | 20 +++++++++ src/nostalgia/studio/lib/json_read.hpp | 8 +--- src/nostalgia/studio/lib/json_test.cpp | 55 ++++++++++++------------ src/nostalgia/studio/lib/json_write.cpp | 22 ++++++++++ src/nostalgia/studio/lib/json_write.hpp | 57 +++++++++++++++++++++++++ src/nostalgia/studio/main.cpp | 2 +- 7 files changed, 131 insertions(+), 34 deletions(-) create mode 100644 src/nostalgia/studio/lib/json_err.hpp diff --git a/src/nostalgia/studio/lib/CMakeLists.txt b/src/nostalgia/studio/lib/CMakeLists.txt index df190e27..5f03e42f 100644 --- a/src/nostalgia/studio/lib/CMakeLists.txt +++ b/src/nostalgia/studio/lib/CMakeLists.txt @@ -32,6 +32,7 @@ target_link_libraries( install( FILES json.hpp + json_err.hpp json_read.hpp json_write.hpp newwizard.hpp diff --git a/src/nostalgia/studio/lib/json_err.hpp b/src/nostalgia/studio/lib/json_err.hpp new file mode 100644 index 00000000..083a5759 --- /dev/null +++ b/src/nostalgia/studio/lib/json_err.hpp @@ -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, +}; + +} +} diff --git a/src/nostalgia/studio/lib/json_read.hpp b/src/nostalgia/studio/lib/json_read.hpp index 892b7808..5208e5ea 100644 --- a/src/nostalgia/studio/lib/json_read.hpp +++ b/src/nostalgia/studio/lib/json_read.hpp @@ -11,16 +11,12 @@ #include #include #include +#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 *dest) { }; template -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); diff --git a/src/nostalgia/studio/lib/json_test.cpp b/src/nostalgia/studio/lib/json_test.cpp index 1c3d2f6b..1fa3b56e 100644 --- a/src/nostalgia/studio/lib/json_test.cpp +++ b/src/nostalgia/studio/lib/json_test.cpp @@ -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; } diff --git a/src/nostalgia/studio/lib/json_write.cpp b/src/nostalgia/studio/lib/json_write.cpp index 5ab43270..73117a5d 100644 --- a/src/nostalgia/studio/lib/json_write.cpp +++ b/src/nostalgia/studio/lib/json_write.cpp @@ -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; +} } } diff --git a/src/nostalgia/studio/lib/json_write.hpp b/src/nostalgia/studio/lib/json_write.hpp index 20b56572..c56138da 100644 --- a/src/nostalgia/studio/lib/json_write.hpp +++ b/src/nostalgia/studio/lib/json_write.hpp @@ -8,9 +8,66 @@ #pragma once +#include +#include +#include +#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 + ox::Error op(QString fieldName, T *src); + + template + ox::Error op(QString fieldName, QVector *src); + +}; + +template +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 +ox::Error JsonWriter::op(QString fieldName, QVector *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 +int writeJson(QString *json, T *src) { + auto obj = QJsonObject(); + JsonWriter rdr(obj); + auto err = ioOp(&rdr, src); + *json = QJsonDocument(obj).toJson(); + return err; +} } } diff --git a/src/nostalgia/studio/main.cpp b/src/nostalgia/studio/main.cpp index e9db9f62..3ab89aeb 100644 --- a/src/nostalgia/studio/main.cpp +++ b/src/nostalgia/studio/main.cpp @@ -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);