Movatterモバイル変換


[0]ホーム

URL:


serializeJson()

Description

serializeJson() serializes aJsonDocument to create aminified JSON document, i.e. a document without spaces or line break between values.

If you want aprettified JSON document, useserializeJsonPretty()

Signatures

size_tserializeJson(TSourcesrc,char*output,size_toutputSize);// see remark 1size_tserializeJson(TSourcesrc,charoutput[N]);// see remark 1size_tserializeJson(TSourcesrc,Print&output);size_tserializeJson(TSourcesrc,String&output);size_tserializeJson(TSourcesrc,std::string&output);size_tserializeJson(TSourcesrc,std::ostream&output);template<typenameWriter>// custom writer class (see below)size_tserializeJson(TSourcesrc,Writer&output);

These overloads only add the null terminator if there is enough room in the buffer; this is the same caveat asstrncpy().
Therefore, make sure that the buffer is large enough or check the return value.

The two first overloads supportunsigned char as well.

Arguments

Becauseoutput can be any implementation ofPrint, you can uses instances likeSerial,EthernetClient,WifiClient

Return value

The number of bytes written, excluding the null terminator (char* andchar[N] overloads only).

Configuration

You can configureserializeJson() with the following settings:

How to view the JSON output?

When you pass aStream toserializeJson(), 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 whatserializeJson() writes, useWriteLoggingStream from theStreamUtils library. See the example below.

Performance

When you pass aStream toserializeJson(), it sends the bytes one by one, which can be slow depending on the target stream. For example, if you send to aWiFiClient on anESP8266, 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 useWriteBufferingStream from theStreamUtils library. See the example below.

Custom writer

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:

structCustomWriter{// Writes one byte, returns the number of bytes written (0 or 1)size_twrite(uint8_tc);// Writes several bytes, returns the number of bytes writtensize_twrite(constuint8_t*buffer,size_tlength);};

Then, pass a reference to an instance of this class as the second argument ofserializeJson().

Example

Write the JSON document to the serial port

JsonDocumentdoc;doc["hello"]="world";serializeJson(doc,Serial);

will write the following string to the serial port:

{"hello":"world"}

Write to a file

Filefile=SD.open(filename,FILE_WRITE);JsonDocumentdoc;doc["hello"]="world";serializeJson(doc,file);file.close();

Write to a file and print to the serial port

This example requires theStreamUtils library; it uses theWriteLoggingStream decorator that adds logging capability to an existingStream.

Filefile=SD.open(filename,FILE_WRITE);WriteLoggingStreamloggedFile(file,Serial);doc["hello"]="world";serializeJson(doc,loggedFile);file.close();

The second line create a newStream that forward every calls tofile and duplicate writes calls toSerial.

Improve file write performance

This example requires theStreamUtils library; it uses theWriteBufferingStream decorator that adds buffering capability to an existingStream.

Filefile=SD.open(filename,FILE_WRITE);WriteBufferingStreambufferedFile(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 also

Global warming stripes by Professor Ed Hawkins (University of Reading)

[8]ページ先頭

©2009-2025 Movatter.jp