JsonObjectStream.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * JsonObjectStream.h - Stream implementation for ArduinoJson Version 6
8  *
9  ****/
10 
11 #pragma once
12 
14 #include "ArduinoJson.h"
15 
23 {
24 public:
29  JsonObjectStream(Json::SerializationFormat format, size_t capacity = 1024) : doc(capacity), format(format)
30  {
31  doc.to<JsonObject>();
32  }
33 
37  JsonObjectStream(size_t capacity = 1024) : doc(capacity)
38  {
39  doc.to<JsonObject>();
40  }
41 
42  //Use base class documentation
43  StreamType getStreamType() const override
44  {
45  return eSST_JsonObject;
46  }
47 
51  JsonObject getRoot()
52  {
53  return doc.as<JsonObject>();
54  }
55 
56  //Use base class documentation
57  uint16_t readMemoryBlock(char* data, int bufSize) override
58  {
59  getData();
60  return MemoryDataStream::readMemoryBlock(data, bufSize);
61  }
62 
67  int available() override
68  {
69  getData();
70  return (doc.isNull() ? 0 : Json::measure(doc, format));
71  }
72 
73  bool isFinished() override
74  {
75  getData();
77  }
78 
79 private:
80  void getData()
81  {
82  if(send && !doc.isNull()) {
83  Json::serialize(doc, this, format);
84  send = false;
85  }
86  }
87 
88 private:
89  DynamicJsonDocument doc;
91  bool send = true;
92 };
93 
JsonObject stream class.
Definition: JsonObjectStream.h:23
bool isFinished() override
Check if all data has been read.
Definition: JsonObjectStream.h:73
int available() override
Return the total length of the stream.
Definition: JsonObjectStream.h:67
StreamType getStreamType() const override
Get the stream type.
Definition: JsonObjectStream.h:43
JsonObjectStream(Json::SerializationFormat format, size_t capacity=1024)
Create a JSON object stream with a specific format.
Definition: JsonObjectStream.h:29
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
Definition: JsonObjectStream.h:57
JsonObjectStream(size_t capacity=1024)
Create a JSON object stream using default (Compact) format.
Definition: JsonObjectStream.h:37
JsonObject getRoot()
Get the JSON root node.
Definition: JsonObjectStream.h:51
Read/write stream using expandable memory buffer.
Definition: MemoryDataStream.h:27
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
bool isFinished() override
Check if all data has been read.
Definition: MemoryDataStream.h:78
StreamType
Data stream type.
Definition: DataSourceStream.h:25
@ eSST_JsonObject
JSON object data stream.
Definition: DataSourceStream.h:32
size_t measure(const TSource &source, SerializationFormat format=JSON_FORMAT_DEFAULT)
Compute the size of a serialized Json object for a specified format.
Definition: ArduinoJson.h:132
size_t serialize(const TSource &source, TDestination &destination, SerializationFormat format=JSON_FORMAT_DEFAULT)
Write a Json object in a specified format.
Definition: ArduinoJson.h:160
SerializationFormat
Describes format of serialized Json object.
Definition: ArduinoJson.h:114
@ Compact
Compact JSON format.
Definition: ArduinoJson.h:115