Skip to content

Commit 9294e25

Browse files
authored
Merge pull request #1301 from theodelrieu/fix/1299
add new is_constructible_* traits used in from_json
2 parents b553a8a + a946dfc commit 9294e25

4 files changed

Lines changed: 396 additions & 152 deletions

File tree

include/nlohmann/detail/conversions/from_json.hpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
8484
}
8585

8686
template <
87-
typename BasicJsonType, typename CompatibleStringType,
87+
typename BasicJsonType, typename ConstructibleStringType,
8888
enable_if_t <
89-
is_compatible_string_type<BasicJsonType, CompatibleStringType>::value and
89+
is_constructible_string_type<BasicJsonType, ConstructibleStringType>::value and
9090
not std::is_same<typename BasicJsonType::string_t,
91-
CompatibleStringType>::value,
91+
ConstructibleStringType>::value,
9292
int > = 0 >
93-
void from_json(const BasicJsonType& j, CompatibleStringType& s)
93+
void from_json(const BasicJsonType& j, ConstructibleStringType& s)
9494
{
9595
if (JSON_UNLIKELY(not j.is_string()))
9696
{
@@ -173,11 +173,11 @@ auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
173173
}
174174
}
175175

176-
template<typename BasicJsonType, typename CompatibleArrayType>
177-
auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<1> /*unused*/)
176+
template<typename BasicJsonType, typename ConstructibleArrayType>
177+
auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
178178
-> decltype(
179-
arr.reserve(std::declval<typename CompatibleArrayType::size_type>()),
180-
j.template get<typename CompatibleArrayType::value_type>(),
179+
arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
180+
j.template get<typename ConstructibleArrayType::value_type>(),
181181
void())
182182
{
183183
using std::end;
@@ -188,12 +188,12 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, prio
188188
{
189189
// get<BasicJsonType>() returns *this, this won't call a from_json
190190
// method when value_type is BasicJsonType
191-
return i.template get<typename CompatibleArrayType::value_type>();
191+
return i.template get<typename ConstructibleArrayType::value_type>();
192192
});
193193
}
194194

195-
template <typename BasicJsonType, typename CompatibleArrayType>
196-
void from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr,
195+
template <typename BasicJsonType, typename ConstructibleArrayType>
196+
void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
197197
priority_tag<0> /*unused*/)
198198
{
199199
using std::end;
@@ -204,21 +204,21 @@ void from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr,
204204
{
205205
// get<BasicJsonType>() returns *this, this won't call a from_json
206206
// method when value_type is BasicJsonType
207-
return i.template get<typename CompatibleArrayType::value_type>();
207+
return i.template get<typename ConstructibleArrayType::value_type>();
208208
});
209209
}
210210

211-
template <typename BasicJsonType, typename CompatibleArrayType,
211+
template <typename BasicJsonType, typename ConstructibleArrayType,
212212
enable_if_t <
213-
is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value and
214-
not is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value and
215-
not is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value and
216-
not is_basic_json<CompatibleArrayType>::value,
213+
is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value and
214+
not is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value and
215+
not is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value and
216+
not is_basic_json<ConstructibleArrayType>::value,
217217
int > = 0 >
218218

219-
auto from_json(const BasicJsonType& j, CompatibleArrayType& arr)
219+
auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
220220
-> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
221-
j.template get<typename CompatibleArrayType::value_type>(),
221+
j.template get<typename ConstructibleArrayType::value_type>(),
222222
void())
223223
{
224224
if (JSON_UNLIKELY(not j.is_array()))
@@ -230,23 +230,23 @@ void())
230230
from_json_array_impl(j, arr, priority_tag<3> {});
231231
}
232232

233-
template<typename BasicJsonType, typename CompatibleObjectType,
234-
enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value, int> = 0>
235-
void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
233+
template<typename BasicJsonType, typename ConstructibleObjectType,
234+
enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
235+
void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
236236
{
237237
if (JSON_UNLIKELY(not j.is_object()))
238238
{
239239
JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name())));
240240
}
241241

