cereal
A C++11 library for serialization
Classes | List of all members
cereal::JSONOutputArchive Class Reference

An output archive designed to save data to JSON. More...

#include </home/shane/workspace/cereal/include/cereal/archives/json.hpp>

Inheritance diagram for cereal::JSONOutputArchive:
cereal::OutputArchive< JSONOutputArchive > cereal::traits::TextArchive cereal::detail::OutputArchiveBase

Classes

class  Options
 A class containing various advanced options for the JSON archive. More...
 

Public Member Functions

Common Functionality

Common use cases for directly interacting with an JSONOutputArchive

 JSONOutputArchive (std::ostream &stream, Options const &options=Options::Default())
 Construct, outputting to the provided stream. More...
 
 ~JSONOutputArchive () CEREAL_NOEXCEPT
 Destructor, flushes the JSON.
 
void saveBinaryValue (const void *data, size_t size, const char *name=nullptr)
 Saves some binary data, encoded as a base64 string, with an optional name. More...
 
- Public Member Functions inherited from cereal::OutputArchive< JSONOutputArchive >
 OutputArchive (JSONOutputArchive *const derived)
 Construct the output archive. More...
 
OutputArchiveoperator= (OutputArchive const &)=delete
 
JSONOutputArchiveoperator() (Types &&...args)
 Serializes all passed in data. More...
 
std::uint32_t registerSharedPointer (void const *addr)
 Registers a shared pointer with the archive. More...
 
std::uint32_t registerPolymorphicType (char const *name)
 Registers a polymorphic type name with the archive. More...
 
JSONOutputArchiveoperator& (T &&arg)
 Serializes passed in data. More...
 
JSONOutputArchiveoperator<< (T &&arg)
 Serializes passed in data. More...
 
- Public Member Functions inherited from cereal::detail::OutputArchiveBase
 OutputArchiveBase (OutputArchiveBase &&) CEREAL_NOEXCEPT
 
OutputArchiveBaseoperator= (OutputArchiveBase &&) CEREAL_NOEXCEPT
 

Internal Functionality

Functionality designed for use by those requiring control over the inner mechanisms of the JSONOutputArchive

void startNode ()
 Starts a new node in the JSON output. More...
 
void finishNode ()
 Designates the most recently added node as finished.
 
void setNextName (const char *name)
 Sets the name for the next node created with startNode.
 
void saveValue (bool b)
 Saves a bool to the current node.
 
void saveValue (int i)
 Saves an int to the current node.
 
void saveValue (unsigned u)
 Saves a uint to the current node.
 
void saveValue (int64_t i64)
 Saves an int64 to the current node.
 
void saveValue (uint64_t u64)
 Saves a uint64 to the current node.
 
void saveValue (double d)
 Saves a double to the current node.
 
void saveValue (std::string const &s)
 Saves a string to the current node.
 
void saveValue (char const *s)
 Saves a const char * to the current node.
 
void saveValue (std::nullptr_t)
 Saves a nullptr to the current node.
 
template<class T , traits::EnableIf< std::is_same< T, long >::value,!std::is_same< T, std::int32_t >::value,!std::is_same< T, std::int64_t >::value > = traits::sfinae>
void saveValue (T t)
 Serialize a long if it would not be caught otherwise.
 
template<class T , traits::EnableIf< std::is_same< T, unsigned long >::value,!std::is_same< T, std::uint32_t >::value,!std::is_same< T, std::uint64_t >::value > = traits::sfinae>
void saveValue (T t)
 Serialize an unsigned long if it would not be caught otherwise.
 
template<class T , traits::EnableIf< std::is_arithmetic< T >::value,!std::is_same< T, long >::value,!std::is_same< T, unsigned long >::value,!std::is_same< T, std::int64_t >::value,!std::is_same< T, std::uint64_t >::value,(sizeof(T) > = sizeof(long double) || sizeof(T) >= sizeof(long long)>
void saveValue (T const &t)
 Save exotic arithmetic as strings to current node. More...
 
void writeName ()
 Write the name of the upcoming node and prepare object/array state. More...
 
void makeArray ()
 Designates that the current node should be output as an array, not an object.
 

Additional Inherited Members

- Public Types inherited from cereal::OutputArchive< JSONOutputArchive >
using is_loading = std::false_type
 Indicates this archive is not intended for loading. More...
 
using is_saving = std::true_type
 Indicates this archive is intended for saving. More...
 

Detailed Description

An output archive designed to save data to JSON.

This archive uses RapidJSON to build serialize data to JSON.

JSON archives provides a human readable output but at decreased performance (both in time and space) compared to binary archives.

JSON archives are only guaranteed to finish flushing their contents upon destruction and should thus be used in an RAII fashion.

JSON benefits greatly from name-value pairs, which if present, will name the nodes in the output. If these are not present, each level of the output will be given an automatically generated delimited name.

The precision of the output archive controls the number of decimals output for floating point numbers and should be sufficiently large (i.e. at least 20) if there is a desire to have binary equality between the numbers output and those read in. In general you should expect a loss of precision when going from floating point to text and back.

JSON archives do not output the size information for any dynamically sized structure and instead infer it from the number of children for a node. This means that data can be hand edited for dynamic sized structures and will still be readable. This is accomplished through the cereal::SizeTag object, which will cause the archive to output the data as a JSON array (e.g. marked by [] instead of {}), which indicates that the container is variable sized and may be edited.

Constructor & Destructor Documentation

cereal::JSONOutputArchive::JSONOutputArchive ( std::ostream &  stream,
Options const &  options = Options::Default() 
)
inline

Construct, outputting to the provided stream.

Parameters
streamThe stream to output to.
optionsThe JSON specific options to use. See the Options struct for the values of default parameters

Member Function Documentation

void cereal::JSONOutputArchive::saveBinaryValue ( const void *  data,
size_t  size,
const char *  name = nullptr 
)
inline

Saves some binary data, encoded as a base64 string, with an optional name.

This will create a new node, optionally named, and insert a value that consists of the data encoded as a base64 string

template<class T , traits::EnableIf< std::is_arithmetic< T >::value,!std::is_same< T, long >::value,!std::is_same< T, unsigned long >::value,!std::is_same< T, std::int64_t >::value,!std::is_same< T, std::uint64_t >::value,(sizeof(T) > = sizeof(long double) || sizeof(T) >= sizeof(long long)>
void cereal::JSONOutputArchive::saveValue ( T const &  t)
inline

Save exotic arithmetic as strings to current node.

Handles long long (if distinct from other types), unsigned long (if distinct), and long double

void cereal::JSONOutputArchive::startNode ( )
inline

Starts a new node in the JSON output.

The node can optionally be given a name by calling setNextName prior to creating the node

Nodes only need to be started for types that are themselves objects or arrays

void cereal::JSONOutputArchive::writeName ( )
inline

Write the name of the upcoming node and prepare object/array state.

Since writeName is called for every value that is output, regardless of whether it has a name or not, it is the place where we will do a deferred check of our node state and decide whether we are in an array or an object.

The general workflow of saving to the JSON archive is:

  1. (optional) Set the name for the next node to be created, usually done by an NVP
  2. Start the node
  3. (if there is data to save) Write the name of the node (this function)
  4. (if there is data to save) Save the data (with saveValue)
  5. Finish the node

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