Debian Codegraph @

debian/patches/96c1b52f1cc4742a00ee02b34e043f61bbffa6ae.patch

raw
1 From 96c1b52f1cc4742a00ee02b34e043f61bbffa6ae Mon Sep 17 00:00:00 2001
2 From: Niels Lohmann <[email protected]>
3 Date: Tue, 15 Apr 2025 16:10:39 +0200
4 Subject: [PATCH] Fix conversion to std::optional (#4742)
5
6 * :alembic: remove JSON_USE_IMPLICIT_CONVERSIONS check for from_json(json, std::optional)
7
8 Signed-off-by: Niels Lohmann <[email protected]>
9
10 * :alembic: remove JSON_USE_IMPLICIT_CONVERSIONS check for from_json(json, std::optional)
11
12 Signed-off-by: Niels Lohmann <[email protected]>
13
14 * :alembic: remove JSON_USE_IMPLICIT_CONVERSIONS check for from_json(json, std::optional)
15
16 Signed-off-by: Niels Lohmann <[email protected]>
17
18 * :alembic: remove JSON_USE_IMPLICIT_CONVERSIONS check for from_json(json, std::optional)
19
20 Signed-off-by: Niels Lohmann <[email protected]>
21
22 * :alembic: remove JSON_USE_IMPLICIT_CONVERSIONS check for from_json(json, std::optional)
23
24 Signed-off-by: Niels Lohmann <[email protected]>
25
26 * :alembic: add C++ standard library as matrix option
27
28 Signed-off-by: Niels Lohmann <[email protected]>
29
30 * :rewind: remove inline
31
32 Signed-off-by: Niels Lohmann <[email protected]>
33
34 ---------
35
36 Signed-off-by: Niels Lohmann <[email protected]>
37 ---
38 .../nlohmann/detail/conversions/from_json.hpp | 11 +++--
39 .../nlohmann/detail/conversions/to_json.hpp | 2 +-
40 single_include/nlohmann/json.hpp | 13 +++---
41 tests/src/unit-regression2.cpp | 43 +++++++++++++++++++
42 4 files changed, 55 insertions(+), 14 deletions(-)
43
44 diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp
45 index d647d74239..797f714dfa 100644
46 --- a/include/nlohmann/detail/conversions/from_json.hpp
47 +++ b/include/nlohmann/detail/conversions/from_json.hpp
48 @@ -13,9 +13,6 @@
49 #include <forward_list> // forward_list
50 #include <iterator> // inserter, front_inserter, end
51 #include <map> // map
52 -#ifdef JSON_HAS_CPP_17
53 - #include <optional> // optional
54 -#endif
55 #include <string> // string
56 #include <tuple> // tuple, make_tuple
57 #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
58 @@ -32,6 +29,11 @@
59 #include <nlohmann/detail/string_concat.hpp>
60 #include <nlohmann/detail/value_t.hpp>
61
62 +// include after macro_scope.hpp
63 +#ifdef JSON_HAS_CPP_17
64 + #include <optional> // optional
65 +#endif
66 +
67 NLOHMANN_JSON_NAMESPACE_BEGIN
68 namespace detail
69 {
70 @@ -47,7 +49,6 @@ inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
71 }
72
73 #ifdef JSON_HAS_CPP_17
74 -#ifndef JSON_USE_IMPLICIT_CONVERSIONS
75 template<typename BasicJsonType, typename T>
76 void from_json(const BasicJsonType& j, std::optional<T>& opt)
77 {
78 @@ -60,8 +61,6 @@ void from_json(const BasicJsonType& j, std::optional<T>& opt)
79 opt.emplace(j.template get<T>());
80 }
81 }
82 -
83 -#endif // JSON_USE_IMPLICIT_CONVERSIONS
84 #endif // JSON_HAS_CPP_17
85
86 // overloads for basic_json template parameters
87 diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp
88 index ead45665f1..f8413850d5 100644
89 --- a/include/nlohmann/detail/conversions/to_json.hpp
90 +++ b/include/nlohmann/detail/conversions/to_json.hpp
91 @@ -267,7 +267,7 @@ struct external_constructor<value_t::object>
92 #ifdef JSON_HAS_CPP_17
93 template<typename BasicJsonType, typename T,
94 enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
95 -void to_json(BasicJsonType& j, const std::optional<T>& opt)
96 +void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept
97 {
98 if (opt.has_value())
99 {
100 diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp
101 index 7d0ffaab11..f4e63aabef 100644
102 --- a/single_include/nlohmann/json.hpp
103 +++ b/single_include/nlohmann/json.hpp
104 @@ -173,9 +173,6 @@
105 #include <forward_list> // forward_list
106 #include <iterator> // inserter, front_inserter, end
107 #include <map> // map
108 -#ifdef JSON_HAS_CPP_17
109 - #include <optional> // optional
110 -#endif
111 #include <string> // string
112 #include <tuple> // tuple, make_tuple
113 #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
114 @@ -4817,6 +4814,11 @@ NLOHMANN_JSON_NAMESPACE_END
115 // #include <nlohmann/detail/value_t.hpp>
116
117
118 +// include after macro_scope.hpp
119 +#ifdef JSON_HAS_CPP_17
120 + #include <optional> // optional
121 +#endif
122 +
123 NLOHMANN_JSON_NAMESPACE_BEGIN
124 namespace detail
125 {
126 @@ -4832,7 +4834,6 @@ inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
127 }
128
129 #ifdef JSON_HAS_CPP_17
130 -#ifndef JSON_USE_IMPLICIT_CONVERSIONS
131 template<typename BasicJsonType, typename T>
132 void from_json(const BasicJsonType& j, std::optional<T>& opt)
133 {
134 @@ -4845,8 +4846,6 @@ void from_json(const BasicJsonType& j, std::optional<T>& opt)
135 opt.emplace(j.template get<T>());
136 }
137 }
138 -
139 -#endif // JSON_USE_IMPLICIT_CONVERSIONS
140 #endif // JSON_HAS_CPP_17
141
142 // overloads for basic_json template parameters
143 @@ -5914,7 +5913,7 @@ struct external_constructor<value_t::object>
144 #ifdef JSON_HAS_CPP_17
145 template<typename BasicJsonType, typename T,
146 enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
147 -void to_json(BasicJsonType& j, const std::optional<T>& opt)
148 +void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept
149 {
150 if (opt.has_value())
151 {
152 diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp
153 index 2c3977fef9..22bb8efc65 100644
154 --- a/tests/src/unit-regression2.cpp
155 +++ b/tests/src/unit-regression2.cpp
156 @@ -36,6 +36,14 @@ using ordered_json = nlohmann::ordered_json;
157 #include <variant>
158 #endif
159
160 +#ifdef JSON_HAS_CPP_17
161 + #if __has_include(<optional>)
162 + #include <optional>
163 + #elif __has_include(<experimental/optional>)
164 + #include <experimental/optional>
165 + #endif
166 +#endif
167 +
168 #ifdef JSON_HAS_CPP_20
169 #if __has_include(<span>)
170 #include <span>
171 @@ -390,6 +398,19 @@ struct Example_3810
172
173 NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Example_3810, bla); // NOLINT(misc-use-internal-linkage)
174
175 +/////////////////////////////////////////////////////////////////////
176 +// for #4740
177 +/////////////////////////////////////////////////////////////////////
178 +
179 +#ifdef JSON_HAS_CPP_17
180 +struct Example_4740
181 +{
182 + std::optional<std::string> host = std::nullopt;
183 + std::optional<int> port = std::nullopt;
184 + NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Example_4740, host, port)
185 +};
186 +#endif
187 +
188 TEST_CASE("regression tests 2")
189 {
190 SECTION("issue #1001 - Fix memory leak during parser callback")
191 @@ -1037,6 +1058,28 @@ TEST_CASE("regression tests 2")
192 oj["test"] = states;
193 CHECK(oj["test"].dump() == expected);
194 }
195 +
196 +#ifdef JSON_HAS_CPP_17
197 + SECTION("issue #4740 - build issue with std::optional")
198 + {
199 + const auto t1 = Example_4740();
200 + const auto j1 = nlohmann::json(t1);
201 + CHECK(j1.dump() == "{\"host\":null,\"port\":null}");
202 + const auto t2 = j1.get<Example_4740>();
203 + CHECK(!t2.host.has_value());
204 + CHECK(!t2.port.has_value());
205 +
206 + // improve coverage
207 + auto t3 = Example_4740();
208 + t3.port = 80;
209 + t3.host = "example.com";
210 + const auto j2 = nlohmann::json(t3);
211 + CHECK(j2.dump() == "{\"host\":\"example.com\",\"port\":80}");
212 + const auto t4 = j2.get<Example_4740>();
213 + CHECK(t4.host.has_value());
214 + CHECK(t4.port.has_value());
215 + }
216 +#endif
217 }
218
219 DOCTEST_CLANG_SUPPRESS_WARNING_POP