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

An input archive designed to load data from JSON. More...

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

Inheritance diagram for cereal::JSONInputArchive:
cereal::InputArchive< JSONInputArchive > cereal::traits::TextArchive cereal::detail::InputArchiveBase

Public Member Functions

Common Functionality

Common use cases for directly interacting with an JSONInputArchive

 JSONInputArchive (std::istream &stream)
 Construct, reading from the provided stream. More...
 ~JSONInputArchive () CEREAL_NOEXCEPT=default
void loadBinaryValue (void *data, size_t size, const char *name=nullptr)
 Loads some binary data, encoded as a base64 string. More...
- Public Member Functions inherited from cereal::InputArchive< JSONInputArchive >
 InputArchive (JSONInputArchive *const derived)
 Construct the output archive. More...
InputArchiveoperator= (InputArchive const &)=delete
JSONInputArchiveoperator() (Types &&... args)
 Serializes all passed in data. More...
void serializeDeferments ()
 Serializes any data marked for deferment using defer. More...
std::shared_ptr< void > getSharedPointer (std::uint32_t const id)
 Retrieves a shared pointer given a unique key for it. More...
void registerSharedPointer (std::uint32_t const id, std::shared_ptr< void > ptr)
 Registers a shared pointer to its unique identifier. More...
std::string getPolymorphicName (std::uint32_t const id)
 Retrieves the string for a polymorphic type given a unique key for it. More...
void registerPolymorphicName (std::uint32_t const id, std::string const &name)
 Registers a polymorphic name string to its unique identifier. More...
JSONInputArchiveoperator& (T &&arg)
 Serializes passed in data. More...
JSONInputArchiveoperator>> (T &&arg)
 Serializes passed in data. More...
- Public Member Functions inherited from cereal::detail::InputArchiveBase
 InputArchiveBase (InputArchiveBase &&) CEREAL_NOEXCEPT
InputArchiveBaseoperator= (InputArchiveBase &&) CEREAL_NOEXCEPT

Internal Functionality

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

void startNode ()
 Starts a new node, going into its proper iterator. More...
void finishNode ()
 Finishes the most recently started node.
const char * getNodeName () const
 Retrieves the current node name. More...
void setNextName (const char *name)
 Sets the name for the next node created with startNode.
template<class T , traits::EnableIf< std::is_signed< T >::value, sizeof(T)< sizeof(int64_t)>=traits::sfinae > inline void loadValue(T &val) { search();val=static_cast< T >(itsIteratorStack.back().value().GetInt());++itsIteratorStack.back();} template< class T, traits::EnableIf< std::is_unsigned< T >::value, sizeof(T)< sizeof(uint64_t), !std::is_same< bool, T >::value >=traits::sfinae > inline void loadValue(T &val) { search();val=static_cast< T >(itsIteratorStack.back().value().GetUint());++itsIteratorStack.back();} void loadValue(bool &val) { search();val=itsIteratorStack.back().value().GetBool();++itsIteratorStack.back();} void loadValue(int64_t &val) { search();val=itsIteratorStack.back().value().GetInt64();++itsIteratorStack.back();} void loadValue(uint64_t &val) { search();val=itsIteratorStack.back().value().GetUint64();++itsIteratorStack.back();} void loadValue(float &val) { search();val=static_cast< float >(itsIteratorStack.back().value().GetDouble());++itsIteratorStack.back();} void loadValue(double &val) { search();val=itsIteratorStack.back().value().GetDouble();++itsIteratorStack.back();} void loadValue(std::string &val) { search();val=itsIteratorStack.back().value().GetString();++itsIteratorStack.back();} void loadValue(std::nullptr_t &) { search();CEREAL_RAPIDJSON_ASSERT(itsIteratorStack.back().value().IsNull());++itsIteratorStack.back();} private:template< class T > inline typename std::enable_if< sizeof(T)==sizeof(std::int32_t) &&std::is_signed< T >::value, void >::type loadLong(T &l){ loadValue(reinterpret_cast< std::int32_t & >(l));} template< class T > inline typename std::enable_if< sizeof(T)==sizeof(std::int64_t) &&std::is_signed< T >::value, void >::type loadLong(T &l){ loadValue(reinterpret_cast< std::int64_t & >(l));} template< class T > inline typename std::enable_if< sizeof(T)==sizeof(std::uint32_t) &&!std::is_signed< T >::value, void >::type loadLong(T &lu){ loadValue(reinterpret_cast< std::uint32_t & >(lu));} template< class T > inline typename std::enable_if< sizeof(T)==sizeof(std::uint64_t) &&!std::is_signed< T >::value, void >::type loadLong(T &lu){ loadValue(reinterpret_cast< std::uint64_t & >(lu));} public:template< class T > inline typename std::enable_if< std::is_same< T, long >::value &&sizeof(T) >=sizeof(std::int64_t) &&!std::is_same< T, std::int64_t >::value, void >::type loadValue(T &t){ loadLong(t);} template< class T > inline typename std::enable_if< std::is_same< T, unsigned long >::value &&sizeof(T) > = sizeof(std::uint64_t) && !std::is_same<T, std::uint64_t>::value, void >
::type loadValue (T &t)
 Loads a value from the current node - small signed overload.
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 loadValue (T &val)
 Loads a value from the current node - long double and long long overloads.
