NDDEM
Classes | Private Types | Private Attributes | List of all members
cereal::JSONInputArchive Class Reference

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

#include <json.hpp>

+ Inheritance diagram for cereal::JSONInputArchive:

Classes

class  Iterator
 An internal iterator that handles both array and object types. More...
 

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 ()=default
 
 InputArchiveBase (InputArchiveBase &&) CEREAL_NOEXCEPT
 
InputArchiveBaseoperator= (InputArchiveBase &&) CEREAL_NOEXCEPT
 
virtual ~InputArchiveBase () CEREAL_NOEXCEPT=default
 

Private Types

using ReadStream = CEREAL_RAPIDJSON_NAMESPACE::IStreamWrapper
 
typedef CEREAL_RAPIDJSON_NAMESPACE::GenericValue< CEREAL_RAPIDJSON_NAMESPACE::UTF8<> > JSONValue
 
typedef JSONValue::ConstMemberIterator MemberIterator
 
typedef JSONValue::ConstValueIterator ValueIterator
 
typedef CEREAL_RAPIDJSON_NAMESPACE::Document::GenericValue GenericValue
 

Private Attributes

const char * itsNextName
 Next name set by NVP. More...
 
ReadStream itsReadStream
 Rapidjson write stream. More...
 
std::vector< IteratoritsIteratorStack
 'Stack' of rapidJSON iterators More...
 
CEREAL_RAPIDJSON_NAMESPACE::Document itsDocument
 Rapidjson document. More...
 

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. More...
 
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. More...
 
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();} template< class T > inline typename std::enable_if<!std::is_same< T, int64_t >::value &&std::is_same< T, long long >::value, void >::type loadValue(T &val) { search();val=itsIteratorStack.back().value().GetInt64();++itsIteratorStack.back();} template< class T > inline typename std::enable_if<!std::is_same< T, uint64_t >::value &&std::is_same< T, unsigned long long >::value, void >::type loadValue(T &val) { search();val=itsIteratorStack.back().value().GetUint64();++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. More...
 
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, !std::is_same< T, long long >::value, !std::is_same< T, unsigned long long >::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. More...
 
void loadSize (size_type &size)
 Loads the size for a SizeTag. More...
 
void search ()
 Searches for the expectedName node if it doesn't match the actualName. More...
 
void stringToNumber (std::string const &str, long long &val)
 Convert a string to a long long. More...
 
void stringToNumber (std::string const &str, unsigned long long &val)
 Convert a string to an unsigned long long. More...
 
void stringToNumber (std::string const &str, long double &val)
 Convert a string to a long double. More...
 

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

Member Typedef Documentation

◆ GenericValue

typedef CEREAL_RAPIDJSON_NAMESPACE::Document::GenericValue cereal::JSONInputArchive::GenericValue
private

◆ JSONValue

typedef CEREAL_RAPIDJSON_NAMESPACE::GenericValue<CEREAL_RAPIDJSON_NAMESPACE::UTF8<> > cereal::JSONInputArchive::JSONValue
private

◆ MemberIterator

typedef JSONValue::ConstMemberIterator cereal::JSONInputArchive::MemberIterator
private

◆ ReadStream

◆ ValueIterator

typedef JSONValue::ConstValueIterator cereal::JSONInputArchive::ValueIterator
private

Constructor & Destructor Documentation

◆ JSONInputArchive()

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

Construct, reading from the provided stream.

Parameters
streamThe stream to read from

◆ ~JSONInputArchive()

cereal::JSONInputArchive::~JSONInputArchive ( )
default

Member Function Documentation

◆ finishNode()

void cereal::JSONInputArchive::finishNode ( )
inline

Finishes the most recently started node.

◆ getNodeName()

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

Retrieves the current node name.

Returns
nullptr if no name exists

◆ loadBinaryValue()

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

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

◆ loadSize()

void cereal::JSONInputArchive::loadSize ( size_type size)
inline

Loads the size for a SizeTag.

◆ loadValue() [1/2]

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();} template< class T > inline typename std::enable_if<!std::is_same< T, int64_t >::value &&std::is_same< T, long long >::value, void >::type loadValue(T &val) { search();val=itsIteratorStack.back().value().GetInt64();++itsIteratorStack.back();} template< class T > inline typename std::enable_if<!std::is_same< T, uint64_t >::value &&std::is_same< T, unsigned long long >::value, void >::type loadValue(T &val) { search();val=itsIteratorStack.back().value().GetUint64();++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 cereal::JSONInputArchive::loadValue ( T &  t)
inline

Loads a value from the current node - small signed overload.

◆ loadValue() [2/2]

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, !std::is_same< T, long long >::value, !std::is_same< T, unsigned long long >::value,(sizeof(T) >=sizeof(long double)||sizeof(T) >=sizeof(long long))> = traits::sfinae>
void cereal::JSONInputArchive::loadValue ( T &  val)
inline

Loads a value from the current node - long double and long long overloads.

◆ search()

void cereal::JSONInputArchive::search ( )
inlineprivate

Searches for the expectedName node if it doesn't match the actualName.

This needs to be called before every load or node start occurs. This function will check to see if an NVP has been provided (with setNextName) and if so, see if that name matches the actual next name given. If the names do not match, it will search in the current level of the JSON for that name. If the name is not found, an exception will be thrown.

Resets the NVP name after called.

Exceptions
Exceptionif an expectedName is given and not found

◆ setNextName()

void cereal::JSONInputArchive::setNextName ( const char *  name)
inline

Sets the name for the next node created with startNode.

◆ startNode()

void cereal::JSONInputArchive::startNode ( )
inline

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().

◆ stringToNumber() [1/3]

void cereal::JSONInputArchive::stringToNumber ( std::string const str,
long double &  val 
)
inlineprivate

Convert a string to a long double.

◆ stringToNumber() [2/3]

void cereal::JSONInputArchive::stringToNumber ( std::string const str,
long long &  val 
)
inlineprivate

Convert a string to a long long.

◆ stringToNumber() [3/3]

void cereal::JSONInputArchive::stringToNumber ( std::string const str,
unsigned long long &  val 
)
inlineprivate

Convert a string to an unsigned long long.

Member Data Documentation

◆ itsDocument

CEREAL_RAPIDJSON_NAMESPACE::Document cereal::JSONInputArchive::itsDocument
private

Rapidjson document.

◆ itsIteratorStack

std::vector<Iterator> cereal::JSONInputArchive::itsIteratorStack
private

'Stack' of rapidJSON iterators

◆ itsNextName

const char* cereal::JSONInputArchive::itsNextName
private

Next name set by NVP.

◆ itsReadStream

ReadStream cereal::JSONInputArchive::itsReadStream
private

Rapidjson write stream.


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