diff options
author | Daniel Friesel <daniel.friesel@uos.de> | 2021-05-12 09:12:09 +0200 |
---|---|---|
committer | Daniel Friesel <daniel.friesel@uos.de> | 2021-05-12 09:12:09 +0200 |
commit | 39895a677e5d370824e702cfe90ebc67737b8482 (patch) | |
tree | ff5b4cc9e373d33a795d50d9333e05549bd9f2b8 /include/lib/ArduinoJson/Deserialization/Readers | |
parent | 42e7fdf01c3a5701bb51e93ad6c650c3dbbc5450 (diff) |
import ArduinoJson 6.18.0
Diffstat (limited to 'include/lib/ArduinoJson/Deserialization/Readers')
7 files changed, 257 insertions, 0 deletions
diff --git a/include/lib/ArduinoJson/Deserialization/Readers/ArduinoStreamReader.hpp b/include/lib/ArduinoJson/Deserialization/Readers/ArduinoStreamReader.hpp new file mode 100644 index 0000000..724638f --- /dev/null +++ b/include/lib/ArduinoJson/Deserialization/Readers/ArduinoStreamReader.hpp @@ -0,0 +1,31 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <Arduino.h> + +namespace ARDUINOJSON_NAMESPACE { + +template <typename TSource> +struct Reader<TSource, + typename enable_if<is_base_of<Stream, TSource>::value>::type> { + public: + explicit Reader(Stream& stream) : _stream(&stream) {} + + int read() { + // don't use _stream.read() as it ignores the timeout + char c; + return _stream->readBytes(&c, 1) ? static_cast<unsigned char>(c) : -1; + } + + size_t readBytes(char* buffer, size_t length) { + return _stream->readBytes(buffer, length); + } + + private: + Stream* _stream; +}; + +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Deserialization/Readers/ArduinoStringReader.hpp b/include/lib/ArduinoJson/Deserialization/Readers/ArduinoStringReader.hpp new file mode 100644 index 0000000..71571d4 --- /dev/null +++ b/include/lib/ArduinoJson/Deserialization/Readers/ArduinoStringReader.hpp @@ -0,0 +1,17 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +namespace ARDUINOJSON_NAMESPACE { + +template <typename TSource> +struct Reader<TSource, + typename enable_if<is_base_of< ::String, TSource>::value>::type> + : BoundedReader<const char*> { + explicit Reader(const ::String& s) + : BoundedReader<const char*>(s.c_str(), s.length()) {} +}; + +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Deserialization/Readers/FlashReader.hpp b/include/lib/ArduinoJson/Deserialization/Readers/FlashReader.hpp new file mode 100644 index 0000000..7eca134 --- /dev/null +++ b/include/lib/ArduinoJson/Deserialization/Readers/FlashReader.hpp @@ -0,0 +1,53 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +namespace ARDUINOJSON_NAMESPACE { + +template <> +struct Reader<const __FlashStringHelper*, void> { + const char* _ptr; + + public: + explicit Reader(const __FlashStringHelper* ptr) + : _ptr(reinterpret_cast<const char*>(ptr)) {} + + int read() { + return pgm_read_byte(_ptr++); + } + + size_t readBytes(char* buffer, size_t length) { + memcpy_P(buffer, _ptr, length); + _ptr += length; + return length; + } +}; + +template <> +struct BoundedReader<const __FlashStringHelper*, void> { + const char* _ptr; + const char* _end; + + public: + explicit BoundedReader(const __FlashStringHelper* ptr, size_t size) + : _ptr(reinterpret_cast<const char*>(ptr)), _end(_ptr + size) {} + + int read() { + if (_ptr < _end) + return pgm_read_byte(_ptr++); + else + return -1; + } + + size_t readBytes(char* buffer, size_t length) { + size_t available = static_cast<size_t>(_end - _ptr); + if (available < length) + length = available; + memcpy_P(buffer, _ptr, length); + _ptr += length; + return length; + } +}; +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Deserialization/Readers/IteratorReader.hpp b/include/lib/ArduinoJson/Deserialization/Readers/IteratorReader.hpp new file mode 100644 index 0000000..37c3c31 --- /dev/null +++ b/include/lib/ArduinoJson/Deserialization/Readers/IteratorReader.hpp @@ -0,0 +1,43 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +namespace ARDUINOJSON_NAMESPACE { + +template <typename TIterator> +class IteratorReader { + TIterator _ptr, _end; + + public: + explicit IteratorReader(TIterator begin, TIterator end) + : _ptr(begin), _end(end) {} + + int read() { + if (_ptr < _end) + return static_cast<unsigned char>(*_ptr++); + else + return -1; + } + + size_t readBytes(char* buffer, size_t length) { + size_t i = 0; + while (i < length && _ptr < _end) buffer[i++] = *_ptr++; + return i; + } +}; + +template <typename T> +struct void_ { + typedef void type; +}; + +template <typename TSource> +struct Reader<TSource, typename void_<typename TSource::const_iterator>::type> + : IteratorReader<typename TSource::const_iterator> { + explicit Reader(const TSource& source) + : IteratorReader<typename TSource::const_iterator>(source.begin(), + source.end()) {} +}; +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Deserialization/Readers/RamReader.hpp b/include/lib/ArduinoJson/Deserialization/Readers/RamReader.hpp new file mode 100644 index 0000000..67cf682 --- /dev/null +++ b/include/lib/ArduinoJson/Deserialization/Readers/RamReader.hpp @@ -0,0 +1,50 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Polyfills/type_traits.hpp> + +namespace ARDUINOJSON_NAMESPACE { + +template <typename T> +struct IsCharOrVoid { + static const bool value = + is_same<T, void>::value || is_same<T, char>::value || + is_same<T, unsigned char>::value || is_same<T, signed char>::value; +}; + +template <typename T> +struct IsCharOrVoid<const T> : IsCharOrVoid<T> {}; + +template <typename TSource> +struct Reader<TSource*, + typename enable_if<IsCharOrVoid<TSource>::value>::type> { + const char* _ptr; + + public: + explicit Reader(const void* ptr) + : _ptr(ptr ? reinterpret_cast<const char*>(ptr) : "") {} + + int read() { + return static_cast<unsigned char>(*_ptr++); + } + + size_t readBytes(char* buffer, size_t length) { + for (size_t i = 0; i < length; i++) buffer[i] = *_ptr++; + return length; + } +}; + +template <typename TSource> +struct BoundedReader<TSource*, + typename enable_if<IsCharOrVoid<TSource>::value>::type> + : public IteratorReader<const char*> { + public: + explicit BoundedReader(const void* ptr, size_t len) + : IteratorReader<const char*>(reinterpret_cast<const char*>(ptr), + reinterpret_cast<const char*>(ptr) + len) {} +}; + +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Deserialization/Readers/StdStreamReader.hpp b/include/lib/ArduinoJson/Deserialization/Readers/StdStreamReader.hpp new file mode 100644 index 0000000..eebaa2c --- /dev/null +++ b/include/lib/ArduinoJson/Deserialization/Readers/StdStreamReader.hpp @@ -0,0 +1,29 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <istream> + +namespace ARDUINOJSON_NAMESPACE { + +template <typename TSource> +struct Reader<TSource, typename enable_if< + is_base_of<std::istream, TSource>::value>::type> { + public: + explicit Reader(std::istream& stream) : _stream(&stream) {} + + int read() { + return _stream->get(); + } + + size_t readBytes(char* buffer, size_t length) { + _stream->read(buffer, static_cast<std::streamsize>(length)); + return static_cast<size_t>(_stream->gcount()); + } + + private: + std::istream* _stream; +}; +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Deserialization/Readers/VariantReader.hpp b/include/lib/ArduinoJson/Deserialization/Readers/VariantReader.hpp new file mode 100644 index 0000000..e56e262 --- /dev/null +++ b/include/lib/ArduinoJson/Deserialization/Readers/VariantReader.hpp @@ -0,0 +1,34 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Object/MemberProxy.hpp> +#include <ArduinoJson/Variant/VariantRef.hpp> + +namespace ARDUINOJSON_NAMESPACE { + +template <typename TArray> +struct Reader<ElementProxy<TArray>, void> : Reader<char*, void> { + explicit Reader(const ElementProxy<TArray>& x) + : Reader<char*, void>(x.template as<const char*>()) {} +}; + +template <typename TObject, typename TStringRef> +struct Reader<MemberProxy<TObject, TStringRef>, void> : Reader<char*, void> { + explicit Reader(const MemberProxy<TObject, TStringRef>& x) + : Reader<char*, void>(x.template as<const char*>()) {} +}; + +template <> +struct Reader<VariantRef, void> : Reader<char*, void> { + explicit Reader(VariantRef x) : Reader<char*, void>(x.as<const char*>()) {} +}; + +template <> +struct Reader<VariantConstRef, void> : Reader<char*, void> { + explicit Reader(VariantConstRef x) + : Reader<char*, void>(x.as<const char*>()) {} +}; +} // namespace ARDUINOJSON_NAMESPACE |