RflySimSDK v4.10
RflySimSDK说明文档
载入中...
搜索中...
未找到
to_json.hpp
1// __ _____ _____ _____
2// __| | __| | | | JSON for Modern C++
3// | | |__ | | | | | | version 3.12.0
4// |_____|_____|_____|_|___| https://github.com/nlohmann/json
5//
6// SPDX-FileCopyrightText: 2013-2025 Niels Lohmann <https://nlohmann.me>
7// SPDX-License-Identifier: MIT
8
9#pragma once
10
11#include <nlohmann/detail/macro_scope.hpp> // JSON_HAS_CPP_17
12#ifdef JSON_HAS_CPP_17
13 #include <optional> // optional
14#endif
15
16#include <algorithm> // copy
17#include <iterator> // begin, end
18#include <memory> // allocator_traits
19#include <string> // basic_string, char_traits
20#include <tuple> // tuple, get
21#include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
22#include <utility> // move, forward, declval, pair
23#include <valarray> // valarray
24#include <vector> // vector
25
26#include <nlohmann/detail/iterators/iteration_proxy.hpp>
27#include <nlohmann/detail/meta/cpp_future.hpp>
28#include <nlohmann/detail/meta/std_fs.hpp>
29#include <nlohmann/detail/meta/type_traits.hpp>
30#include <nlohmann/detail/value_t.hpp>
31
32NLOHMANN_JSON_NAMESPACE_BEGIN
33namespace detail
34{
35
36//////////////////
37// constructors //
38//////////////////
39
40/*
41 * Note all external_constructor<>::construct functions need to call
42 * j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
43 * allocated value (e.g., a string). See bug issue
44 * https://github.com/nlohmann/json/issues/2865 for more information.
45 */
46
47template<value_t> struct external_constructor;
48
49template<>
51{
52 template<typename BasicJsonType>
53 static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
54 {
55 j.m_data.m_value.destroy(j.m_data.m_type);
56 j.m_data.m_type = value_t::boolean;
57 j.m_data.m_value = b;
58 j.assert_invariant();
59 }
60};
61
62template<>
64{
65 template<typename BasicJsonType>
66 static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
67 {
68 j.m_data.m_value.destroy(j.m_data.m_type);
69 j.m_data.m_type = value_t::string;
70 j.m_data.m_value = s;
71 j.assert_invariant();
72 }
73
74 template<typename BasicJsonType>
75 static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
76 {
77 j.m_data.m_value.destroy(j.m_data.m_type);
78 j.m_data.m_type = value_t::string;
79 j.m_data.m_value = std::move(s);
80 j.assert_invariant();
81 }
82
83 template < typename BasicJsonType, typename CompatibleStringType,
84 enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
85 int > = 0 >
86 static void construct(BasicJsonType& j, const CompatibleStringType& str)
87 {
88 j.m_data.m_value.destroy(j.m_data.m_type);
89 j.m_data.m_type = value_t::string;
90 j.m_data.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
91 j.assert_invariant();
92 }
93};
94
95template<>
97{
98 template<typename BasicJsonType>
99 static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
100 {
101 j.m_data.m_value.destroy(j.m_data.m_type);
102 j.m_data.m_type = value_t::binary;
103 j.m_data.m_value = typename BasicJsonType::binary_t(b);
104 j.assert_invariant();
105 }
106
107 template<typename BasicJsonType>
108 static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
109 {
110 j.m_data.m_value.destroy(j.m_data.m_type);
111 j.m_data.m_type = value_t::binary;
112 j.m_data.m_value = typename BasicJsonType::binary_t(std::move(b));
113 j.assert_invariant();
114 }
115};
116
117template<>
119{
120 template<typename BasicJsonType>
121 static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
122 {
123 j.m_data.m_value.destroy(j.m_data.m_type);
124 j.m_data.m_type = value_t::number_float;
125 j.m_data.m_value = val;
126 j.assert_invariant();
127 }
128};
129
130template<>
132{
133 template<typename BasicJsonType>
134 static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
135 {
136 j.m_data.m_value.destroy(j.m_data.m_type);
137 j.m_data.m_type = value_t::number_unsigned;
138 j.m_data.m_value = val;
139 j.assert_invariant();
140 }
141};
142
143template<>
145{
146 template<typename BasicJsonType>
147 static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
148 {
149 j.m_data.m_value.destroy(j.m_data.m_type);
150 j.m_data.m_type = value_t::number_integer;
151 j.m_data.m_value = val;
152 j.assert_invariant();
153 }
154};
155
156template<>
158{
159 template<typename BasicJsonType>
160 static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
161 {
162 j.m_data.m_value.destroy(j.m_data.m_type);
163 j.m_data.m_type = value_t::array;
164 j.m_data.m_value = arr;
165 j.set_parents();
166 j.assert_invariant();
167 }
168
169 template<typename BasicJsonType>
170 static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
171 {
172 j.m_data.m_value.destroy(j.m_data.m_type);
173 j.m_data.m_type = value_t::array;
174 j.m_data.m_value = std::move(arr);
175 j.set_parents();
176 j.assert_invariant();
177 }
178
179 template < typename BasicJsonType, typename CompatibleArrayType,
180 enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
181 int > = 0 >
182 static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
183 {
184 using std::begin;
185 using std::end;
186
187 j.m_data.m_value.destroy(j.m_data.m_type);
188 j.m_data.m_type = value_t::array;
189 j.m_data.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
190 j.set_parents();
191 j.assert_invariant();
192 }
193
194 template<typename BasicJsonType>
195 static void construct(BasicJsonType& j, const std::vector<bool>& arr)
196 {
197 j.m_data.m_value.destroy(j.m_data.m_type);
198 j.m_data.m_type = value_t::array;
199 j.m_data.m_value = value_t::array;
200 j.m_data.m_value.array->reserve(arr.size());
201 for (const bool x : arr)
202 {
203 j.m_data.m_value.array->push_back(x);
204 j.set_parent(j.m_data.m_value.array->back());
205 }
206 j.assert_invariant();
207 }
208
209 template<typename BasicJsonType, typename T,
210 enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
211 static void construct(BasicJsonType& j, const std::valarray<T>& arr)
212 {
213 j.m_data.m_value.destroy(j.m_data.m_type);
214 j.m_data.m_type = value_t::array;
215 j.m_data.m_value = value_t::array;
216 j.m_data.m_value.array->resize(arr.size());
217 if (arr.size() > 0)
218 {
219 std::copy(std::begin(arr), std::end(arr), j.m_data.m_value.array->begin());
220 }
221 j.set_parents();
222 j.assert_invariant();
223 }
224};
225
226template<>
228{
229 template<typename BasicJsonType>
230 static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
231 {
232 j.m_data.m_value.destroy(j.m_data.m_type);
233 j.m_data.m_type = value_t::object;
234 j.m_data.m_value = obj;
235 j.set_parents();
236 j.assert_invariant();
237 }
238
239 template<typename BasicJsonType>
240 static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
241 {
242 j.m_data.m_value.destroy(j.m_data.m_type);
243 j.m_data.m_type = value_t::object;
244 j.m_data.m_value = std::move(obj);
245 j.set_parents();
246 j.assert_invariant();
247 }
248
249 template < typename BasicJsonType, typename CompatibleObjectType,
250 enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
251 static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
252 {
253 using std::begin;
254 using std::end;
255
256 j.m_data.m_value.destroy(j.m_data.m_type);
257 j.m_data.m_type = value_t::object;
258 j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
259 j.set_parents();
260 j.assert_invariant();
261 }
262};
263
264/////////////
265// to_json //
266/////////////
267
268#ifdef JSON_HAS_CPP_17
269template<typename BasicJsonType, typename T,
270 enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
271void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept
272{
273 if (opt.has_value())
274 {
275 j = *opt;
276 }
277 else
278 {
279 j = nullptr;
280 }
281}
282#endif
283
284template<typename BasicJsonType, typename T,
285 enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
286inline void to_json(BasicJsonType& j, T b) noexcept
287{
289}
290
291template < typename BasicJsonType, typename BoolRef,
292 enable_if_t <
293 ((std::is_same<std::vector<bool>::reference, BoolRef>::value
294 && !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
295 || (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
296 && !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
297 typename BasicJsonType::boolean_t >::value))
298 && std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
299inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
300{
301 external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
302}
303
304template<typename BasicJsonType, typename CompatibleString,
305 enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
306inline void to_json(BasicJsonType& j, const CompatibleString& s)
307{
309}
310
311template<typename BasicJsonType>
312inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
313{
315}
316
317template<typename BasicJsonType, typename FloatType,
318 enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
319inline void to_json(BasicJsonType& j, FloatType val) noexcept
320{
321 external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
322}
323
324template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
325 enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
326inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
327{
328 external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
329}
330
331template<typename BasicJsonType, typename CompatibleNumberIntegerType,
332 enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
333inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
334{
335 external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
336}
337
338#if !JSON_DISABLE_ENUM_SERIALIZATION
339template<typename BasicJsonType, typename EnumType,
340 enable_if_t<std::is_enum<EnumType>::value, int> = 0>
341inline void to_json(BasicJsonType& j, EnumType e) noexcept
342{
343 using underlying_type = typename std::underlying_type<EnumType>::type;
344 static constexpr value_t integral_value_t = std::is_unsigned<underlying_type>::value ? value_t::number_unsigned : value_t::number_integer;
345 external_constructor<integral_value_t>::construct(j, static_cast<underlying_type>(e));
346}
347#endif // JSON_DISABLE_ENUM_SERIALIZATION
348
349template<typename BasicJsonType>
350inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
351{
353}
354
355template < typename BasicJsonType, typename CompatibleArrayType,
356 enable_if_t < is_compatible_array_type<BasicJsonType,
357 CompatibleArrayType>::value&&
359 !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
360 !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
362 int > = 0 >
363inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
364{
366}
367
368template<typename BasicJsonType>
369inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
370{
372}
373
374template<typename BasicJsonType, typename T,
375 enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
376inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
377{
379}
380
381template<typename BasicJsonType>
382inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
383{
385}
386
387template < typename BasicJsonType, typename CompatibleObjectType,
388 enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
389inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
390{
392}
393
394template<typename BasicJsonType>
395inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
396{
398}
399
400template <
401 typename BasicJsonType, typename T, std::size_t N,
402 enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
403 const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
404 int > = 0 >
405inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
406{
408}
409
410template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
411inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
412{
413 j = { p.first, p.second };
414}
415
416// for https://github.com/nlohmann/json/pull/1134
417template<typename BasicJsonType, typename T,
418 enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
419inline void to_json(BasicJsonType& j, const T& b)
420{
421 j = { {b.key(), b.value()} };
422}
423
424template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
425inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
426{
427 j = { std::get<Idx>(t)... };
428}
429
430template<typename BasicJsonType, typename Tuple>
431inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/)
432{
433 using array_t = typename BasicJsonType::array_t;
434 j = array_t();
435}
436
437template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
438inline void to_json(BasicJsonType& j, const T& t)
439{
440 to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
441}
442
443#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
444#if defined(__cpp_lib_char8_t)
445template<typename BasicJsonType, typename Tr, typename Allocator>
446inline void to_json(BasicJsonType& j, const std::basic_string<char8_t, Tr, Allocator>& s)
447{
448 using OtherAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<char>;
449 j = std::basic_string<char, std::char_traits<char>, OtherAllocator>(s.begin(), s.end(), s.get_allocator());
450}
451#endif
452
453template<typename BasicJsonType>
454inline void to_json(BasicJsonType& j, const std_fs::path& p)
455{
456 // Returns either a std::string or a std::u8string depending whether library
457 // support for char8_t is enabled.
458 j = p.u8string();
459}
460#endif
461
463{
464 template<typename BasicJsonType, typename T>
465 auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
466 -> decltype(to_json(j, std::forward<T>(val)), void())
467 {
468 return to_json(j, std::forward<T>(val));
469 }
470};
471} // namespace detail
472
473#ifndef JSON_HAS_CPP_17
474/// namespace to hold default `to_json` function
475/// to see why this is required:
476/// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
477namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
478{
479#endif
480JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
481 detail::static_const<detail::to_json_fn>::value;
482#ifndef JSON_HAS_CPP_17
483} // namespace
484#endif
485
486NLOHMANN_JSON_NAMESPACE_END
detail namespace with internal helper functions
定义 from_json.hpp:43
value_t
the JSON type enumeration
定义 value_t.hpp:54
@ number_integer
number value (signed integer)
定义 value_t.hpp:60
@ boolean
boolean value
定义 value_t.hpp:59
@ binary
binary array (ordered collection of bytes)
定义 value_t.hpp:63
@ object
object (unordered set of name/value pairs)
定义 value_t.hpp:56
@ string
string value
定义 value_t.hpp:58
@ number_float
number value (floating-point)
定义 value_t.hpp:62
@ number_unsigned
number value (unsigned integer)
定义 value_t.hpp:61
@ array
array (ordered collection of values)
定义 value_t.hpp:57
定义 to_json.hpp:47
定义 type_traits.hpp:52
定义 type_traits.hpp:473
定义 type_traits.hpp:394
定义 to_json.hpp:463