serializeJson()
serializes a JsonDocument
to create a minified JSON document, i.e. a document without spaces or line break between values.
If you want a prettified JSON document, use serializeJsonPretty()
size_t serializeJson(TSource src, char* output, size_t outputSize); // see remark 1
size_t serializeJson(TSource src, char output[N]); // see remark 1
size_t serializeJson(TSource src, Print& output);
size_t serializeJson(TSource src, String& output);
size_t serializeJson(TSource src, std::string& output);
size_t serializeJson(TSource src, std::ostream& output);
template<typename Writer> // custom writer class (see below)
size_t serializeJson(TSource src, Writer& output);
➀ These overloads only add the null terminator if there is enough room in the buffer; this is the same caveat as strncpy()
.
Therefore, make sure that the buffer is large enough or check the return value.
The two first overloads support unsigned char
as well.
src
: the value to serialize. It can be a JsonDocument
, JsonArray
, JsonArrayConst
, JsonObject
, JsonObjectConst
, JsonVariant
, or JsonVariantConst
.output
: the destination buffer where the result should be writtenoutputSize
: the capacity of the destination bufferBecause output
can be any implementation of Print
, you can uses instances like Serial
, EthernetClient
, WifiClient
…
The number of bytes written, excluding the null terminator (char*
and char[N]
overloads only).
You can configure serializeJson()
with the following settings:
ARDUINOJSON_ENABLE_NAN
writes NaN
instead of null
,ARDUINOJSON_ENABLE_INFINITY
writes Infinity
instead of null
.When you pass a Stream
to serializeJson()
, it writes the JSON to the stream but doesn’t print anything to the serial port, which makes troubleshooting difficult.
If you want to see what serializeJson()
writes, use WriteLoggingStream
from the StreamUtils library. See the example below.
When you pass a Stream
to serializeJson()
, it sends the bytes one by one, which can be slow depending on the target stream. For example, if you send to a WiFiClient
on an ESP8266, it will send a packet over the air for each byte, which is terribly slow and inefficient. To improve speed and efficiency, we must send fewer, larger packets.
To write the JSON document in chunks, you can use WriteBufferingStream
from the StreamUtils library. See the example below.
If none of the supported output types is suitable for you, you can implement a custom writer class. This class must implement two member functions, as shown below:
struct CustomWriter {
// Writes one byte, returns the number of bytes written (0 or 1)
size_t write(uint8_t c);
// Writes several bytes, returns the number of bytes written
size_t write(const uint8_t *buffer, size_t length);
};
Then, pass a reference to an instance of this class as the second argument of serializeJson()
.
JsonDocument doc;
doc["hello"] = "world";
serializeJson(doc, Serial);
will write the following string to the serial port:
Write to a fileFile file = SD.open(filename, FILE_WRITE);
JsonDocument doc;
doc["hello"] = "world";
serializeJson(doc, file);
file.close();
Write to a file and print to the serial port
This example requires the StreamUtils library; it uses the WriteLoggingStream
decorator that adds logging capability to an existing Stream
.
File file = SD.open(filename, FILE_WRITE);
WriteLoggingStream loggedFile(file, Serial);
doc["hello"] = "world";
serializeJson(doc, loggedFile);
file.close();
The second line create a new Stream
that forward every calls to file
and duplicate writes calls to Serial
.
This example requires the StreamUtils library; it uses the WriteBufferingStream
decorator that adds buffering capability to an existing Stream
.
File file = SD.open(filename, FILE_WRITE);
WriteBufferingStream bufferedFile(file, 64);
doc["hello"] = "world";
serializeJson(doc, bufferedFile);
bufferedFile.flush();
file.close();
The first line creates the file and the second line adds a 64 byte buffer in front of the file. The next to last line writes the remaining bytes in the file.
See alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4