Rosetta
Public Member Functions | Protected Member Functions | Private Member Functions | List of all members
basic::JsonEmitter Class Reference

Lightweight class to ease writting JSON documents, a subset of YAML (http://json.org) More...

#include <Emitter.hh>

Inheritance diagram for basic::JsonEmitter:
Inheritance graph
[legend]

Public Member Functions

 JsonEmitter (std::ostream &out)
 
 ~JsonEmitter () override
 
void start_doc () override
 JSON doesn't support multiple documents, so this just calls end(1) to return you to the top (least-nested) level. More...
 
- Public Member Functions inherited from basic::Emitter
 Emitter (std::ostream &out)
 
 ~Emitter () override
 
void flush ()
 Flush the underlying output stream. More...
 
template<typename T >
void write (T const &data)
 Write method for use inside lists / arrays. More...
 
void start_list (bool indent=true)
 Write method for use inside lists / arrays. More...
 
void start_map (bool indent=true)
 Write method for use inside lists / arrays. More...
 
template<typename T >
void write (std::string const &label, T const &data)
 Write method for use inside maps / dicts / objects. More...
 
void start_list (std::string const &label, bool indent=true)
 Write method for use inside maps / dicts / objects. More...
 
void start_list (const char *label, bool indent=true)
 Write method for use inside maps / dicts / objects. More...
 
void start_map (std::string const &label, bool indent=true)
 Write method for use inside maps / dicts / objects. More...
 
void start_map (const char *label, bool indent=true)
 Write method for use inside maps / dicts / objects. More...
 
void end_list ()
 Counterpart to start_list() – writes closing bracket. More...
 
void end_map ()
 Counterpart to start_map() – writes closing brace. More...
 
void end (size_t desired_depth=0)
 By default, closes all open maps/lists, ending the document. More...
 
int depth () const
 Number of closing brackets/braces required to end document (non-negative) More...
 
void set_indent (int num_spaces)
 Number of spaces used for indenting. Default is one. More...
 
- Public Member Functions inherited from utility::VirtualBase
 VirtualBase ()=default
 Default constructor. More...
 
virtual ~VirtualBase ()=default
 The virtual destructor is one of the main reasons for the VirtualBase class. More...
 
 VirtualBase (VirtualBase const &)=default
 
 VirtualBase (VirtualBase &&)=default
 
VirtualBaseoperator= (VirtualBase const &)=default
 
VirtualBaseoperator= (VirtualBase &&)=default
 

Protected Member Functions

std::string quote_string (std::string const &s) override
 JSON always quotes strings, regardless. More...
 
void do_indent (bool write_comma=true) override
 Handle pretty-printing indentation. Don't want to use commas for opening/closing brace/bracket. More...
 
void write_list_marker () override
 JSON has no special marker for list items. More...
 
void start_raw (bool is_map, bool indent) override
 
void end_raw () override
 
- Protected Member Functions inherited from basic::Emitter
 Emitter ()
 
 Emitter (Emitter const &)
 
bool assert_in (bool in_map, std::string const &msg)
 Check that we're in the expected context (either map or list) More...
 
void write_label (std::string const &label)
 Write the key part of a key-value pair. More...
 
void write_raw (bool b)
 
void write_raw (int i)
 
void write_raw (long l)
 
void write_raw (float f)
 
void write_raw (double d)
 
void write_raw (std::string const &s)
 
std::string escape_string (std::string const &s, bool &needs_quotes_out)
 Converts special characters (newlines, etc) to escape codes (
, etc). More...
 

Private Member Functions

 JsonEmitter ()
 
 JsonEmitter (JsonEmitter const &)
 

Additional Inherited Members

- Protected Attributes inherited from basic::Emitter
std::ostream & out_
 
std::string indent_str_
 
std::vector< bool > in_map_
 
std::vector< bool > first_
 
std::vector< bool > indent_
 
std::vector< int > indent_depth_
 

Detailed Description

Lightweight class to ease writting JSON documents, a subset of YAML (http://json.org)

Using this class is not recommended – use YamlEmitter instead.

JSON is JavaScript Object Notation, the format for object and array literals in the JavaScript language. It also looks a great deal like nested Python dicts and lists, except for the special JSON values true, false, and null. (Python uses True, False, and None instead. Otherwise, JSON can be embedded directly into Python scripts.)

JSON is legal subset of YAML, but leaves out much of YAML's complexity and advanced features. At the present time, there appears to be more library support for JSON than for YAML, especially for reading and parsing. (Both are fairly easy to write, as demonstrated here.)

The topmost level of every valid JSON document consists of exactly one map, which is automatically started for you when the class is initialized. When you're finished you should call end(), and then probably flush().

Whitespace is JSON documents is not significant. By default, this class will try to pretty-print, showing the depth of nesting through indentation. When starting a new list or map, you can request that it not be indented (i.e., be printed all on one line), but all lists and maps nested inside it will also be printed without linebreaks, regardless of the requested indentation.

Constructor & Destructor Documentation

◆ JsonEmitter() [1/3]

basic::JsonEmitter::JsonEmitter ( std::ostream &  out)
inline

References start_raw().

◆ ~JsonEmitter()

basic::JsonEmitter::~JsonEmitter ( )
inlineoverride

◆ JsonEmitter() [2/3]

basic::JsonEmitter::JsonEmitter ( )
private

◆ JsonEmitter() [3/3]

basic::JsonEmitter::JsonEmitter ( JsonEmitter const &  )
private

Member Function Documentation

◆ do_indent()

void basic::JsonEmitter::do_indent ( bool  write_comma = true)
inlineoverrideprotectedvirtual

Handle pretty-printing indentation. Don't want to use commas for opening/closing brace/bracket.

Implements basic::Emitter.

References basic::Emitter::depth(), create_a3b_hbs::first, basic::Emitter::first_, create_a3b_hbs::i, binder::indent(), basic::Emitter::indent_, basic::Emitter::indent_depth_, basic::Emitter::indent_str_, and basic::Emitter::out_.

Referenced by end_raw().

◆ end_raw()

void basic::JsonEmitter::end_raw ( )
inlineoverrideprotectedvirtual

JSON always uses brackets and braces, regardless of whether we're indenting.

Implements basic::Emitter.

References do_indent(), basic::Emitter::first_, basic::Emitter::in_map_, basic::Emitter::indent_, basic::Emitter::indent_depth_, and basic::Emitter::out_.

◆ quote_string()

std::string basic::JsonEmitter::quote_string ( std::string const &  s)
inlineoverrideprotectedvirtual

JSON always quotes strings, regardless.

Implements basic::Emitter.

References basic::Emitter::escape_string(), docking::s, and predPRE::t.

◆ start_doc()

void basic::JsonEmitter::start_doc ( )
inlineoverridevirtual

JSON doesn't support multiple documents, so this just calls end(1) to return you to the top (least-nested) level.

Implements basic::Emitter.

References basic::Emitter::end().

◆ start_raw()

void basic::JsonEmitter::start_raw ( bool  is_map,
bool  indent 
)
inlineoverrideprotectedvirtual

JSON always uses brackets and braces, regardless of whether we're indenting.

Implements basic::Emitter.

References basic::Emitter::first_, basic::Emitter::in_map_, binder::indent(), basic::Emitter::indent_, basic::Emitter::indent_depth_, and basic::Emitter::out_.

Referenced by JsonEmitter().

◆ write_list_marker()

void basic::JsonEmitter::write_list_marker ( )
inlineoverrideprotectedvirtual

JSON has no special marker for list items.

Implements basic::Emitter.


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