WPILibC++ 2024.3.2
from_json.h
Go to the documentation of this file.
1// __ _____ _____ _____
2// __| | __| | | | JSON for Modern C++
3// | | |__ | | | | | | version 3.11.2
4// |_____|_____|_____|_|___| https://github.com/nlohmann/json
5//
6// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
7// SPDX-License-Identifier: MIT
8
9#pragma once
10
11#include <algorithm> // transform
12#include <array> // array
13#include <forward_list> // forward_list
14#include <iterator> // inserter, front_inserter, end
15#include <map> // map
16#include <string> // string
17#include <tuple> // tuple, make_tuple
18#include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
19#include <unordered_map> // unordered_map
20#include <utility> // pair, declval
21#include <valarray> // valarray
22
30#include <wpi/detail/value_t.h>
31
33namespace detail
34{
35
36template<typename BasicJsonType>
37inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
38{
39 if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
40 {
41 JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
42 }
43 n = nullptr;
44}
45
46// overloads for basic_json template parameters
47template < typename BasicJsonType, typename ArithmeticType,
49 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
50 int > = 0 >
51void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
52{
53 switch (static_cast<value_t>(j))
54 {
56 {
57 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
58 break;
59 }
61 {
62 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
63 break;
64 }
66 {
67 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
68 break;
69 }
70
71 case value_t::null:
72 case value_t::object:
73 case value_t::array:
74 case value_t::string:
76 case value_t::binary:
78 default:
79 JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
80 }
81}
82
83template<typename BasicJsonType>
84inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
85{
86 if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
87 {
88 JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
89 }
90 b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
91}
92
93template<typename BasicJsonType>
94inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
95{
96 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
97 {
98 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
99 }
100 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
101}
102
103template <
104 typename BasicJsonType, typename StringType,
106 std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
107 && is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
108 && !std::is_same<typename BasicJsonType::string_t, StringType>::value
109 && !is_json_ref<StringType>::value, int > = 0 >
110inline void from_json(const BasicJsonType& j, StringType& s)
111{
112 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
113 {
114 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
115 }
116
117 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
118}
119
120template<typename BasicJsonType>
121inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
122{
123 get_arithmetic_value(j, val);
124}
125
126template<typename BasicJsonType>
127inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
128{
129 get_arithmetic_value(j, val);
130}
131
132template<typename BasicJsonType>
133inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
134{
135 get_arithmetic_value(j, val);
136}
137
138#if !JSON_DISABLE_ENUM_SERIALIZATION
139template<typename BasicJsonType, typename EnumType,
141inline void from_json(const BasicJsonType& j, EnumType& e)
142{
144 get_arithmetic_value(j, val);
145 e = static_cast<EnumType>(val);
146}
147#endif // JSON_DISABLE_ENUM_SERIALIZATION
148
149// forward_list doesn't have an insert method
150template<typename BasicJsonType, typename T, typename Allocator,
152inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
153{
154 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
155 {
156 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
157 }
158 l.clear();
159 std::transform(j.rbegin(), j.rend(),
160 std::front_inserter(l), [](const BasicJsonType & i)
161 {
162 return i.template get<T>();
163 });
164}
165
166// valarray doesn't have an insert method
167template<typename BasicJsonType, typename T,
169inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
170{
171 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
172 {
173 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
174 }
175 l.resize(j.size());
176 std::transform(j.begin(), j.end(), std::begin(l),
177 [](const BasicJsonType & elem)
178 {
179 return elem.template get<T>();
180 });
181}
182
183template<typename BasicJsonType, typename T, std::size_t N>
184auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
185-> decltype(j.template get<T>(), void())
186{
187 for (std::size_t i = 0; i < N; ++i)
188 {
189 arr[i] = j.at(i).template get<T>();
190 }
191}
192
193template<typename BasicJsonType>
194inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
195{
196 arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
197}
198
199template<typename BasicJsonType, typename T, std::size_t N>
200auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
201 priority_tag<2> /*unused*/)
202-> decltype(j.template get<T>(), void())
203{
204 for (std::size_t i = 0; i < N; ++i)
205 {
206 arr[i] = j.at(i).template get<T>();
207 }
208}
209
210template<typename BasicJsonType, typename ConstructibleArrayType,
212 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
213 int> = 0>
214auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
215-> decltype(
216 arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
217 j.template get<typename ConstructibleArrayType::value_type>(),
218 void())
219{
220 using std::end;
221
222 ConstructibleArrayType ret;
223 ret.reserve(j.size());
224 std::transform(j.begin(), j.end(),
225 std::inserter(ret, end(ret)), [](const BasicJsonType & i)
226 {
227 // get<BasicJsonType>() returns *this, this won't call a from_json
228 // method when value_type is BasicJsonType
229 return i.template get<typename ConstructibleArrayType::value_type>();
230 });
231 arr = std::move(ret);
232}
233
234template<typename BasicJsonType, typename ConstructibleArrayType,
236 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
237 int> = 0>
238inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
239 priority_tag<0> /*unused*/)
240{
241 using std::end;
242
243 ConstructibleArrayType ret;
244 std::transform(
245 j.begin(), j.end(), std::inserter(ret, end(ret)),
246 [](const BasicJsonType & i)
247 {
248 // get<BasicJsonType>() returns *this, this won't call a from_json
249 // method when value_type is BasicJsonType
250 return i.template get<typename ConstructibleArrayType::value_type>();
251 });
252 arr = std::move(ret);
253}
254
255template < typename BasicJsonType, typename ConstructibleArrayType,
257 is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
258 !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
260 !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
261 !is_basic_json<ConstructibleArrayType>::value,
262 int > = 0 >
263auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
264-> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
265j.template get<typename ConstructibleArrayType::value_type>(),
266void())
267{
268 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
269 {
270 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
271 }
272
273 from_json_array_impl(j, arr, priority_tag<3> {});
274}
275
276template < typename BasicJsonType, typename T, std::size_t... Idx >
277std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
278 identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
279{
280 return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
281}
282
283template < typename BasicJsonType, typename T, std::size_t N >
284auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
285-> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
286{
287 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
288 {
289 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
290 }
291
292 return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
293}
294
295template<typename BasicJsonType>
296inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
297{
298 if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
299 {
300 JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
301 }
302
303 bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
304}
305
306template<typename BasicJsonType, typename ConstructibleObjectType,
308inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
309{
310 if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
311 {
312 JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
313 }
314
315 ConstructibleObjectType ret;
316 const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
317 using value_type = typename ConstructibleObjectType::value_type;
318 std::transform(
319 inner_object->begin(), inner_object->end(),
320 std::inserter(ret, ret.begin()),
321 [](typename BasicJsonType::object_t::value_type const & p)
322 {
323 return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
324 });
325 obj = std::move(ret);
326}
327
328// overload for arithmetic types, not chosen for basic_json template arguments
329// (BooleanType, etc..); note: Is it really necessary to provide explicit
330// overloads for boolean_t etc. in case of a custom BooleanType which is not
331// an arithmetic type?
332template < typename BasicJsonType, typename ArithmeticType,
334 std::is_arithmetic<ArithmeticType>::value&&
335 !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
336 !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
337 !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
338 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
339 int > = 0 >
340inline void from_json(const BasicJsonType& j, ArithmeticType& val)
341{
342 switch (static_cast<value_t>(j))
343 {
345 {
346 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
347 break;
348 }
350 {
351 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
352 break;
353 }
355 {
356 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
357 break;
358 }
359 case value_t::boolean:
360 {
361 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
362 break;
363 }
364
365 case value_t::null:
366 case value_t::object:
367 case value_t::array:
368 case value_t::string:
369 case value_t::binary:
371 default:
372 JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
373 }
374}
375
376template<typename BasicJsonType, typename... Args, std::size_t... Idx>
377std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
378{
379 return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
380}
381
382template < typename BasicJsonType, class A1, class A2 >
383std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
384{
385 return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
386 std::forward<BasicJsonType>(j).at(1).template get<A2>()};
387}
388
389template<typename BasicJsonType, typename A1, typename A2>
390inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
391{
392 p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
393}
394
395template<typename BasicJsonType, typename... Args>
396std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
397{
398 return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
399}
400
401template<typename BasicJsonType, typename... Args>
402inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
403{
404 t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
405}
406
407template<typename BasicJsonType, typename TupleRelated>
408auto from_json(BasicJsonType&& j, TupleRelated&& t)
409-> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
410{
411 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
412 {
413 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
414 }
415
416 return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
417}
418
419template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
420 typename = enable_if_t < !std::is_constructible <
421 typename BasicJsonType::string_t, Key >::value >>
422inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
423{
424 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
425 {
426 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
427 }
428 m.clear();
429 for (const auto& p : j)
430 {
431 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
432 {
433 JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
434 }
435 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
436 }
437}
438
439template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
440 typename = enable_if_t < !std::is_constructible <
441 typename BasicJsonType::string_t, Key >::value >>
442inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
443{
444 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
445 {
446 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
447 }
448 m.clear();
449 for (const auto& p : j)
450 {
451 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
452 {
453 JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
454 }
455 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
456 }
457}
458
459#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
460template<typename BasicJsonType>
461inline void from_json(const BasicJsonType& j, std_fs::path& p)
462{
463 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
464 {
465 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
466 }
467 p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
468}
469#endif
470
472{
473 template<typename BasicJsonType, typename T>
474 auto operator()(const BasicJsonType& j, T&& val) const
475 noexcept(noexcept(from_json(j, std::forward<T>(val))))
476 -> decltype(from_json(j, std::forward<T>(val)))
477 {
478 return from_json(j, std::forward<T>(val));
479 }
480};
481
482} // namespace detail
483
484#ifndef JSON_HAS_CPP_17
485/// namespace to hold default `from_json` function
486/// to see why this is required:
487/// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
488namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
489{
490#endif
491JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
493#ifndef JSON_HAS_CPP_17
494} // namespace
495#endif
496
#define WPI_JSON_NAMESPACE_END
Definition: abi_macros.h:59
#define WPI_JSON_NAMESPACE_BEGIN
Definition: abi_macros.h:53
static type_error create(int id_, const std::string &what_arg, BasicJsonContext context)
Definition: exceptions.h:209
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:256
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: hedley.h:1396
#define JSON_INLINE_VARIABLE
Definition: macro_scope.h:139
#define JSON_THROW(exception)
Definition: macro_scope.h:163
detail namespace with internal helper functions
Definition: xchar.h:20
OutStringType concat(Args &&... args)
Definition: string_concat.h:137
std::pair< A1, A2 > from_json_tuple_impl(BasicJsonType &&j, identity_tag< std::pair< A1, A2 > >, priority_tag< 0 >)
Definition: from_json.h:383
void from_json_array_impl(const BasicJsonType &j, typename BasicJsonType::array_t &arr, priority_tag< 3 >)
Definition: from_json.h:194
void from_json(const BasicJsonType &j, std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > &m)
Definition: from_json.h:442
value_t
the JSON type enumeration
Definition: value_t.h:54
@ null
null value
@ number_integer
number value (signed integer)
@ boolean
boolean value
@ discarded
discarded by the parser callback function
@ binary
binary array (ordered collection of bytes)
@ object
object (unordered set of name/value pairs)
@ string
string value
@ number_float
number value (floating-point)
@ number_unsigned
number value (unsigned integer)
@ array
array (ordered collection of values)
std::array< T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType &&j, identity_tag< std::array< T, sizeof...(Idx)> >, index_sequence< Idx... >)
Definition: from_json.h:277
void from_json(const BasicJsonType &j, typename std::nullptr_t &n)
Definition: from_json.h:37
void get_arithmetic_value(const BasicJsonType &j, ArithmeticType &val)
Definition: from_json.h:51
type
Definition: core.h:556
std::tuple< Args... > from_json_tuple_impl_base(BasicJsonType &&j, index_sequence< Idx... >)
Definition: from_json.h:377
static constexpr const charge::coulomb_t e(1.6021766208e-19)
elementary charge.
b
Definition: data.h:44
Definition: from_json.h:472
auto operator()(const BasicJsonType &j, T &&val) const noexcept(noexcept(from_json(j, std::forward< T >(val)))) -> decltype(from_json(j, std::forward< T >(val)))
Definition: from_json.h:474
Definition: identity_tag.h:18
Definition: ranges.h:187
static constexpr auto value
Definition: type_traits.h:364
Definition: ranges.h:196
Definition: cpp_future.h:150
Definition: cpp_future.h:149
Definition: cpp_future.h:155