Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Utilities.hpp
00001 // ArduinoJson - arduinojson.org 00002 // Copyright Benoit Blanchon 2014-2021 00003 // MIT License 00004 00005 #pragma once 00006 00007 #include <ArduinoJson/Array/ArrayRef.hpp> 00008 #include <ArduinoJson/Document/JsonDocument.hpp> 00009 00010 namespace ARDUINOJSON_NAMESPACE { 00011 00012 // Copy a 1D array to a JsonArray 00013 template <typename T, size_t N, typename TDestination> 00014 inline typename enable_if<!is_array<T>::value && 00015 !is_base_of<JsonDocument, TDestination>::value, 00016 bool>::type 00017 copyArray(T (&src)[N], const TDestination& dst) { 00018 return copyArray(src, N, dst); 00019 } 00020 00021 // Copy a 1D array to a JsonDocument 00022 template <typename T, size_t N> 00023 inline bool copyArray(T (&src)[N], JsonDocument& dst) { 00024 return copyArray(src, dst.to<ArrayRef>()); 00025 } 00026 00027 // Copy a 1D array to a JsonArray 00028 template <typename T, typename TDestination> 00029 inline typename enable_if<!is_array<T>::value && 00030 !is_base_of<JsonDocument, TDestination>::value, 00031 bool>::type 00032 copyArray(T* src, size_t len, const TDestination& dst) { 00033 bool ok = true; 00034 for (size_t i = 0; i < len; i++) { 00035 ok &= dst.add(src[i]); 00036 } 00037 return ok; 00038 } 00039 00040 // Copy a 1D array to a JsonDocument 00041 template <typename T> 00042 inline bool copyArray(T* src, size_t len, JsonDocument& dst) { 00043 return copyArray(src, len, dst.to<ArrayRef>()); 00044 } 00045 00046 // Copy a 2D array to a JsonArray 00047 template <typename T, size_t N1, size_t N2, typename TDestination> 00048 inline typename enable_if<!is_base_of<JsonDocument, TDestination>::value, 00049 bool>::type 00050 copyArray(T (&src)[N1][N2], const TDestination& dst) { 00051 bool ok = true; 00052 for (size_t i = 0; i < N1; i++) { 00053 ArrayRef nestedArray = dst.createNestedArray(); 00054 for (size_t j = 0; j < N2; j++) { 00055 ok &= nestedArray.add(src[i][j]); 00056 } 00057 } 00058 return ok; 00059 } 00060 00061 // Copy a 2D array to a JsonDocument 00062 template <typename T, size_t N1, size_t N2> 00063 inline bool copyArray(T (&src)[N1][N2], JsonDocument& dst) { 00064 return copyArray(src, dst.to<ArrayRef>()); 00065 } 00066 00067 template <typename T> 00068 class ArrayCopier1D : public Visitor<size_t> { 00069 public: 00070 ArrayCopier1D(T* destination, size_t capacity) 00071 : _destination(destination), _capacity(capacity) {} 00072 00073 size_t visitArray(const CollectionData& array) { 00074 size_t size = 0; 00075 VariantSlot* slot = array.head(); 00076 00077 while (slot != 0 && size < _capacity) { 00078 _destination[size++] = variantAs<T>(slot->data()); 00079 slot = slot->next(); 00080 } 00081 return size; 00082 } 00083 00084 size_t visitObject(const CollectionData&) { 00085 return 0; 00086 } 00087 00088 size_t visitFloat(Float) { 00089 return 0; 00090 } 00091 00092 size_t visitString(const char*) { 00093 return 0; 00094 } 00095 00096 size_t visitRawJson(const char*, size_t) { 00097 return 0; 00098 } 00099 00100 size_t visitNegativeInteger(UInt) { 00101 return 0; 00102 } 00103 00104 size_t visitPositiveInteger(UInt) { 00105 return 0; 00106 } 00107 00108 size_t visitBoolean(bool) { 00109 return 0; 00110 } 00111 00112 size_t visitNull() { 00113 return 0; 00114 } 00115 00116 private: 00117 T* _destination; 00118 size_t _capacity; 00119 }; 00120 00121 template <typename T, size_t N1, size_t N2> 00122 class ArrayCopier2D : public Visitor<void> { 00123 public: 00124 ArrayCopier2D(T (*destination)[N1][N2]) : _destination(destination) {} 00125 00126 void visitArray(const CollectionData& array) { 00127 VariantSlot* slot = array.head(); 00128 size_t n = 0; 00129 while (slot != 0 && n < N1) { 00130 ArrayCopier1D<T> copier((*_destination)[n++], N2); 00131 variantAccept(slot->data(), copier); 00132 slot = slot->next(); 00133 } 00134 } 00135 void visitObject(const CollectionData&) {} 00136 void visitFloat(Float) {} 00137 void visitString(const char*) {} 00138 void visitRawJson(const char*, size_t) {} 00139 void visitNegativeInteger(UInt) {} 00140 void visitPositiveInteger(UInt) {} 00141 void visitBoolean(bool) {} 00142 void visitNull() {} 00143 00144 private: 00145 T (*_destination)[N1][N2]; 00146 size_t _capacity1, _capacity2; 00147 }; 00148 00149 // Copy a JsonArray to a 1D array 00150 template <typename TSource, typename T, size_t N> 00151 inline typename enable_if<!is_array<T>::value, size_t>::type copyArray( 00152 const TSource& src, T (&dst)[N]) { 00153 return copyArray(src, dst, N); 00154 } 00155 00156 // Copy a JsonArray to a 1D array 00157 template <typename TSource, typename T> 00158 inline size_t copyArray(const TSource& src, T* dst, size_t len) { 00159 ArrayCopier1D<T> copier(dst, len); 00160 00161 return src.accept(copier); 00162 } 00163 00164 // Copy a JsonArray to a 2D array 00165 template <typename TSource, typename T, size_t N1, size_t N2> 00166 inline void copyArray(const TSource& src, T (&dst)[N1][N2]) { 00167 ArrayCopier2D<T, N1, N2> copier(&dst); 00168 src.accept(copier); 00169 } 00170 00171 } // namespace ARDUINOJSON_NAMESPACE
Generated on Wed Jul 13 2022 01:10:37 by
1.7.2