xrpld
Loading...
Searching...
No Matches
json::StyledStreamWriter Class Reference

Writes a Value in JSON format in a human friendly way, to a stream rather than to a string. More...

#include <json_writer.h>

Collaboration diagram for json::StyledStreamWriter:

Public Member Functions

 StyledStreamWriter (std::string indentation="\t")
 ~StyledStreamWriter ()=default
void write (std::ostream &out, Value const &root)
 Serialize a Value in JSON format.

Private Types

using ChildValues = std::vector<std::string>

Private Member Functions

void writeValue (Value const &value)
void writeArrayValue (Value const &value)
bool isMultilineArray (Value const &value)
void pushValue (std::string const &value)
void writeIndent ()
void writeWithIndent (std::string const &value)
void indent ()
void unindent ()

Private Attributes

ChildValues childValues_
std::ostreamdocument_ {nullptr}
std::string indentString_
int rightMargin_ {74}
std::string indentation_
bool addChildValues_ {}

Detailed Description

Writes a Value in JSON format in a human friendly way, to a stream rather than to a string.

The rules for line break and indent are as follow:

  • Object value:
    • if empty then print {} without indent and line break
    • if not empty the print '{', line break & indent, print one value per line and then unindent and line break and print '}'.
  • Array value:
    • if empty then print [] without indent and line break
    • if the array contains no object value, empty array or some other value types, and all the values fit on one lines, then print the array on a single line.
    • otherwise, it the values do not fit on one line, or the array contains object or non empty array, then print one value per line.
Parameters
indentationEach level will be indented by this amount extra.
See also
Reader, Value

Definition at line 129 of file json_writer.h.

Member Typedef Documentation

◆ ChildValues

Definition at line 163 of file json_writer.h.

Constructor & Destructor Documentation

◆ StyledStreamWriter()

json::StyledStreamWriter::StyledStreamWriter ( std::string indentation = "\t")

Definition at line 488 of file json_writer.cpp.

◆ ~StyledStreamWriter()

json::StyledStreamWriter::~StyledStreamWriter ( )
default

Member Function Documentation

◆ write()

void json::StyledStreamWriter::write ( std::ostream & out,
Value const & root )

Serialize a Value in JSON format.

Parameters
outStream to write to. (Can be ostringstream, e.g.)
rootValue to serialize.
Note
There is no point in deriving from Writer, since write() should not return a value.

Definition at line 494 of file json_writer.cpp.

◆ writeValue()

void json::StyledStreamWriter::writeValue ( Value const & value)
private

Definition at line 505 of file json_writer.cpp.

◆ writeArrayValue()

void json::StyledStreamWriter::writeArrayValue ( Value const & value)
private

Definition at line 573 of file json_writer.cpp.

◆ isMultilineArray()

bool json::StyledStreamWriter::isMultilineArray ( Value const & value)
private

Definition at line 636 of file json_writer.cpp.

◆ pushValue()

void json::StyledStreamWriter::pushValue ( std::string const & value)
private

Definition at line 669 of file json_writer.cpp.

◆ writeIndent()

void json::StyledStreamWriter::writeIndent ( )
private

Definition at line 682 of file json_writer.cpp.

◆ writeWithIndent()

void json::StyledStreamWriter::writeWithIndent ( std::string const & value)
private

Definition at line 700 of file json_writer.cpp.

◆ indent()

void json::StyledStreamWriter::indent ( )
private

Definition at line 707 of file json_writer.cpp.

◆ unindent()

void json::StyledStreamWriter::unindent ( )
private

Definition at line 713 of file json_writer.cpp.

Member Data Documentation

◆ childValues_

ChildValues json::StyledStreamWriter::childValues_
private

Definition at line 165 of file json_writer.h.

◆ document_

std::ostream* json::StyledStreamWriter::document_ {nullptr}
private

Definition at line 166 of file json_writer.h.

◆ indentString_

std::string json::StyledStreamWriter::indentString_
private

Definition at line 167 of file json_writer.h.

◆ rightMargin_

int json::StyledStreamWriter::rightMargin_ {74}
private

Definition at line 168 of file json_writer.h.

◆ indentation_

std::string json::StyledStreamWriter::indentation_
private

Definition at line 169 of file json_writer.h.

◆ addChildValues_

bool json::StyledStreamWriter::addChildValues_ {}
private

Definition at line 170 of file json_writer.h.