void loadSize (size_type &size)
 Loads the size for a SizeTag.

Additional Inherited Members

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

Detailed Description

An input archive designed to load data from JSON.

This archive uses RapidJSON to read in a JSON archive.

As with the output JSON archive, the preferred way to use this archive is in an RAII fashion, ensuring its destruction after all data has been read.

Input JSON should have been produced by the JSONOutputArchive. Data can only be added to dynamically sized containers (marked by JSON arrays) - the input archive will determine their size by looking at the number of child nodes. Only JSON originating from a JSONOutputArchive is officially supported, but data from other sources may work if properly formatted.

The JSONInputArchive does not require that nodes are loaded in the same order they were saved by JSONOutputArchive. Using name value pairs (NVPs), it is possible to load in an out of order fashion or otherwise skip/select specific nodes to load.

The default behavior of the input archive is to read sequentially starting with the first node and exploring its children. When a given NVP does not match the read in name for a node, the archive will search for that node at the current level and load it if it exists. After loading an out of order node, the archive will then proceed back to loading sequentially from its new position.

Consider this simple example where loading of some data is skipped:

// imagine the input file has someData(1-9) saved in order at the top level node
ar( someData1, someData2, someData3 ); // XML loads in the order it sees in the file
ar( cereal::make_nvp( "hello", someData6 ) ); // NVP given does not
// match expected NVP name, so we search
// for the given NVP and load that value
ar( someData7, someData8, someData9 ); // with no NVP given, loading resumes at its
// current location, proceeding sequentially

Constructor & Destructor Documentation

◆ JSONInputArchive()

cereal::JSONInputArchive::JSONInputArchive ( std::istream &  stream)

Construct, reading from the provided stream.

streamThe stream to read from

Member Function Documentation

◆ getNodeName()

const char* cereal::JSONInputArchive::getNodeName ( ) const

Retrieves the current node name.

nullptr if no name exists

◆ loadBinaryValue()

void cereal::JSONInputArchive::loadBinaryValue ( void *  data,
size_t  size,
const char *  name = nullptr 

Loads some binary data, encoded as a base64 string.

This will automatically start and finish a node to load the data, and can be called directly by users.

Note that this follows the same ordering rules specified in the class description in regards to loading in/out of order

◆ startNode()

void cereal::JSONInputArchive::startNode ( )

Starts a new node, going into its proper iterator.

This places an iterator for the next node to be parsed onto the iterator stack. If the next node is an array, this will be a value iterator, otherwise it will be a member iterator.

By default our strategy is to start with the document root node and then recursively iterate through all children in the order they show up in the document. We don't need to know NVPs to do this; we'll just blindly load in the order things appear in.

If we were given an NVP, we will search for it if it does not match our the name of the next node that would normally be loaded. This functionality is provided by search().

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