242242
auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
243-
using value_type = typename CompatibleObjectType::value_type;
243+
using value_type = typename ConstructibleObjectType::value_type;
244244
std::transform(
245245
inner_object->begin(), inner_object->end(),
246246
std::inserter(obj, obj.begin()),
247247
[](typename BasicJsonType::object_t::value_type const & p)
248248
{
249-
return value_type(p.first, p.second.template get<typename CompatibleObjectType::mapped_type>());
249+
return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
250250
});
251251
}
252252

include/nlohmann/detail/meta/type_traits.hpp

Lines changed: 153 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ namespace detail
2626
// helpers //
2727
/////////////
2828

29+
// Note to maintainers:
30+
//
31+
// Every trait in this file expects a non CV-qualified type.
32+
// The only exceptions are in the 'aliases for detected' section
33+
// (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
34+
//
35+
// In this case, T has to be properly CV-qualified to constraint the function arguments
36+
// (e.g. to_json(BasicJsonType&, const T&))
37+
2938
template<typename> struct is_basic_json : std::false_type {};
3039

3140
NLOHMANN_BASIC_JSON_TPL_DECLARATION
@@ -68,6 +77,52 @@ using from_json_function = decltype(T::from_json(std::declval<Args>()...));
6877
template <typename T, typename U>
6978
using get_template_function = decltype(std::declval<T>().template get<U>());
7079

80+
// trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
81+
template <typename BasicJsonType, typename T, typename = void>
82+
struct has_from_json : std::false_type {};
83+
84+
template <typename BasicJsonType, typename T>
85+
struct has_from_json<BasicJsonType, T,
86+
enable_if_t<not is_basic_json<T>::value>>
87+
{
88+
using serializer = typename BasicJsonType::template json_serializer<T, void>;
89+
90+
static constexpr bool value =
91+
is_detected_exact<void, from_json_function, serializer,
92+
const BasicJsonType&, T&>::value;
93+
};
94+
95+
// This trait checks if JSONSerializer<T>::from_json(json const&) exists
96+
// this overload is used for non-default-constructible user-defined-types
97+
template <typename BasicJsonType, typename T, typename = void>
98+
struct has_non_default_from_json : std::false_type {};
99+
100+
template<typename BasicJsonType, typename T>
101+
struct has_non_default_from_json<BasicJsonType, T, enable_if_t<not is_basic_json<T>::value>>
102+
{
103+
using serializer = typename BasicJsonType::template json_serializer<T, void>;
104+
105+
static constexpr bool value =
106+
is_detected_exact<T, from_json_function, serializer,
107+
const BasicJsonType&>::value;
108+
};
109+
110+
// This trait checks if BasicJsonType::json_serializer<T>::to_json exists
111+
// Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
112+
template <typename BasicJsonType, typename T, typename = void>
113+
struct has_to_json : std::false_type {};
114+
115+
template <typename BasicJsonType, typename T>
116+
struct has_to_json<BasicJsonType, T, enable_if_t<not is_basic_json<T>::value>>
117+
{
118+
using serializer = typename BasicJsonType::template json_serializer<T, void>;
119+
120+
static constexpr bool value =
121+
is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
122+
T>::value;
123+
};
124+
125+
71126
///////////////////
72127
// is_ functions //
73128
///////////////////
@@ -123,6 +178,35 @@ template <typename BasicJsonType, typename CompatibleObjectType>
123178
struct is_compatible_object_type
124179
: is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
125180

