REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
utils::StreamingJson Class Reference

The Streaming JSON API provides a way of constructing (writing) JSON messages without RAM overhead. More...

#include <streaming_json.h>

Public Member Functions

 StreamingJson (Print &output)
 
void check_comma ()
 
void needs_comma ()
 
void begin_dict ()
 
void end_dict ()
 
void begin_list ()
 
void end_list ()
 
void begin_str (char quote='"')
 
void end_str (char quote='"')
 
void key (const char *str, char quote='"')
 
void key (const std::string &str)
 
void val (const char *str, char quote='"')
 
void val (const std::string &str)
 
void val (bool b)
 
template<typename V >
void val (V i)
 
void null ()
 
void json (const char *str)
 An embedded Raw json structure.
 
template<typename V >
void kv (const char *_key, V _val)
 
void endln ()
 

Public Attributes

Print & output
 

Detailed Description

The Streaming JSON API provides a way of constructing (writing) JSON messages without RAM overhead.

In contrast to ArduinoJSON, there is no document which needs to be represented in memory and which is setup before serialization. Instead, serialization and printing happens here at method call time. Ideally, this uses the Arduino Print infrastructure and thus avoids any string representations where possible.

The class stores only a minimal state of parsing. JSON does not allow trailing commas so this is avoided with a simple state boolean. The lib is not aware of whether it constructs a map/dictionary or a list/array. It is up to the user to make sure the dict follows the key-value-key-value semantics.

Correct usage example:

StreamingJson s(some_client_or_so); s.begin_dict(); s.key("foo"); s.val("bar"); s.key("biz"); s.begin_list(); s.val(123); s.val(true); s.val(3.14); s.null(); s.end_list(); s.end_dict();

should create the following JSON document:

{"foo":"bar","biz":[123, true, 3.14, null]}

Definition at line 44 of file streaming_json.h.

Constructor & Destructor Documentation

◆ StreamingJson()

utils::StreamingJson::StreamingJson ( Print & output)
inline

Definition at line 49 of file streaming_json.h.

Member Function Documentation

◆ begin_dict()

void utils::StreamingJson::begin_dict ( )
inline

◆ begin_list()

void utils::StreamingJson::begin_list ( )
inline

◆ begin_str()

void utils::StreamingJson::begin_str ( char quote = '"')
inline

Definition at line 62 of file streaming_json.h.

References output.

Referenced by key(), val(), and msg::StreamLogger::write().

◆ check_comma()

void utils::StreamingJson::check_comma ( )
inline

Definition at line 51 of file streaming_json.h.

References output.

Referenced by client::StreamingRunDataNotificationHandler::handle(), json(), key(), null(), val(), val(), and val().

◆ end_dict()

void utils::StreamingJson::end_dict ( )
inline

◆ end_list()

void utils::StreamingJson::end_list ( )
inline

◆ end_str()

void utils::StreamingJson::end_str ( char quote = '"')
inline

Definition at line 63 of file streaming_json.h.

References output.

Referenced by key(), val(), and msg::StreamLogger::write().

◆ endln()

void utils::StreamingJson::endln ( )
inline

Definition at line 118 of file streaming_json.h.

References output.

Referenced by debug_print_json(), and client::StreamingRunDataNotificationHandler::handle().

◆ json()

void utils::StreamingJson::json ( const char * str)
inline

An embedded Raw json structure.

Definition at line 108 of file streaming_json.h.

References check_comma(), needs_comma(), and output.

Referenced by client::StreamingRunDataNotificationHandler::handle(), and msg::StartupLog::stream_to_json().

◆ key() [1/2]

void utils::StreamingJson::key ( const char * str,
char quote = '"' )
inline

◆ key() [2/2]

void utils::StreamingJson::key ( const std::string & str)
inline

Definition at line 73 of file streaming_json.h.

References key().

Referenced by key().

◆ kv()

template<typename V >
void utils::StreamingJson::kv ( const char * _key,
V _val )
inline

◆ needs_comma()

void utils::StreamingJson::needs_comma ( )
inline

◆ null()

void utils::StreamingJson::null ( )
inline

Definition at line 101 of file streaming_json.h.

References check_comma(), needs_comma(), and output.

◆ val() [1/4]

void utils::StreamingJson::val ( bool b)
inline

Definition at line 87 of file streaming_json.h.

References check_comma(), needs_comma(), and output.

◆ val() [2/4]

void utils::StreamingJson::val ( const char * str,
char quote = '"' )
inline

◆ val() [3/4]

void utils::StreamingJson::val ( const std::string & str)
inline

Definition at line 85 of file streaming_json.h.

References val().

Referenced by val().

◆ val() [4/4]

template<typename V >
void utils::StreamingJson::val ( V i)
inline

Definition at line 95 of file streaming_json.h.

References check_comma(), needs_comma(), and output.

Member Data Documentation

◆ output

Print& utils::StreamingJson::output

The documentation for this class was generated from the following file: