mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	* Added DynamicJsonArray and StaticJsonArray * Added DynamicJsonObject and StaticJsonObject * Added DynamicJsonVariant and StaticJsonVariant * Added deserializeJson() * Removed JsonBuffer::parseArray(), parseObject() and parse() * Removed JsonBuffer::createArray() and createObject()
		
			
				
	
	
		
			130 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			130 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| // ArduinoJson - arduinojson.org
 | |
| // Copyright Benoit Blanchon 2014-2018
 | |
| // MIT License
 | |
| 
 | |
| #pragma once
 | |
| 
 | |
| #include "JsonBuffer.hpp"
 | |
| #include "TypeTraits/Max.hpp"
 | |
| 
 | |
| namespace ArduinoJson {
 | |
| namespace Internals {
 | |
| 
 | |
| class StaticJsonBufferBase : public JsonBuffer {
 | |
|  public:
 | |
|   class String {
 | |
|    public:
 | |
|     String(StaticJsonBufferBase* parent) : _parent(parent) {
 | |
|       _start = parent->_buffer + parent->_size;
 | |
|     }
 | |
| 
 | |
|     void append(char c) {
 | |
|       if (_parent->canAlloc(1)) {
 | |
|         char* last = static_cast<char*>(_parent->doAlloc(1));
 | |
|         *last = c;
 | |
|       }
 | |
|     }
 | |
| 
 | |
|     const char* c_str() const {
 | |
|       if (_parent->canAlloc(1)) {
 | |
|         char* last = static_cast<char*>(_parent->doAlloc(1));
 | |
|         *last = '\0';
 | |
|         return _start;
 | |
|       } else {
 | |
|         return NULL;
 | |
|       }
 | |
|     }
 | |
| 
 | |
|    private:
 | |
|     StaticJsonBufferBase* _parent;
 | |
|     char* _start;
 | |
|   };
 | |
| 
 | |
|   StaticJsonBufferBase(char* buffer, size_t capa)
 | |
|       : _buffer(buffer), _capacity(capa), _size(0) {}
 | |
| 
 | |
|   // Gets the capacity of the buffer in bytes
 | |
|   size_t capacity() const {
 | |
|     return _capacity;
 | |
|   }
 | |
| 
 | |
|   // Gets the current usage of the buffer in bytes
 | |
|   size_t size() const {
 | |
|     return _size;
 | |
|   }
 | |
| 
 | |
|   // Allocates the specified amount of bytes in the buffer
 | |
|   virtual void* alloc(size_t bytes) {
 | |
|     alignNextAlloc();
 | |
|     if (!canAlloc(bytes)) return NULL;
 | |
|     return doAlloc(bytes);
 | |
|   }
 | |
| 
 | |
|   // Resets the buffer.
 | |
|   // USE WITH CAUTION: this invalidates all previously allocated data
 | |
|   void clear() {
 | |
|     _size = 0;
 | |
|   }
 | |
| 
 | |
|   String startString() {
 | |
|     return String(this);
 | |
|   }
 | |
| 
 | |
|  protected:
 | |
|   ~StaticJsonBufferBase() {}
 | |
| 
 | |
|  private:
 | |
|   void alignNextAlloc() {
 | |
|     _size = round_size_up(_size);
 | |
|   }
 | |
| 
 | |
|   bool canAlloc(size_t bytes) const {
 | |
|     return _size + bytes <= _capacity;
 | |
|   }
 | |
| 
 | |
|   void* doAlloc(size_t bytes) {
 | |
|     void* p = &_buffer[_size];
 | |
|     _size += bytes;
 | |
|     return p;
 | |
|   }
 | |
| 
 | |
|   char* _buffer;
 | |
|   size_t _capacity;
 | |
|   size_t _size;
 | |
| };
 | |
| }  // namespace Internals
 | |
| 
 | |
| #if defined(__clang__)
 | |
| #pragma clang diagnostic push
 | |
| #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
 | |
| #elif defined(__GNUC__)
 | |
| #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
 | |
| #pragma GCC diagnostic push
 | |
| #endif
 | |
| #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
 | |
| #endif
 | |
| 
 | |
| // Implements a JsonBuffer with fixed memory allocation.
 | |
| // The template paramenter CAPACITY specifies the capacity of the buffer in
 | |
| // bytes.
 | |
| template <size_t CAPACITY>
 | |
| class StaticJsonBuffer : public Internals::StaticJsonBufferBase {
 | |
|   static const size_t ACTUAL_CAPACITY = Internals::Max<1, CAPACITY>::value;
 | |
| 
 | |
|  public:
 | |
|   explicit StaticJsonBuffer()
 | |
|       : Internals::StaticJsonBufferBase(_buffer, ACTUAL_CAPACITY) {}
 | |
| 
 | |
|  private:
 | |
|   char _buffer[ACTUAL_CAPACITY];
 | |
| };
 | |
| }  // namespace ArduinoJson
 | |
| 
 | |
| #if defined(__clang__)
 | |
| #pragma clang diagnostic pop
 | |
| #elif defined(__GNUC__)
 | |
| #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
 | |
| #pragma GCC diagnostic pop
 | |
| #endif
 | |
| #endif
 |