181+
template <typename BasicJsonType, typename ConstructibleObjectType,
182+
typename = void>
183+
struct is_constructible_object_type_impl : std::false_type {};
184+
185+
template <typename BasicJsonType, typename ConstructibleObjectType>
186+
struct is_constructible_object_type_impl <
187+
BasicJsonType, ConstructibleObjectType,
188+
enable_if_t<is_detected<mapped_type_t, ConstructibleObjectType>::value and
189+
is_detected<key_type_t, ConstructibleObjectType>::value >>
190+
{
191+
using object_t = typename BasicJsonType::object_t;
192+
193+
static constexpr bool value =
194+
std::is_constructible<typename ConstructibleObjectType::key_type,
195+
typename object_t::key_type>::value and
196+
std::is_same<typename object_t::mapped_type,
197+
typename ConstructibleObjectType::mapped_type>::value or
198+
(has_from_json<BasicJsonType,
199+
typename ConstructibleObjectType::mapped_type>::value or
200+
has_non_default_from_json <
201+
BasicJsonType,
202+
typename ConstructibleObjectType::mapped_type >::value);
203+
};
204+
205+
template <typename BasicJsonType, typename ConstructibleObjectType>
206+
struct is_constructible_object_type
207+
: is_constructible_object_type_impl<BasicJsonType,
208+
ConstructibleObjectType> {};
209+
126210
template <typename BasicJsonType, typename CompatibleStringType,
127211
typename = void>
128212
struct is_compatible_string_type_impl : std::false_type {};
@@ -137,9 +221,28 @@ struct is_compatible_string_type_impl <
137221
std::is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
138222
};
139223

140-
template <typename BasicJsonType, typename CompatibleStringType>
224+
template <typename BasicJsonType, typename ConstrutibleStringType>
141225
struct is_compatible_string_type
142-
: is_compatible_string_type_impl<BasicJsonType, CompatibleStringType> {};
226+
: is_compatible_string_type_impl<BasicJsonType, ConstrutibleStringType> {};
227+
228+
template <typename BasicJsonType, typename ConstrutibleStringType,
229+
typename = void>
230+
struct is_constructible_string_type_impl : std::false_type {};
231+
232+
template <typename BasicJsonType, typename ConstrutibleStringType>
233+
struct is_constructible_string_type_impl <
234+
BasicJsonType, ConstrutibleStringType,
235+
enable_if_t<is_detected_exact<typename BasicJsonType::string_t::value_type,
236+
value_type_t, ConstrutibleStringType>::value >>
237+
{
238+
static constexpr auto value =
239+
std::is_constructible<ConstrutibleStringType,
240+
typename BasicJsonType::string_t>::value;
241+
};
242+
243+
template <typename BasicJsonType, typename ConstrutibleStringType>
244+
struct is_constructible_string_type
245+
: is_constructible_string_type_impl<BasicJsonType, ConstrutibleStringType> {};
143246

144247
template <typename BasicJsonType, typename CompatibleArrayType, typename = void>
145248
struct is_compatible_array_type_impl : std::false_type {};
@@ -148,18 +251,61 @@ template <typename BasicJsonType, typename CompatibleArrayType>
148251
struct is_compatible_array_type_impl <
149252
BasicJsonType, CompatibleArrayType,
150253
enable_if_t<is_detected<value_type_t, CompatibleArrayType>::value and
151-
is_detected<iterator_t, CompatibleArrayType>::value >>
254+
is_detected<iterator_t, CompatibleArrayType>::value and
255+
// This is needed because json_reverse_iterator has a ::iterator type...
256+
// Therefore it is detected as a CompatibleArrayType.
257+
// The real fix would be to have an Iterable concept.
258+
not is_iterator_traits<
259+
std::iterator_traits<CompatibleArrayType>>::value >>
152260
{
153-
// This is needed because json_reverse_iterator has a ::iterator type...
154-
// Therefore it is detected as a CompatibleArrayType.
155-
// The real fix would be to have an Iterable concept.
156-
static constexpr bool value = not is_iterator_traits<std::iterator_traits<CompatibleArrayType>>::value;
261+
static constexpr bool value =
262+
std::is_constructible<BasicJsonType,
263+
typename CompatibleArrayType::value_type>::value;
157264
};
158265

159266
template <typename BasicJsonType, typename CompatibleArrayType>
160267
struct is_compatible_array_type
161268
: is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
162269

