fixed enum to/from json conversion

Dieser Commit ist enthalten in:
Reinder Feenstra 2020-08-18 00:01:11 +02:00
Ursprung 1d8cdee29f
Commit 3db602d789
13 geänderte Dateien mit 21 neuen und 103 gelöschten Zeilen

Datei anzeigen

@ -160,7 +160,7 @@ class Property : public AbstractProperty
nlohmann::json toJSON() const final
{
return m_value;
return to<nlohmann::json>(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<T>(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

Datei anzeigen

@ -22,7 +22,6 @@
#include "settings.hpp"
#include <fstream>
#include <nlohmann/json.hpp>
#include "traintastic.hpp"
using nlohmann::json;

Datei anzeigen

@ -110,6 +110,17 @@ To to(const From& value)
else
return value;
}
else if constexpr(std::is_same_v<To, nlohmann::json>)
{
if constexpr(std::is_enum_v<From>)
{
To json;
to_json(json, value);
return json;
}
else
return value;
}
throw conversion_error();
}

Datei anzeigen

@ -22,7 +22,6 @@
#include "traintastic.hpp"
#include <fstream>
#include <nlohmann/json.hpp>
#include <boost/uuid/nil_generator.hpp>
#include <boost/uuid/string_generator.hpp>
#include <boost/uuid/uuid_io.hpp>

Datei anzeigen

@ -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<nlohmann::json>(m_value);
v["unit"] = to<nlohmann::json>(m_unit);
return v;//{"value" = m_value, "unit" = m_unit};
}

Datei anzeigen

@ -28,9 +28,9 @@
#include <traintastic/set/set.hpp>
template<typename BasicJsonType, typename EnumType>
inline void to_json(const BasicJsonType& j, const EnumType& e, typename std::enable_if_t<std::is_enum_v<EnumType> && !is_set_v<EnumType>>* = nullptr)
inline void to_json(BasicJsonType& j, EnumType e, typename std::enable_if_t<std::is_enum_v<EnumType> && !is_set_v<EnumType>>* = nullptr)
{
e = EnumValues<EnumType>::value.at(e);
j = EnumValues<EnumType>::value.at(e);
}
template<typename BasicJsonType, typename EnumType>
@ -46,7 +46,7 @@ inline void from_json(const BasicJsonType& j, EnumType& e, typename std::enable_
}
template<typename BasicJsonType, typename EnumType>
inline void to_json(const BasicJsonType& j, const EnumType& e, typename std::enable_if_t<is_set_v<EnumType>>* = nullptr)
inline void to_json(BasicJsonType& j, const EnumType& e, typename std::enable_if_t<is_set_v<EnumType>>* = nullptr)
{
assert(false);
}

Datei anzeigen

@ -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)
{
}*/

Datei anzeigen

@ -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 <nlohmann/json.hpp>
class AbstractProperty;
struct JSONUtils
{
//void loadPropertyValue(AbstractProperty& property, const nlohmann::json& value)
};
#endif

Datei anzeigen

@ -29,7 +29,6 @@
#include "../core/stdfilesystem.hpp"
#include <unordered_map>
#include <boost/uuid/uuid.hpp>
#include <nlohmann/json.hpp>
#include <traintastic/enum/worldevent.hpp>
#include "../enum/worldscale.hpp"
#include <traintastic/set/worldstate.hpp>

Datei anzeigen

@ -22,7 +22,6 @@
#include "worldlist.hpp"
#include <fstream>
#include <nlohmann/json.hpp>
#include <boost/uuid/string_generator.hpp>
#include "../core/traintastic.hpp"
#include "worldlisttablemodel.hpp"

Datei anzeigen

@ -26,9 +26,9 @@
#include <memory>
#include <string>
#include <unordered_map>
#include <nlohmann/json.hpp>
#include "../core/stdfilesystem.hpp"
#include "../core/objectptr.hpp"
#include "../utils/json.hpp"
class Object;
class World;

Datei anzeigen

@ -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();

Datei anzeigen

@ -23,8 +23,8 @@
#ifndef TRAINTASTIC_SERVER_WORLD_WORLDSAVER_HPP
#define TRAINTASTIC_SERVER_WORLD_WORLDSAVER_HPP
#include <nlohmann/json.hpp>
#include "../core/objectptr.hpp"
#include "../utils/json.hpp"
class World;