CBMC
json_streamt Class Referenceabstract

This class provides a facility for streaming JSON objects directly to the output instead of waiting for the object to be fully formed in memory and then print it (as done using jsont). More...

#include <json_stream.h>

+ Inheritance diagram for json_streamt:
+ Collaboration diagram for json_streamt:

Public Member Functions

void close ()
 Outputs the current current child stream and closes this JSON stream. More...
 
virtual ~json_streamt ()=default
 

Protected Types

typedef std::map< std::string, jsontobjectt
 Non-streaming JSON elements These will be printed when closing the stream or creating a child stream. More...
 

Protected Member Functions

 json_streamt (std::ostream &_out, unsigned _indent)
 Constructor to be used by derived classes. More...
 
json_stream_arraytcreate_child_stream_array ()
 Create a new JSON array child stream. More...
 
json_stream_objecttcreate_child_stream_object ()
 Create a new JSON object child stream. More...
 
void output_delimiter ()
 Outputs the delimiter between JSON elements. More...
 
virtual void output_child_stream ()=0
 
virtual void output_finalizer ()=0
 

Protected Attributes

bool open
 Denotes whether the current stream is open or has been invalidated. More...
 
std::ostream & out
 
unsigned indent
 
bool first
 Is the current element the first element in the object or array? This is required to know whether a delimiter must be output or not. More...
 
objectt object
 
std::unique_ptr< json_streamtchild_stream
 The current child stream. More...
 

Detailed Description

This class provides a facility for streaming JSON objects directly to the output instead of waiting for the object to be fully formed in memory and then print it (as done using jsont).

This reduces memory consumption when preparing e.g. large traces for output.

json_streamt is the base class from which the classes for streaming arrays (json_stream_arrayt) and objects (json_stream_objectt) are derived. json_streamt wraps an output stream and stores the current element to be output. This can be either a child stream (of type json_streamt that outputs JSON objects incrementally) or a non-streaming JSON object (of type jsont that will be output as a whole) To ensure consistency of the output, a class invariant is that there is at most one child stream at any time. For this reason, the existing child stream is flushed and closed when creating a new child stream.

Definition at line 36 of file json_stream.h.

Member Typedef Documentation

◆ objectt

typedef std::map<std::string, jsont> json_streamt::objectt
protected

Non-streaming JSON elements These will be printed when closing the stream or creating a child stream.

Definition at line 72 of file json_stream.h.

Constructor & Destructor Documentation

◆ ~json_streamt()

virtual json_streamt::~json_streamt ( )
virtualdefault

◆ json_streamt()

json_streamt::json_streamt ( std::ostream &  _out,
unsigned  _indent 
)
inlineprotected

Constructor to be used by derived classes.

Parameters
_outoutput stream
_indentindentation level

Definition at line 56 of file json_stream.h.

Member Function Documentation

◆ close()

void json_streamt::close ( )
inline

Outputs the current current child stream and closes this JSON stream.

Definition at line 40 of file json_stream.h.

◆ create_child_stream_array()

json_stream_arrayt & json_streamt::create_child_stream_array ( )
protected

Create a new JSON array child stream.

Definition at line 66 of file json_stream.cpp.

◆ create_child_stream_object()

json_stream_objectt & json_streamt::create_child_stream_object ( )
protected

Create a new JSON object child stream.

Definition at line 74 of file json_stream.cpp.

◆ output_child_stream()

virtual void json_streamt::output_child_stream ( )
protectedpure virtual

◆ output_delimiter()

void json_streamt::output_delimiter ( )
protected

Outputs the delimiter between JSON elements.

Output the element delimiter to the output stream.

Definition at line 14 of file json_stream.cpp.

◆ output_finalizer()

virtual void json_streamt::output_finalizer ( )
protectedpure virtual

Member Data Documentation

◆ child_stream

std::unique_ptr<json_streamt> json_streamt::child_stream
protected

The current child stream.

One can create and close many child streams sequentially (timewise), e.g. for each element in an array or each property in an object. The invariant is that there can only be at most child one stream at a time.

Definition at line 79 of file json_stream.h.

◆ first

bool json_streamt::first
protected

Is the current element the first element in the object or array? This is required to know whether a delimiter must be output or not.

Definition at line 68 of file json_stream.h.

◆ indent

unsigned json_streamt::indent
protected

Definition at line 64 of file json_stream.h.

◆ object

objectt json_streamt::object
protected

Definition at line 73 of file json_stream.h.

◆ open

bool json_streamt::open
protected

Denotes whether the current stream is open or has been invalidated.

Definition at line 62 of file json_stream.h.

◆ out

std::ostream& json_streamt::out
protected

Definition at line 63 of file json_stream.h.


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