| cereal::JSONOutputArchive(3) | Library Functions Manual | cereal::JSONOutputArchive(3) |
NAME
cereal::JSONOutputArchive - An output archive designed to save data to JSON.
SYNOPSIS
#include </home/build/include/cereal/archives/json.hpp>
Inherits cereal::OutputArchive< JSONOutputArchive >, and cereal::traits::TextArchive.
Classes
class Options
A class containing various advanced options for the JSON archive.
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. ~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.
Public Member Functions inherited from
cereal::OutputArchive< JSONOutputArchive >
OutputArchive (JSONOutputArchive *const derived)
Construct the output archive. OutputArchive & operator=
(OutputArchive const &)=delete
JSONOutputArchive & operator() (Types &&... args)
Serializes all passed in data. void serializeDeferments ()
Serializes any data marked for deferment using defer. std::uint32_t
registerSharedPointer (const std::shared_ptr< const void >
&sharedPointer)
Registers a shared pointer with the archive. std::uint32_t
registerPolymorphicType (char const *name)
Registers a polymorphic type name with the archive.
JSONOutputArchive & operator& (T &&arg)
Serializes passed in data.
JSONOutputArchive & operator<< (T &&arg)
Serializes passed in data.
Public Member Functions inherited from
cereal::detail::OutputArchiveBase
OutputArchiveBase (OutputArchiveBase &&) CEREAL_NOEXCEPT
OutputArchiveBase & operator= (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. 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, int >::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, unsigned >::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))> = traits::sfinae> void
saveValue (T const &t)
Save exotic arithmetic as strings to current node. void writeName ()
Write the name of the upcoming node and prepare object/array state. void
makeArray ()
Designates that the current node should be output as an array, not an object.
Additional Inherited Members
using is_loading
Indicates this archive is not intended for loading.
using is_saving
Indicates this archive is intended for saving.
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
options The 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))> = traits::sfinae> 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
Author
Generated automatically by Doxygen for cereal from the source code.
| Sat Oct 10 2026 08:08:15 | cereal |