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

Writes a Value in JSON format in a human friendly way. More...

#include <json_writer.h>

Inheritance diagram for json::StyledWriter:
Collaboration diagram for json::StyledWriter:

Public Member Functions

 StyledWriter ()
 ~StyledWriter () override=default
std::string write (Value const &root) override
 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::string document_
std::string indentString_
int rightMargin_ {74}
int indentSize_ {3}
bool addChildValues_ {}

Detailed Description

Writes a Value in JSON format in a human friendly way.

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.
See also
Reader, Value

Definition at line 66 of file json_writer.h.

Member Typedef Documentation

◆ ChildValues

Definition at line 98 of file json_writer.h.

Constructor & Destructor Documentation

◆ StyledWriter()

json::StyledWriter::StyledWriter ( )
default

◆ ~StyledWriter()

json::StyledWriter::~StyledWriter ( )
overridedefault

Member Function Documentation

◆ write()

std::string json::StyledWriter::write ( Value const & root)
overridevirtual

Serialize a Value in JSON format.

Parameters
rootValue to serialize.
Returns
String containing the JSON document that represents the root value.

Implements json::WriterBase.

Definition at line 259 of file json_writer.cpp.

◆ writeValue()

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

Definition at line 270 of file json_writer.cpp.

◆ writeArrayValue()

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

Definition at line 338 of file json_writer.cpp.

◆ isMultilineArray()

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

Definition at line 401 of file json_writer.cpp.

◆ pushValue()

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

Definition at line 434 of file json_writer.cpp.

◆ writeIndent()

void json::StyledWriter::writeIndent ( )
private

Definition at line 447 of file json_writer.cpp.

◆ writeWithIndent()

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

Definition at line 464 of file json_writer.cpp.

◆ indent()

void json::StyledWriter::indent ( )
private

Definition at line 471 of file json_writer.cpp.

◆ unindent()

void json::StyledWriter::unindent ( )
private

Definition at line 477 of file json_writer.cpp.

Member Data Documentation

◆ childValues_

ChildValues json::StyledWriter::childValues_
private

Definition at line 100 of file json_writer.h.

◆ document_

std::string json::StyledWriter::document_
private

Definition at line 101 of file json_writer.h.

◆ indentString_

std::string json::StyledWriter::indentString_
private

Definition at line 102 of file json_writer.h.

◆ rightMargin_

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

Definition at line 103 of file json_writer.h.

◆ indentSize_

int json::StyledWriter::indentSize_ {3}
private

Definition at line 104 of file json_writer.h.

◆ addChildValues_

bool json::StyledWriter::addChildValues_ {}
private

Definition at line 105 of file json_writer.h.