BDE 4.14.0 Production release
Loading...
Searching...
No Matches
baljsn::SimpleFormatter Class Reference

#include <baljsn_simpleformatter.h>

Public Types

enum  ArrayFormattingStyle { e_REGULAR_ARRAY_FORMAT = 1 , e_EMPTY_ARRAY_FORMAT }
 

Public Member Functions

 BSLMF_NESTED_TRAIT_DECLARATION (SimpleFormatter, bslma::UsesBslmaAllocator)
 
 SimpleFormatter (bsl::ostream &stream, bslma::Allocator *basicAllocator=0)
 
 SimpleFormatter (bsl::ostream &stream, const EncoderOptions &encoderOptions, bslma::Allocator *basicAllocator=0)
 
 SimpleFormatter (const SimpleFormatter &original, bslma::Allocator *basicAllocator)
 
 ~SimpleFormatter ()
 
void openObject ()
 
void openObject (const bsl::string_view &name)
 
void addMemberName (const bsl::string_view &name)
 
void closeObject ()
 
void openArray (ArrayFormattingStyle formattingStyle=e_REGULAR_ARRAY_FORMAT)
 
void openArray (const bsl::string_view &name, ArrayFormattingStyle formattingStyle=e_REGULAR_ARRAY_FORMAT)
 
void closeArray (ArrayFormattingStyle formattingStyle=e_REGULAR_ARRAY_FORMAT)
 
void addNullValue ()
 
void addNullValue (const bsl::string_view &name)
 
template<class TYPE >
int addValue (const TYPE &value)
 
template<class TYPE >
int addValue (const bsl::string_view &name, const TYPE &value)
 
bool isCompleteJSON () const
 
bool isFormattingArray () const
 
bool isFormattingObject () const
 
bool isNameNeeded () const
 
bslma::Allocatorallocator () const
 

Detailed Description

This class implements a formatter providing operations for rendering JSON text elements to an output stream (supplied at construction) according to a set of formatting options (also supplied at construction).

This class has an interface that's easier to use than that of baljsn::Formatter, and generates more correctly-formatted pretty output.

See baljsn_simpleformatter

Member Enumeration Documentation

◆ ArrayFormattingStyle

Enumerator
e_REGULAR_ARRAY_FORMAT 
e_EMPTY_ARRAY_FORMAT 

Constructor & Destructor Documentation

◆ SimpleFormatter() [1/3]

baljsn::SimpleFormatter::SimpleFormatter ( bsl::ostream &  stream,
bslma::Allocator basicAllocator = 0 
)
explicit

◆ SimpleFormatter() [2/3]

baljsn::SimpleFormatter::SimpleFormatter ( bsl::ostream &  stream,
const EncoderOptions encoderOptions,
bslma::Allocator basicAllocator = 0 
)
explicit

Create a SimpleFormatter object using the specified stream. Optionally specify encoderOptions to configure the output options

  • if encoderOptions is not supplied, a default-constructed EncoderOptions object will be used. Note that the encodeEmptyArrays attribute in the encoderOptions is ignored. Optionally specify a basicAllocator used to supply memory. If basicAllocator is 0, the currently installed default allocator is used.

◆ SimpleFormatter() [3/3]

baljsn::SimpleFormatter::SimpleFormatter ( const SimpleFormatter original,
bslma::Allocator basicAllocator 
)

Create a SimpleFormatter object having the same value as the specified original object. Optionally specify a basicAllocator used to supply memory. If basicAllocator is 0, the currently installed default allocator is used.

◆ ~SimpleFormatter()

baljsn::SimpleFormatter::~SimpleFormatter ( )

Destroy this object. Note that correct JSON has been generated if the isCompleteJSON() call returns true.

Member Function Documentation

◆ addMemberName()

void baljsn::SimpleFormatter::addMemberName ( const bsl::string_view name)
inline

Print onto the stream supplied at construction the specified name in double-quotes, preceded, if necessary, by a comma, and followed by a :. The behavior is undefined unless isNameNeeded() is true. After this operation, isNameNeeded() will be false, and an immediately subsequent attempt to add a value (or open an object or array) should not provide a name.

◆ addNullValue() [1/2]

void baljsn::SimpleFormatter::addNullValue ( )
inline

Print onto the stream supplied at construction the value corresponding to a null element, preceded, if necessary, by a comma. The behavior is undefined unless isNameNeeded() is false.

◆ addNullValue() [2/2]

void baljsn::SimpleFormatter::addNullValue ( const bsl::string_view name)
inline

Print onto the stream supplied at construction the value corresponding to a null element with the specified name, preceded, if necessary, by a comma. The behavior is undefined unless isNameNeeded() is true.

◆ addValue() [1/2]

template<class TYPE >
int baljsn::SimpleFormatter::addValue ( const bsl::string_view name,
const TYPE &  value 
)

Print onto the stream supplied at construction the specified name and the specified value, preceded, if necessary, by a comma, passing the optionally specified options through to the rendering routines. Return 0 on success and a non-zero value otherwise. The behavior is undefined unless isNameNeeded() is true.

◆ addValue() [2/2]

template<class TYPE >
int baljsn::SimpleFormatter::addValue ( const TYPE &  value)

