diff --git a/server/src/core/property.hpp b/server/src/core/property.hpp index 07732dcd..256fd7bc 100644 --- a/server/src/core/property.hpp +++ b/server/src/core/property.hpp @@ -160,7 +160,7 @@ class Property : public AbstractProperty nlohmann::json toJSON() const final { - return m_value; + return to(m_value); } void fromBool(bool value) final @@ -191,30 +191,6 @@ class Property : public AbstractProperty void load(const nlohmann::json& value) final { m_value = to(value); - /* - switch(value.type()) - { - case nlohmann::json::value_t::boolean: - fromBool(value); - break; - - case nlohmann::json::value_t::number_integer: - case nlohmann::json::value_t::number_unsigned: - fromInt64(value); - break; - - case nlohmann::json::value_t::number_float: - fromDouble(value); - break; - - case nlohmann::json::value_t::string: - fromString(value); - break; - - default: - throw std::runtime_error("unsupported JSON type"); - } - */ } void load(const ObjectPtr& value) final diff --git a/server/src/core/settings.cpp b/server/src/core/settings.cpp index bfafa1d8..5f1cd524 100644 --- a/server/src/core/settings.cpp +++ b/server/src/core/settings.cpp @@ -22,7 +22,6 @@ #include "settings.hpp" #include -#include #include "traintastic.hpp" using nlohmann::json; diff --git a/server/src/core/to.hpp b/server/src/core/to.hpp index 230583d3..6ec8e9fa 100644 --- a/server/src/core/to.hpp +++ b/server/src/core/to.hpp @@ -110,6 +110,17 @@ To to(const From& value) else return value; } + else if constexpr(std::is_same_v) + { + if constexpr(std::is_enum_v) + { + To json; + to_json(json, value); + return json; + } + else + return value; + } throw conversion_error(); } diff --git a/server/src/core/traintastic.cpp b/server/src/core/traintastic.cpp index 0836cd6b..d2bf4c0c 100644 --- a/server/src/core/traintastic.cpp +++ b/server/src/core/traintastic.cpp @@ -22,7 +22,6 @@ #include "traintastic.hpp" #include -#include #include #include #include diff --git a/server/src/core/unitproperty.hpp b/server/src/core/unitproperty.hpp index 76dd33db..1386c28c 100644 --- a/server/src/core/unitproperty.hpp +++ b/server/src/core/unitproperty.hpp @@ -132,8 +132,8 @@ class UnitProperty : public AbstractProperty//InterfaceItem nlohmann::json toJSON() const final { nlohmann::json v; - v["value"] = m_value; - v["unit"] = m_unit; + v["value"] = to(m_value); + v["unit"] = to(m_unit); return v;//{"value" = m_value, "unit" = m_unit}; } diff --git a/server/src/utils/json.hpp b/server/src/utils/json.hpp index c9d82e95..e372dc08 100644 --- a/server/src/utils/json.hpp +++ b/server/src/utils/json.hpp @@ -28,9 +28,9 @@ #include template -inline void to_json(const BasicJsonType& j, const EnumType& e, typename std::enable_if_t && !is_set_v>* = nullptr) +inline void to_json(BasicJsonType& j, EnumType e, typename std::enable_if_t && !is_set_v>* = nullptr) { - e = EnumValues::value.at(e); + j = EnumValues::value.at(e); } template @@ -46,7 +46,7 @@ inline void from_json(const BasicJsonType& j, EnumType& e, typename std::enable_ } template -inline void to_json(const BasicJsonType& j, const EnumType& e, typename std::enable_if_t>* = nullptr) +inline void to_json(BasicJsonType& j, const EnumType& e, typename std::enable_if_t>* = nullptr) { assert(false); } diff --git a/server/src/utils/jsonutils.cpp b/server/src/utils/jsonutils.cpp deleted file mode 100644 index 970e20b4..00000000 --- a/server/src/utils/jsonutils.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/** - * server/src/utils/jsonutils.cpp - * - * This file is part of the traintastic source code. - * - * Copyright (C) 2019-2020 Reinder Feenstra - * - * This program 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 2 - * of the License, or (at your option) any later version. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#include "jsonutils.hpp" -#include "../core/abstractproperty.hpp" -/* -void JSONUtils::loadPropertyValue(AbstractProperty& property, const nlohmann::json& value) -{ - - -}*/ diff --git a/server/src/utils/jsonutils.hpp b/server/src/utils/jsonutils.hpp deleted file mode 100644 index 7be7a437..00000000 --- a/server/src/utils/jsonutils.hpp +++ /dev/null @@ -1,35 +0,0 @@ -/** - * server/src/utils/jsonutils.hpp - * - * This file is part of the traintastic source code. - * - * Copyright (C) 2019-2020 Reinder Feenstra - * - * This program 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 2 - * of the License, or (at your option) any later version. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#ifndef TRAINTASTIC_SERVER_UTILS_JSONUTILS_HPP -#define TRAINTASTIC_SERVER_UTILS_JSONUTILS_HPP - -#include - -class AbstractProperty; - -struct JSONUtils -{ - //void loadPropertyValue(AbstractProperty& property, const nlohmann::json& value) -}; - -#endif diff --git a/server/src/world/world.hpp b/server/src/world/world.hpp index fbe4c57b..20e31531 100644 --- a/server/src/world/world.hpp +++ b/server/src/world/world.hpp @@ -29,7 +29,6 @@ #include "../core/stdfilesystem.hpp" #include #include -#include #include #include "../enum/worldscale.hpp" #include diff --git a/server/src/world/worldlist.cpp b/server/src/world/worldlist.cpp index 06e1b614..0f7f6d05 100644 --- a/server/src/world/worldlist.cpp +++ b/server/src/world/worldlist.cpp @@ -22,7 +22,6 @@ #include "worldlist.hpp" #include -#include #include #include "../core/traintastic.hpp" #include "worldlisttablemodel.hpp" diff --git a/server/src/world/worldloader.hpp b/server/src/world/worldloader.hpp index 0146b469..3fd21624 100644 --- a/server/src/world/worldloader.hpp +++ b/server/src/world/worldloader.hpp @@ -26,9 +26,9 @@ #include #include #include -#include #include "../core/stdfilesystem.hpp" #include "../core/objectptr.hpp" +#include "../utils/json.hpp" class Object; class World; diff --git a/server/src/world/worldsaver.cpp b/server/src/world/worldsaver.cpp index 23642c99..8662889e 100644 --- a/server/src/world/worldsaver.cpp +++ b/server/src/world/worldsaver.cpp @@ -36,8 +36,8 @@ WorldSaver::WorldSaver(const World& world) json data; data["uuid"] = to_string(world.m_uuid); - data[world.name.name()] = world.name.value(); - data[world.scale.name()] = world.scale.value(); + data[world.name.name()] = world.name.toJSON(); + data[world.scale.name()] = world.scale.toJSON(); data["objects"] = objects; std::filesystem::path dir = std::filesystem::path(world.m_filename).remove_filename(); diff --git a/server/src/world/worldsaver.hpp b/server/src/world/worldsaver.hpp index c0fdaf51..52805b54 100644 --- a/server/src/world/worldsaver.hpp +++ b/server/src/world/worldsaver.hpp @@ -23,8 +23,8 @@ #ifndef TRAINTASTIC_SERVER_WORLD_WORLDSAVER_HPP #define TRAINTASTIC_SERVER_WORLD_WORLDSAVER_HPP -#include #include "../core/objectptr.hpp" +#include "../utils/json.hpp" class World;