/src/arduinojson/src/ArduinoJson/Deserialization/deserialize.hpp
Line | Count | Source |
1 | | // ArduinoJson - https://arduinojson.org |
2 | | // Copyright © 2014-2026, Benoit BLANCHON |
3 | | // MIT License |
4 | | |
5 | | #pragma once |
6 | | |
7 | | #include <ArduinoJson/Deserialization/DeserializationError.hpp> |
8 | | #include <ArduinoJson/Deserialization/DeserializationOptions.hpp> |
9 | | #include <ArduinoJson/Deserialization/Reader.hpp> |
10 | | #include <ArduinoJson/Polyfills/utility.hpp> |
11 | | |
12 | | ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE |
13 | | |
14 | | // A meta-function that returns the first type of the parameter pack |
15 | | // or void if empty |
16 | | template <typename...> |
17 | | struct first_or_void { |
18 | | using type = void; |
19 | | }; |
20 | | template <typename T, typename... Rest> |
21 | | struct first_or_void<T, Rest...> { |
22 | | using type = T; |
23 | | }; |
24 | | |
25 | | // A meta-function that returns true if T is a valid destination type for |
26 | | // deserialize() |
27 | | template <class T> |
28 | | using is_deserialize_destination = |
29 | | bool_constant<is_base_of<JsonDocument, remove_cv_t<T>>::value || |
30 | | IsVariant<T>::value>; |
31 | | |
32 | | template <typename TDestination> |
33 | | inline void shrinkJsonDocument(TDestination&) { |
34 | | // no-op by default |
35 | | } |
36 | | |
37 | | #if ARDUINOJSON_AUTO_SHRINK |
38 | 0 | inline void shrinkJsonDocument(JsonDocument& doc) { |
39 | 0 | doc.shrinkToFit(); |
40 | 0 | } |
41 | | #endif |
42 | | |
43 | | template <template <typename> class TDeserializer, typename TDestination, |
44 | | typename TReader, typename TOptions> |
45 | | DeserializationError doDeserialize(TDestination&& dst, TReader reader, |
46 | | TOptions options) { |
47 | | auto data = VariantAttorney::getOrCreateData(dst); |
48 | | if (!data) |
49 | | return DeserializationError::NoMemory; |
50 | | auto resources = VariantAttorney::getResourceManager(dst); |
51 | | dst.clear(); |
52 | | auto err = TDeserializer<TReader>(resources, reader) |
53 | | .parse(data, options.filter, options.nestingLimit); |
54 | | shrinkJsonDocument(dst); |
55 | | return err; |
56 | | } |
57 | | |
58 | | template < |
59 | | template <typename> class TDeserializer, typename TDestination, |
60 | | typename TStream, typename... Args, |
61 | | enable_if_t< // issue #1897 |
62 | | !is_integral<typename first_or_void<Args...>::type>::value, int> = 0> |
63 | | DeserializationError deserialize(TDestination&& dst, TStream&& input, |
64 | | Args... args) { |
65 | | return doDeserialize<TDeserializer>( |
66 | | dst, makeReader(detail::forward<TStream>(input)), |
67 | | makeDeserializationOptions(args...)); |
68 | | } |
69 | | |
70 | | template <template <typename> class TDeserializer, typename TDestination, |
71 | | typename TChar, typename Size, typename... Args, |
72 | | enable_if_t<is_integral<Size>::value, int> = 0> |
73 | | DeserializationError deserialize(TDestination&& dst, TChar* input, |
74 | | Size inputSize, Args... args) { |
75 | | return doDeserialize<TDeserializer>(dst, makeReader(input, size_t(inputSize)), |
76 | | makeDeserializationOptions(args...)); |
77 | | } |
78 | | |
79 | | ARDUINOJSON_END_PRIVATE_NAMESPACE |