Rosetta Utilities  2015.09
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>

Public Member Functions

 JsonEmitter (std::ostream &out)
 
virtual ~JsonEmitter ()
 
virtual void start_doc ()
 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)
 
virtual ~Emitter ()
 
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::pointer::ReferenceCount
 ReferenceCount ()
 Default constructor. More...
 
virtual ~ReferenceCount ()
 

Protected Member Functions

virtual std::string quote_string (std::string const &s)
 JSON always quotes strings, regardless. More...
 
virtual void do_indent (bool write_comma=true)
 Handle pretty-printing indentation. Don't want to use commas for opening/closing brace/bracket. More...
 
virtual void write_list_marker ()
 JSON has no special marker for list items. More...
 
virtual void start_raw (bool is_map, bool indent)
 
virtual void end_raw ()
 
- 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

- Public Types inherited from utility::pointer::ReferenceCount
typedef platform::Size Size
 
typedef platform::Size size_type
 
- Protected Attributes inherited from basic::Emitter
std::ostream & out_
 
std::string indent_str_
 
std::vector< boolin_map_
 
std::vector< boolfirst_
 
std::vector< boolindent_
 
std::vector< intindent_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

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

References start_raw().

virtual basic::JsonEmitter::~JsonEmitter ( )
inlinevirtual
basic::JsonEmitter::JsonEmitter ( )
private
basic::JsonEmitter::JsonEmitter ( JsonEmitter const &  )
private

Member Function Documentation

virtual void basic::JsonEmitter::do_indent ( bool  write_comma = true)
inlineprotectedvirtual

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

Implements basic::Emitter.

References basic::Emitter::depth(), basic::Emitter::first_, basic::Emitter::indent_, basic::Emitter::indent_depth_, basic::Emitter::indent_str_, and basic::Emitter::out_.

Referenced by end_raw().

virtual void basic::JsonEmitter::end_raw ( )
inlineprotectedvirtual

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_.

virtual std::string basic::JsonEmitter::quote_string ( std::string const &  s)
inlineprotectedvirtual

JSON always quotes strings, regardless.

Implements basic::Emitter.

References basic::Emitter::escape_string().

virtual void basic::JsonEmitter::start_doc ( )
inlinevirtual

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().

virtual void basic::JsonEmitter::start_raw ( bool  is_map,
bool  indent 
)
inlineprotectedvirtual

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

Implements basic::Emitter.

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

Referenced by JsonEmitter().

virtual void basic::JsonEmitter::write_list_marker ( )
inlineprotectedvirtual

JSON has no special marker for list items.

Implements basic::Emitter.


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