KiCad PCB EDA Suite
Loading...
Searching...
No Matches
json_serializers.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef JSON_SERIALIZERS_H_
21#define JSON_SERIALIZERS_H_
22
23#include <json_common.h>
24#include <wx/string.h>
25#include <optional>
26
27NLOHMANN_JSON_NAMESPACE_BEGIN
28namespace detail
29{
30// silly hack to silence warnings about std::char_traits<wxUniChar> not being supported by the standard
31// nolhmann json internally has its own char_traits templated to support some exotic char traits
32template <>
33struct char_traits<wxUniChar> : std::char_traits<char>
34{
35 using char_type = wxUniChar;
36 using int_type = uint32_t; // this is wxwidget's internal data type
37
38 // Redefine to_int_type function
39 static int_type to_int_type( char_type c ) noexcept { return static_cast<int_type>( c ); }
40
41 static char_type to_char_type( int_type i ) noexcept { return static_cast<char_type>( i ); }
42
43 static constexpr int_type eof() noexcept { return static_cast<int_type>( EOF ); }
44};
45} // namespace detail
46NLOHMANN_JSON_NAMESPACE_END
47
48namespace nlohmann
49{
50template <>
51struct adl_serializer<wxString>
52{
53 static void from_json( const json& j, wxString& s )
54 {
55 s = wxString::FromUTF8( j.get<std::string>().c_str() );
56 }
57
58 static void to_json( json& j, const wxString& s ) { j = s.ToUTF8(); }
59};
60
61template <typename T>
62struct adl_serializer<std::optional<T>>
63{
64 static void from_json( const json& j, std::optional<T>& opt )
65 {
66 if( j.is_null() )
67 {
68 opt = std::nullopt;
69 }
70 else
71 {
72 opt = j.template get<T>();
73 }
74 }
75
76 static void to_json( json& j, const std::optional<T>& opt )
77 {
78 if( opt.has_value() )
79 {
80 j = *opt;
81 }
82 else
83 {
84 j = nullptr;
85 }
86 }
87};
88} // namespace nlohmann
89
90
91#endif // JSON_SERIALIZERS_H_
STL namespace.
static char_type to_char_type(int_type i) noexcept
static int_type to_int_type(char_type c) noexcept
static constexpr int_type eof() noexcept
static void to_json(json &j, const std::optional< T > &opt)
static void from_json(const json &j, std::optional< T > &opt)
static void from_json(const json &j, wxString &s)
static void to_json(json &j, const wxString &s)