Move wxString and std::optional JSON serializers to core header.

This commit is contained in:
Alex Shvartzkop 2023-09-06 14:55:09 +03:00
parent 5b438d6398
commit 05a8e59884
4 changed files with 78 additions and 46 deletions

View File

@ -43,6 +43,7 @@
#include <fstream>
#include <map>
#include <nlohmann/json.hpp>
#include <core/json_serializers.h>
#include <optional>
#include <string>
#include <tuple>
@ -50,22 +51,6 @@
#include <wx/string.h>
// Teaching json en/decoder to understand wxStrings
namespace nlohmann
{
template <>
struct adl_serializer<wxString>
{
static void to_json( json& j, const wxString& s ) { j = s.ToUTF8(); }
static void from_json( const json& j, wxString& s )
{
s = wxString::FromUTF8( j.get<std::string>().c_str() );
}
};
} // namespace nlohmann
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( NOTIFICATION, title, description, href, key, date )
class NOTIFICATION_PANEL : public wxPanel

View File

@ -20,22 +20,9 @@
#include <settings/bom_settings.h>
#include <nlohmann/json.hpp>
#include <core/json_serializers.h>
#include <wx/translation.h>
namespace nlohmann
{
template <>
struct adl_serializer<wxString>
{
static void to_json( json& j, const wxString& s ) { j = s.ToUTF8(); }
static void from_json( const json& j, wxString& s )
{
s = wxString::FromUTF8( j.get<std::string>().c_str() );
}
};
}
// Implementations for BOM_FMT_PRESET
bool BOM_FIELD::operator==( const BOM_FIELD& rhs ) const

View File

@ -25,6 +25,7 @@
#include <map>
#include <nlohmann/json.hpp>
#include <core/json_serializers.h>
#include <optional>
#include <string>
#include <tuple>
@ -148,22 +149,6 @@ struct PCM_INSTALLATION_ENTRY
};
// Teaching json en/decoder to understand wxStrings
namespace nlohmann
{
template <>
struct adl_serializer<wxString>
{
static void to_json( json& j, const wxString& s ) { j = s.ToUTF8(); }
static void from_json( const json& j, wxString& s )
{
s = wxString::FromUTF8( j.get<std::string>().c_str() );
}
};
} // namespace nlohmann
NLOHMANN_JSON_SERIALIZE_ENUM( PCM_PACKAGE_TYPE, {
{ PT_INVALID, "invalid" },
{ PT_PLUGIN, "plugin" },

View File

@ -0,0 +1,75 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef JSON_SERIALIZERS_H_
#define JSON_SERIALIZERS_H_
#include <nlohmann/json.hpp>
#include <wx/string.h>
#include <optional>
namespace nlohmann
{
template <>
struct adl_serializer<wxString>
{
static void from_json( const json& j, wxString& s )
{
s = wxString::FromUTF8( j.get<std::string>().c_str() );
}
static void to_json( json& j, const wxString& s ) { j = s.ToUTF8(); }
};
template <typename T>
struct adl_serializer<std::optional<T>>
{
static void from_json( const json& j, std::optional<T>& opt )
{
if( j.is_null() )
{
opt = std::nullopt;
}
else
{
opt = j.template get<T>();
}
}
static void to_json( json& j, const std::optional<T>& opt )
{
if( opt.has_value() )
{
j = *opt;
}
else
{
j = nullptr;
}
}
};
} // namespace nlohmann
#endif // JSON_SERIALIZERS_H_