270+
template <typename BasicJsonType, typename ConstructibleArrayType, typename = void>
271+
struct is_constructible_array_type_impl : std::false_type {};
272+
273+
template <typename BasicJsonType, typename ConstructibleArrayType>
274+
struct is_constructible_array_type_impl <
275+
BasicJsonType, ConstructibleArrayType,
276+
enable_if_t<std::is_same<ConstructibleArrayType,
277+
typename BasicJsonType::value_type>::value >>
278+
: std::true_type {};
279+
280+
template <typename BasicJsonType, typename ConstructibleArrayType>
281+
struct is_constructible_array_type_impl <
282+
BasicJsonType, ConstructibleArrayType,
283+
enable_if_t<not std::is_same<ConstructibleArrayType,
284+
typename BasicJsonType::value_type>::value and
285+
is_detected<value_type_t, ConstructibleArrayType>::value and
286+
is_detected<iterator_t, ConstructibleArrayType>::value and
287+
is_complete_type<
288+
detected_t<value_type_t, ConstructibleArrayType>>::value >>
289+
{
290+
static constexpr bool value =
291+
// This is needed because json_reverse_iterator has a ::iterator type,
292+
// furthermore, std::back_insert_iterator (and other iterators) have a base class `iterator`...
293+
// Therefore it is detected as a ConstructibleArrayType.
294+
// The real fix would be to have an Iterable concept.
295+
not is_iterator_traits <
296+
std::iterator_traits<ConstructibleArrayType >>::value and
297+
298+
(std::is_same<typename ConstructibleArrayType::value_type, typename BasicJsonType::array_t::value_type>::value or
299+
has_from_json<BasicJsonType,
300+
typename ConstructibleArrayType::value_type>::value or
301+
has_non_default_from_json <
302+
BasicJsonType, typename ConstructibleArrayType::value_type >::value);
303+
};
304+
305+
template <typename BasicJsonType, typename ConstructibleArrayType>
306+
struct is_constructible_array_type
307+
: is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
308+
163309
template <typename RealIntegerType, typename CompatibleNumberIntegerType,
164310
typename = void>
165311
struct is_compatible_integer_type_impl : std::false_type {};
@@ -187,51 +333,6 @@ struct is_compatible_integer_type
187333
: is_compatible_integer_type_impl<RealIntegerType,
188334
CompatibleNumberIntegerType> {};
189335

190-
// trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
191-
template <typename BasicJsonType, typename T, typename = void>
192-
struct has_from_json : std::false_type {};
193-
194-
template <typename BasicJsonType, typename T>
195-
struct has_from_json<BasicJsonType, T,
196-
enable_if_t<not is_basic_json<T>::value>>
197-
{
198-
using serializer = typename BasicJsonType::template json_serializer<T, void>;
199-
200-
static constexpr bool value =
201-
is_detected_exact<void, from_json_function, serializer,
202-
const BasicJsonType&, T&>::value;
203-
};
204-
205-
// This trait checks if JSONSerializer<T>::from_json(json const&) exists
206-
// this overload is used for non-default-constructible user-defined-types
207-
template <typename BasicJsonType, typename T, typename = void>
208-
struct has_non_default_from_json : std::false_type {};
209-
210-
template<typename BasicJsonType, typename T>
211-
struct has_non_default_from_json<BasicJsonType, T, enable_if_t<not is_basic_json<T>::value>>
212-
{
213-
using serializer = typename BasicJsonType::template json_serializer<T, void>;
214-
215-
static constexpr bool value =
216-
is_detected_exact<T, from_json_function, serializer,
217-
const BasicJsonType&>::value;
218-
};
219-
220-
// This trait checks if BasicJsonType::json_serializer<T>::to_json exists
221-
// Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
222-
template <typename BasicJsonType, typename T, typename = void>
223-
struct has_to_json : std::false_type {};
224-
225-
template <typename BasicJsonType, typename T>
226-
struct has_to_json<BasicJsonType, T, enable_if_t<not is_basic_json<T>::value>>
227-
{
228-
using serializer = typename BasicJsonType::template json_serializer<T, void>;
229-
230-
static constexpr bool value =
231-
is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
232-
T>::value;
233-
};
234-
235336
template <typename BasicJsonType, typename CompatibleType, typename = void>
236337
struct is_compatible_type_impl: std::false_type {};
237338

0 commit comments

Comments
 (0)