Loading...
Searching...
No Matches
JsWriter Class Reference

This class provides an interface to writing json values directly to a stream. More...

#include <json.h>

Public Types

enum class  Style { Compact , Pretty }
 

Public Member Functions

JS_API JsWriter (std::ostream &ostr, Style style=Style::Compact)
 Constructor.
 
JS_API ~JsWriter ()
 Destructor.
 
 JsWriter (const JsWriter &)=delete
 Disable copies.
 
JsWriteroperator= (const JsWriter &)=delete
 
JS_API bool WriteValue (std::nullptr_t)
 Write a null value.
 
JS_API bool WriteValue (bool b)
 Write a boolean value.
 
JS_API bool WriteValue (int i)
 Write an integer value.
 
JS_API bool WriteValue (unsigned u)
 Write an unsigned integer value.
 
JS_API bool WriteValue (int64_t i)
 Write a 64-bit integer value.
 
JS_API bool WriteValue (uint64_t u)
 Write a 64-bit unsigned integer value.
 
JS_API bool WriteValue (double d)
 Write a double value.
 
JS_API bool WriteValue (const std::string &s)
 Write a string value.
 
JS_API bool WriteValue (const char *s)
 Write a string value.
 
template<size_t N>
bool WriteValue (const char(&s)[N])
 Write a string value.
 
JS_API bool BeginObject ()
 Write the start of an object.
 
JS_API bool WriteKey (const std::string &)
 Write an object key.
 
JS_API bool WriteKey (const char *)
 Write an object key.
 
template<size_t N>
bool WriteKey (const char(&s)[N])
 Write a string literal object key.
 
template<class K , class V >
void WriteKeyValue (K &&k, V &&v)
 Convenience function to write an object key and value.
 
JS_API bool EndObject ()
 Write the end of an object.
 
JS_API bool BeginArray ()
 Write the start of an array.
 
JS_API bool EndArray ()
 Write the end of an array.
 
template<class Container >
void WriteArray (const Container &c)
 Convenience function to write an array of values.
 
template<class Container , class ItemWriteFn >
void WriteArray (const Container &c, const ItemWriteFn &f)
 Convenience function to write an array of values by calling the given functor for each item in the container.
 
template<class Iterator , class ItemWriteFn >
void WriteArray (const Iterator &begin, const Iterator &end, const ItemWriteFn &f)
 Convenience function to write an array of values given two iterators by calling the given functor for each item in the container.
 
template<class ... T>
void WriteObject (T &&... f)
 Convenience function to write an object given key value pair arguments.
 

Detailed Description

This class provides an interface to writing json values directly to a stream.

This can be much more efficient than constructing a JsValue instance and using JsWriteToStream if the data size is significant.

Definition at line 76 of file json.h.

Member Enumeration Documentation

◆ Style

enum class Style
strong

Definition at line 78 of file json.h.

Constructor & Destructor Documentation

◆ JsWriter() [1/2]

JS_API JsWriter ( std::ostream &  ostr,
Style  style = Style::Compact 
)

Constructor.

The lifetime of the /p ostr parameter is assumed to be longer than the JsWriter instance.

◆ ~JsWriter()

JS_API ~JsWriter ( )

Destructor.

◆ JsWriter() [2/2]

JsWriter ( const JsWriter )
delete

Disable copies.

Member Function Documentation

◆ BeginArray()

JS_API bool BeginArray ( )

Write the start of an array.

◆ BeginObject()

JS_API bool BeginObject ( )

Write the start of an object.

◆ EndArray()

JS_API bool EndArray ( )

Write the end of an array.

◆ EndObject()

JS_API bool EndObject ( )

Write the end of an object.

◆ WriteArray() [1/3]

void WriteArray ( const Container &  c)
inline

Convenience function to write an array of values.

Definition at line 155 of file json.h.

◆ WriteArray() [2/3]

void WriteArray ( const Container &  c,
const ItemWriteFn &  f 
)
inline

Convenience function to write an array of values by calling the given functor for each item in the container.

Definition at line 166 of file json.h.

◆ WriteArray() [3/3]

void WriteArray ( const Iterator &  begin,
const Iterator &  end,
const ItemWriteFn &  f 
)
inline

Convenience function to write an array of values given two iterators by calling the given functor for each item in the container.

Definition at line 177 of file json.h.

◆ WriteKey() [1/3]

JS_API bool WriteKey ( const char *  )

Write an object key.

◆ WriteKey() [2/3]

bool WriteKey ( const char(&)  s[N])
inline

Write a string literal object key.

Definition at line 136 of file json.h.

◆ WriteKey() [3/3]

JS_API bool WriteKey ( const std::string &  )

Write an object key.

◆ WriteKeyValue()

void WriteKeyValue ( K &&  k,
V &&  v 
)
inline

Convenience function to write an object key and value.

Definition at line 140 of file json.h.

◆ WriteObject()

void WriteObject ( T &&...  f)
inline

Convenience function to write an object given key value pair arguments.

key arguments must be convertable to strings, value argruments must be either a writable type, or a callablable type taking a JsWriter&.

Definition at line 190 of file json.h.

◆ WriteValue() [1/10]

JS_API bool WriteValue ( bool  b)

Write a boolean value.

◆ WriteValue() [2/10]

JS_API bool WriteValue ( const char *  s)

Write a string value.

◆ WriteValue() [3/10]

bool WriteValue ( const char(&)  s[N])
inline

Write a string value.

Definition at line 123 of file json.h.

◆ WriteValue() [4/10]

JS_API bool WriteValue ( const std::string &  s)

Write a string value.

◆ WriteValue() [5/10]

JS_API bool WriteValue ( double  d)

Write a double value.

◆ WriteValue() [6/10]

JS_API bool WriteValue ( int  i)

Write an integer value.

◆ WriteValue() [7/10]

JS_API bool WriteValue ( int64_t  i)

Write a 64-bit integer value.

◆ WriteValue() [8/10]

JS_API bool WriteValue ( std::nullptr_t  )

Write a null value.

◆ WriteValue() [9/10]

JS_API bool WriteValue ( uint64_t  u)

Write a 64-bit unsigned integer value.

◆ WriteValue() [10/10]

JS_API bool WriteValue ( unsigned  u)

Write an unsigned integer value.


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