Print onto the stream supplied at construction the specified value, preceded, if necessary, by a comma, passing the optionally specified options through to the rendering routines. Return 0 on success and a non-zero value otherwise. The behavior is undefined unless isNameNeeded() is false.

◆ allocator()

bslma::Allocator * baljsn::SimpleFormatter::allocator ( ) const
inline

Return the allocator used by this object to supply memory. Note that if no allocator was supplied at construction the currently installed default allocator is used.

◆ BSLMF_NESTED_TRAIT_DECLARATION()

baljsn::SimpleFormatter::BSLMF_NESTED_TRAIT_DECLARATION ( SimpleFormatter  ,
bslma::UsesBslmaAllocator   
)

◆ closeArray()

void baljsn::SimpleFormatter::closeArray ( ArrayFormattingStyle  formattingStyle = e_REGULAR_ARRAY_FORMAT)

Print onto the stream supplied at construction the sequence of characters designating the end of an array (referred to as an "array" in JSON). Optionally specify formattingStyle denoting if the array being closed should be formatted as an empty array. If formattingStyle is not specified then the array being closed is formatted as a regular array having elements. The behavior is undefined if isFormattingArray() is false. Note that the formatting (and as a consequence the formattingStyle) is relevant only if this formatter encodes in the pretty style and is ignored otherwise.

◆ closeObject()

void baljsn::SimpleFormatter::closeObject ( )

Print onto the stream supplied at construction the sequence of characters designating the end of an object (referred to as an "object" in JSON). The behavior is undefined unless isNameNeeded() is true.

◆ isCompleteJSON()

bool baljsn::SimpleFormatter::isCompleteJSON ( ) const
inline

Return true if this SimpleFormatter has formatted a complete JSON object, where all open* calls have been balanced by their corresponding close* calls. Note that a default-constructed SimpleFormatter will return false - an empty string is not valid JSON.

◆ isFormattingArray()

bool baljsn::SimpleFormatter::isFormattingArray ( ) const
inline

Return true if this SimpleFormatter is currently formatting an array and false otherwise. It is formatting an array if the last open* method overload (openArray or openObject) called on this SimpleFormatter for which the corresponding close* method (respectively, closeArray or closeObject) was openArray. If isFormattingArray() is true, then isFormattingObject() is false. Note that both can be false, at the top-level initial scope before anything is added/opened or after the first open* call has been closed'

◆ isFormattingObject()

bool baljsn::SimpleFormatter::isFormattingObject ( ) const
inline

Return true if this SimpleFormatter is currently formatting an object scope and false otherwise. It is formatting an object scope if the last open* method overload (openArray or openObject) called on this SimpleFormatter for which the corresponding close* method (respectively, closeArray or closeObject) was openObject. If isFormattingObject() is true, then isFormattingArray() is false. Note that both can be false, at the top-level initial scope before anything is added/opened or after the first open* call has been closed' JSON.

◆ isNameNeeded()

bool baljsn::SimpleFormatter::isNameNeeded ( ) const
inline

Return true if a subsequent attempt to add a value must supply a name, and false otherwise. This will be true if isFormattingObject() is true, and addMemberName() was not the most recently called manipulator. That is, a name is needed if this formatter is currently in the context of formatting the members of a JSON object, and addMemberName has not been called to explicitly provide a name for the next member.

◆ openArray() [1/2]

void baljsn::SimpleFormatter::openArray ( ArrayFormattingStyle  formattingStyle = e_REGULAR_ARRAY_FORMAT)

Print onto the stream supplied at construction the sequence of characters designating the start of an array (referred to as an "array" in JSON), preceded, if necessary, by a comma. Optionally specify formattingStyle denoting if the array being opened should be formatted as an empty array. If formattingStyle is not specified then the array being opened is formatted as a regular array having elements. The behavior is undefined unless isNameNeeded() is false. Note that the formatting (and as a consequence the formattingStyle) is relevant only if this formatter encodes in the pretty style and is ignored otherwise.

◆ openArray() [2/2]

void baljsn::SimpleFormatter::openArray ( const bsl::string_view name,
ArrayFormattingStyle  formattingStyle = e_REGULAR_ARRAY_FORMAT 
)

Print onto the stream supplied at construction the sequence of characters designating the start of an array (referred to as an "array" in JSON) with the specified name, preceded, if necessary, by a comma. Optionally specify formattingStyle denoting if the array being opened should be formatted as an empty array. If formattingStyle is not specified then the array being opened is formatted as a regular array having elements. The behavior is undefined unless isNameNeeded() is true. Note that the formatting (and as a consequence the formattingStyle) is relevant only if this formatter encodes in the pretty style and is ignored otherwise.

◆ openObject() [1/2]

void baljsn::SimpleFormatter::openObject ( )

Print onto the stream supplied at construction the sequence of characters designating the start of an object (referred to as an "object" in JSON), preceded, if necessary, by a comma. The behavior is undefined unless isNameNeeded() is false.

◆ openObject() [2/2]

void baljsn::SimpleFormatter::openObject ( const bsl::string_view name)

Print onto the stream supplied at construction the sequence of characters designating the start of an object (referred to as an "object" in JSON) with the specified name , preceded, if necessary, by a comma. The behavior is undefined unless isNameNeeded() is true.


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