12#include <nlohmann/detail/iterators/iteration_proxy.hpp>
13#include <nlohmann/detail/meta/cpp_future.hpp>
14#include <nlohmann/detail/meta/type_traits.hpp>
15#include <nlohmann/detail/value_t.hpp>
37 template<
typename BasicJsonType>
38 static void construct(BasicJsonType& j,
typename BasicJsonType::boolean_t b)
noexcept
40 j.m_value.destroy(j.m_type);
50 template<
typename BasicJsonType>
51 static void construct(BasicJsonType& j,
const typename BasicJsonType::string_t& s)
53 j.m_value.destroy(j.m_type);
59 template<
typename BasicJsonType>
60 static void construct(BasicJsonType& j,
typename BasicJsonType::string_t&& s)
62 j.m_value.destroy(j.m_type);
64 j.m_value = std::move(s);
68 template <
typename BasicJsonType,
typename CompatibleStringType,
69 enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
71 static void construct(BasicJsonType& j,
const CompatibleStringType& str)
73 j.m_value.destroy(j.m_type);
75 j.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
83 template<
typename BasicJsonType>
84 static void construct(BasicJsonType& j,
const typename BasicJsonType::binary_t& b)
86 j.m_value.destroy(j.m_type);
88 j.m_value =
typename BasicJsonType::binary_t(b);
92 template<
typename BasicJsonType>
93 static void construct(BasicJsonType& j,
typename BasicJsonType::binary_t&& b)
95 j.m_value.destroy(j.m_type);
97 j.m_value =
typename BasicJsonType::binary_t(std::move(b));
105 template<
typename BasicJsonType>
106 static void construct(BasicJsonType& j,
typename BasicJsonType::number_float_t val)
noexcept
108 j.m_value.destroy(j.m_type);
111 j.assert_invariant();
118 template<
typename BasicJsonType>
119 static void construct(BasicJsonType& j,
typename BasicJsonType::number_unsigned_t val)
noexcept
121 j.m_value.destroy(j.m_type);
124 j.assert_invariant();
131 template<
typename BasicJsonType>
132 static void construct(BasicJsonType& j,
typename BasicJsonType::number_integer_t val)
noexcept
134 j.m_value.destroy(j.m_type);
137 j.assert_invariant();
144 template<
typename BasicJsonType>
145 static void construct(BasicJsonType& j,
const typename BasicJsonType::array_t& arr)
147 j.m_value.destroy(j.m_type);
151 j.assert_invariant();
154 template<
typename BasicJsonType>
155 static void construct(BasicJsonType& j,
typename BasicJsonType::array_t&& arr)
157 j.m_value.destroy(j.m_type);
159 j.m_value = std::move(arr);
161 j.assert_invariant();
164 template <
typename BasicJsonType,
typename CompatibleArrayType,
165 enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
167 static void construct(BasicJsonType& j,
const CompatibleArrayType& arr)
172 j.m_value.destroy(j.m_type);
174 j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
176 j.assert_invariant();
179 template<
typename BasicJsonType>
180 static void construct(BasicJsonType& j,
const std::vector<bool>& arr)
182 j.m_value.destroy(j.m_type);
185 j.m_value.array->reserve(arr.size());
186 for (
const bool x : arr)
188 j.m_value.array->push_back(x);
189 j.set_parent(j.m_value.array->back());
191 j.assert_invariant();
194 template<
typename BasicJsonType,
typename T,
195 enable_if_t<std::is_convertible<T, BasicJsonType>::value,
int> = 0>
196 static void construct(BasicJsonType& j,
const std::valarray<T>& arr)
198 j.m_value.destroy(j.m_type);
201 j.m_value.array->resize(arr.size());
204 std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
207 j.assert_invariant();
214 template<
typename BasicJsonType>
215 static void construct(BasicJsonType& j,
const typename BasicJsonType::object_t& obj)
217 j.m_value.destroy(j.m_type);
221 j.assert_invariant();
224 template<
typename BasicJsonType>
225 static void construct(BasicJsonType& j,
typename BasicJsonType::object_t&& obj)
227 j.m_value.destroy(j.m_type);
229 j.m_value = std::move(obj);
231 j.assert_invariant();
234 template <
typename BasicJsonType,
typename CompatibleObjectType,
235 enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value,
int > = 0 >
236 static void construct(BasicJsonType& j,
const CompatibleObjectType& obj)
241 j.m_value.destroy(j.m_type);
243 j.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
245 j.assert_invariant();
253template<
typename BasicJsonType,
typename T,
254 enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value,
int> = 0>
255void to_json(BasicJsonType& j, T b)
noexcept
260template<
typename BasicJsonType,
typename CompatibleString,
261 enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value,
int> = 0>
262void to_json(BasicJsonType& j,
const CompatibleString& s)
264 external_constructor<value_t::string>::construct(j, s);
267template<
typename BasicJsonType>
268void to_json(BasicJsonType& j,
typename BasicJsonType::string_t&& s)
270 external_constructor<value_t::string>::construct(j, std::move(s));
273template<
typename BasicJsonType,
typename FloatType,
274 enable_if_t<std::is_floating_point<FloatType>::value,
int> = 0>
275void to_json(BasicJsonType& j, FloatType val)
noexcept
277 external_constructor<value_t::number_float>::construct(j,
static_cast<typename BasicJsonType::number_float_t
>(val));
280template<
typename BasicJsonType,
typename CompatibleNumberUnsignedType,
281 enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value,
int> = 0>
282void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val)
noexcept
284 external_constructor<value_t::number_unsigned>::construct(j,
static_cast<typename BasicJsonType::number_unsigned_t
>(val));
287template<
typename BasicJsonType,
typename CompatibleNumberIntegerType,
288 enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value,
int> = 0>
289void to_json(BasicJsonType& j, CompatibleNumberIntegerType val)
noexcept
291 external_constructor<value_t::number_integer>::construct(j,
static_cast<typename BasicJsonType::number_integer_t
>(val));
294template<
typename BasicJsonType,
typename EnumType,
295 enable_if_t<std::is_enum<EnumType>::value,
int> = 0>
296void to_json(BasicJsonType& j, EnumType e)
noexcept
298 using underlying_type =
typename std::underlying_type<EnumType>::type;
299 external_constructor<value_t::number_integer>::construct(j,
static_cast<underlying_type
>(e));
302template<
typename BasicJsonType>
303void to_json(BasicJsonType& j,
const std::vector<bool>& e)
305 external_constructor<value_t::array>::construct(j, e);
308template <
typename BasicJsonType,
typename CompatibleArrayType,
309 enable_if_t < is_compatible_array_type<BasicJsonType,
311 !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
312 !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
313 !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
314 !is_basic_json<CompatibleArrayType>::value,
316void to_json(BasicJsonType& j,
const CompatibleArrayType& arr)
318 external_constructor<value_t::array>::construct(j, arr);
321template<
typename BasicJsonType>
322void to_json(BasicJsonType& j,
const typename BasicJsonType::binary_t& bin)
324 external_constructor<value_t::binary>::construct(j, bin);
327template<
typename BasicJsonType,
typename T,
328 enable_if_t<std::is_convertible<T, BasicJsonType>::value,
int> = 0>
329void to_json(BasicJsonType& j,
const std::valarray<T>& arr)
331 external_constructor<value_t::array>::construct(j, std::move(arr));
334template<
typename BasicJsonType>
335void to_json(BasicJsonType& j,
typename BasicJsonType::array_t&& arr)
337 external_constructor<value_t::array>::construct(j, std::move(arr));
340template <
typename BasicJsonType,
typename CompatibleObjectType,
341 enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value,
int > = 0 >
342void to_json(BasicJsonType& j,
const CompatibleObjectType& obj)
344 external_constructor<value_t::object>::construct(j, obj);
347template<
typename BasicJsonType>
348void to_json(BasicJsonType& j,
typename BasicJsonType::object_t&& obj)
350 external_constructor<value_t::object>::construct(j, std::move(obj));
354 typename BasicJsonType,
typename T, std::size_t N,
355 enable_if_t < !std::is_constructible<
typename BasicJsonType::string_t,
358void to_json(BasicJsonType& j,
const T(&arr)[N])
360 external_constructor<value_t::array>::construct(j, arr);
363template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value,
int > = 0 >
364void to_json(BasicJsonType& j,
const std::pair<T1, T2>& p)
366 j = { p.first, p.second };
370template<
typename BasicJsonType,
typename T,
371 enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>
::value,
int> = 0>
372void to_json(BasicJsonType& j,
const T& b)
374 j = { {b.key(), b.value()} };
377template<
typename BasicJsonType,
typename Tuple, std::size_t... Idx>
378void to_json_tuple_impl(BasicJsonType& j,
const Tuple& t, index_sequence<Idx...> )
380 j = { std::get<Idx>(t)... };
383template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value,
int > = 0>
384void to_json(BasicJsonType& j,
const T& t)
386 to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
391 template<
typename BasicJsonType,
typename T>
392 auto operator()(BasicJsonType& j, T&& val)
const noexcept(
noexcept(to_json(j, std::forward<T>(val))))
393 ->
decltype(to_json(j, std::forward<T>(val)), void())
395 return to_json(j, std::forward<T>(val));
value_t
the JSON type enumeration
Definition value_t.hpp:41
@ number_integer
number value (signed integer)
@ binary
binary array (ordered collection of bytes)
@ object
object (unordered set of name/value pairs)
@ number_float
number value (floating-point)
@ number_unsigned
number value (unsigned integer)
@ array
array (ordered collection of values)
@ value
the parser finished reading a JSON value
namespace for Niels Lohmann
Definition adl_serializer.hpp:12
Definition to_json.hpp:32
Definition cpp_future.hpp:146
Definition to_json.hpp:390