Untitled diff
567 lines
/*! \file json.hpp
/*! \file json.hpp
\brief JSON input and output archives */
\brief JSON input and output archives */
/*
/*
Copyright (c) 2014, Randolph Voorhies, Shane Grant
Copyright (c) 2014, Randolph Voorhies, Shane Grant
All rights reserved.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
documentation and/or other materials provided with the distribution.
* Neither the name of cereal nor the
* Neither the name of cereal nor the
names of its contributors may be used to endorse or promote products
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
*/
#ifndef CEREAL_ARCHIVES_JSON_HPP_
#ifndef CEREAL_ARCHIVES_JSON_HPP_
#define CEREAL_ARCHIVES_JSON_HPP_
#define CEREAL_ARCHIVES_JSON_HPP_
#include "cereal/cereal.hpp"
#include "cereal/cereal.hpp"
#include "cereal/details/util.hpp"
#include "cereal/details/util.hpp"
namespace cereal
namespace cereal
{
{
//! An exception thrown when rapidjson fails an internal assertion
//! An exception thrown when rapidjson fails an internal assertion
/*! @ingroup Utility */
/*! @ingroup Utility */
struct RapidJSONException : Exception
struct RapidJSONException : Exception
{ RapidJSONException( const char * what_ ) : Exception( what_ ) {} };
{ RapidJSONException( const char * what_ ) : Exception( what_ ) {} };
}
}
// Override rapidjson assertions to throw exceptions by default
// Override rapidjson assertions to throw exceptions by default
#ifndef CEREAL_RAPIDJSON_ASSERT
#ifndef CEREAL_RAPIDJSON_ASSERT
#define CEREAL_RAPIDJSON_ASSERT(x) if(!(x)){ \
#define CEREAL_RAPIDJSON_ASSERT(x) if(!(x)){ \
throw ::cereal::RapidJSONException("rapidjson internal assertion failure: " #x); }
throw ::cereal::RapidJSONException("rapidjson internal assertion failure: " #x); }
#endif // RAPIDJSON_ASSERT
#endif // RAPIDJSON_ASSERT
// Enable support for parsing of nan, inf, -inf
// Enable support for parsing of nan, inf, -inf
#define CEREAL_RAPIDJSON_WRITE_DEFAULT_FLAGS kWriteNanAndInfFlag
#define CEREAL_RAPIDJSON_WRITE_DEFAULT_FLAGS kWriteNanAndInfFlag
#define CEREAL_RAPIDJSON_PARSE_DEFAULT_FLAGS kParseFullPrecisionFlag | kParseNanAndInfFlag
#define CEREAL_RAPIDJSON_PARSE_DEFAULT_FLAGS kParseFullPrecisionFlag | kParseNanAndInfFlag
#include "cereal/external/rapidjson/prettywriter.h"
#include "cereal/external/rapidjson/prettywriter.h"
#include "cereal/external/rapidjson/ostreamwrapper.h"
#include "cereal/external/rapidjson/ostreamwrapper.h"
#include "cereal/external/rapidjson/istreamwrapper.h"
#include "cereal/external/rapidjson/istreamwrapper.h"
#include "cereal/external/rapidjson/document.h"
#include "cereal/external/rapidjson/document.h"
#include "cereal/external/base64.hpp"
#include "cereal/external/base64.hpp"
#include <limits>
#include <limits>
#include <sstream>
#include <sstream>
#include <stack>
#include <stack>
#include <vector>
#include <vector>
#include <string>
#include <string>
#include <EASTL/string.h>
namespace cereal
namespace cereal
{
{
// ######################################################################
// ######################################################################
//! An output archive designed to save data to JSON
//! An output archive designed to save data to JSON
/*! This archive uses RapidJSON to build serialize data to JSON.
/*! This archive uses RapidJSON to build serialize data to JSON.
JSON archives provides a human readable output but at decreased
JSON archives provides a human readable output but at decreased
performance (both in time and space) compared to binary archives.
performance (both in time and space) compared to binary archives.
JSON archives are only guaranteed to finish flushing their contents
JSON archives are only guaranteed to finish flushing their contents
upon destruction and should thus be used in an RAII fashion.
upon destruction and should thus be used in an RAII fashion.
JSON benefits greatly from name-value pairs, which if present, will
JSON benefits greatly from name-value pairs, which if present, will
name the nodes in the output. If these are not present, each level
name the nodes in the output. If these are not present, each level
of the output will be given an automatically generated delimited name.
of the output will be given an automatically generated delimited name.
The precision of the output archive controls the number of decimals output
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)
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
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
those read in. In general you should expect a loss of precision when going
from floating point to text and back.
from floating point to text and back.
JSON archives do not output the size information for any dynamically sized structure
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
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
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
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
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.
that the container is variable sized and may be edited.
\ingroup Archives */
\ingroup Archives */
class JSONOutputArchive : public OutputArchive<JSONOutputArchive>, public traits::TextArchive
class JSONOutputArchive : public OutputArchive<JSONOutputArchive>, public traits::TextArchive
{
{
enum class NodeType { StartObject, InObject, StartArray, InArray };
enum class NodeType { StartObject, InObject, StartArray, InArray };
using WriteStream = CEREAL_RAPIDJSON_NAMESPACE::OStreamWrapper;
using WriteStream = CEREAL_RAPIDJSON_NAMESPACE::OStreamWrapper;
using JSONWriter = CEREAL_RAPIDJSON_NAMESPACE::PrettyWriter<WriteStream>;
using JSONWriter = CEREAL_RAPIDJSON_NAMESPACE::PrettyWriter<WriteStream>;
public:
public:
/*! @name Common Functionality
/*! @name Common Functionality
Common use cases for directly interacting with an JSONOutputArchive */
Common use cases for directly interacting with an JSONOutputArchive */
//! @{
//! @{
//! A class containing various advanced options for the JSON archive
//! A class containing various advanced options for the JSON archive
class Options
class Options
{
{
public:
public:
//! Default options
//! Default options
static Options Default(){ return Options(); }
static Options Default(){ return Options(); }
//! Default options with no indentation
//! Default options with no indentation
static Options NoIndent(){ return Options( JSONWriter::kDefaultMaxDecimalPlaces, IndentChar::space, 0 ); }
static Options NoIndent(){ return Options( JSONWriter::kDefaultMaxDecimalPlaces, IndentChar::space, 0 ); }
//! The character to use for indenting
//! The character to use for indenting
enum class IndentChar : char
enum class IndentChar : char
{
{
space = ' ',
space = ' ',
tab = '\t',
tab = '\t',
newline = '\n',
newline = '\n',
carriage_return = '\r'
carriage_return = '\r'
};
};
//! Specify specific options for the JSONOutputArchive
//! Specify specific options for the JSONOutputArchive
/*! @param precision The precision used for floating point numbers
/*! @param precision The precision used for floating point numbers
@param indentChar The type of character to indent with
@param indentChar The type of character to indent with
@param indentLength The number of indentChar to use for indentation
@param indentLength The number of indentChar to use for indentation
(0 corresponds to no indentation) */
(0 corresponds to no indentation) */
explicit Options( int precision = JSONWriter::kDefaultMaxDecimalPlaces,
explicit Options( int precision = JSONWriter::kDefaultMaxDecimalPlaces,
IndentChar indentChar = IndentChar::space,
IndentChar indentChar = IndentChar::space,
unsigned int indentLength = 4 ) :
unsigned int indentLength = 4 ) :
itsPrecision( precision ),
itsPrecision( precision ),
itsIndentChar( static_cast<char>(indentChar) ),
itsIndentChar( static_cast<char>(indentChar) ),
itsIndentLength( indentLength ) { }
itsIndentLength( indentLength ) { }
private:
private:
friend class JSONOutputArchive;
friend class JSONOutputArchive;
int itsPrecision;
int itsPrecision;
char itsIndentChar;
char itsIndentChar;
unsigned int itsIndentLength;
unsigned int itsIndentLength;
};
};
//! Construct, outputting to the provided stream
//! Construct, outputting to the provided stream
/*! @param stream The stream to output to.
/*! @param stream The stream to output to.
@param options The JSON specific options to use. See the Options struct
@param options The JSON specific options to use. See the Options struct
for the values of default parameters */
for the values of default parameters */
JSONOutputArchive(std::ostream & stream, Options const & options = Options::Default() ) :
JSONOutputArchive(std::ostream & stream, Options const & options = Options::Default() ) :
OutputArchive<JSONOutputArchive>(this),
OutputArchive<JSONOutputArchive>(this),
itsWriteStream(stream),
itsWriteStream(stream),
itsWriter(itsWriteStream),
itsWriter(itsWriteStream),
itsNextName(nullptr)
itsNextName(nullptr)
{
{
itsWriter.SetMaxDecimalPlaces( options.itsPrecision );
itsWriter.SetMaxDecimalPlaces( options.itsPrecision );
itsWriter.SetIndent( options.itsIndentChar, options.itsIndentLength );
itsWriter.SetIndent( options.itsIndentChar, options.itsIndentLength );
itsNameCounter.push(0);
itsNameCounter.push(0);
itsNodeStack.push(NodeType::StartObject);
itsNodeStack.push(NodeType::StartObject);
}
}
//! Destructor, flushes the JSON
//! Destructor, flushes the JSON
~JSONOutputArchive() CEREAL_NOEXCEPT
~JSONOutputArchive() CEREAL_NOEXCEPT
{
{
if (itsNodeStack.top() == NodeType::InObject)
if (itsNodeStack.top() == NodeType::InObject)
itsWriter.EndObject();
itsWriter.EndObject();
else if (itsNodeStack.top() == NodeType::InArray)
else if (itsNodeStack.top() == NodeType::InArray)
itsWriter.EndArray();
itsWriter.EndArray();
}
}
//! Saves some binary data, encoded as a base64 string, with an optional name
//! 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
/*! This will create a new node, optionally named, and insert a value that consists of
the data encoded as a base64 string */
the data encoded as a base64 string */
void saveBinaryValue( const void * data, size_t size, const char * name = nullptr )
void saveBinaryValue( const void * data, size_t size, const char * name = nullptr )
{
{
setNextName( name );
setNextName( name );
writeName();
writeName();
auto base64string = base64::encode( reinterpret_cast<const unsigned char *>( data ), size );
auto base64string = base64::encode( reinterpret_cast<const unsigned char *>( data ), size );
saveValue( base64string );
saveValue( base64string );
};
};
//! @}
//! @}
/*! @name Internal Functionality
/*! @name Internal Functionality
Functionality designed for use by those requiring control over the inner mechanisms of
Functionality designed for use by those requiring control over the inner mechanisms of
the JSONOutputArchive */
the JSONOutputArchive */
//! @{
//! @{
//! Starts a new node in the JSON output
//! Starts a new node in the JSON output
/*! The node can optionally be given a name by calling setNextName prior
/*! The node can optionally be given a name by calling setNextName prior
to creating the node
to creating the node
Nodes only need to be started for types that are themselves objects or arrays */
Nodes only need to be started for types that are themselves objects or arrays */
void startNode()
void startNode()
{
{
writeName();
writeName();
itsNodeStack.push(NodeType::StartObject);
itsNodeStack.push(NodeType::StartObject);
itsNameCounter.push(0);
itsNameCounter.push(0);
}
}
//! Designates the most recently added node as finished
//! Designates the most recently added node as finished
void finishNode()
void finishNode()
{
{
// if we ended up serializing an empty object or array, writeName
// if we ended up serializing an empty object or array, writeName
// will never have been called - so start and then immediately end
// will never have been called - so start and then immediately end
// the object/array.
// the object/array.
//
//
// We'll also end any object/arrays we happen to be in
// We'll also end any object/arrays we happen to be in
switch(itsNodeStack.top())
switch(itsNodeStack.top())
{
{
case NodeType::StartArray:
case NodeType::StartArray:
itsWriter.StartArray();
itsWriter.StartArray();
case NodeType::InArray:
case NodeType::InArray:
itsWriter.EndArray();
itsWriter.EndArray();
break;
break;
case NodeType::StartObject:
case NodeType::StartObject:
itsWriter.StartObject();
itsWriter.StartObject();
case NodeType::InObject:
case NodeType::InObject:
itsWriter.EndObject();
itsWriter.EndObject();
break;
break;
}
}
itsNodeStack.pop();
itsNodeStack.pop();
itsNameCounter.pop();
itsNameCounter.pop();
}
}
//! Sets the name for the next node created with startNode
//! Sets the name for the next node created with startNode
void setNextName( const char * name )
void setNextName( const char * name )
{
{
itsNextName = name;
itsNextName = name;
}
}
//! Saves a bool to the current node
//! Saves a bool to the current node
void saveValue(bool b) { itsWriter.Bool(b); }
void saveValue(bool b) { itsWriter.Bool(b); }
//! Saves an int to the current node
//! Saves an int to the current node
void saveValue(int i) { itsWriter.Int(i); }
void saveValue(int i) { itsWriter.Int(i); }
//! Saves a uint to the current node
//! Saves a uint to the current node
void saveValue(unsigned u) { itsWriter.Uint(u); }
void saveValue(unsigned u) { itsWriter.Uint(u); }
//! Saves an int64 to the current node
//! Saves an int64 to the current node
void saveValue(int64_t i64) { itsWriter.Int64(i64); }
void saveValue(int64_t i64) { itsWriter.Int64(i64); }
//! Saves a uint64 to the current node
//! Saves a uint64 to the current node
void saveValue(uint64_t u64) { itsWriter.Uint64(u64); }
void saveValue(uint64_t u64) { itsWriter.Uint64(u64); }
//! Saves a double to the current node
//! Saves a double to the current node
void saveValue(double d) { itsWriter.Double(d); }
void saveValue(double d) { itsWriter.Double(d); }
//! Saves a string to the current node
//! Saves a string to the current node
void saveValue(std::string const & s) { itsWriter.String(s.c_str(), static_cast<CEREAL_RAPIDJSON_NAMESPACE::SizeType>( s.size() )); }
void saveValue(std::string const & s) { itsWriter.String(s.c_str(), static_cast<CEREAL_RAPIDJSON_NAMESPACE::SizeType>( s.size() )); }
//! Saves a string to the current node
void saveValue( eastl::string const & s ) { itsWriter.String( s.c_str(), static_cast<CEREAL_RAPIDJSON_NAMESPACE::SizeType>( s.size() ) ); }
//! Saves a const char * to the current node
//! Saves a const char * to the current node
void saveValue(char const * s) { itsWriter.String(s); }
void saveValue(char const * s) { itsWriter.String(s); }
//! Saves a nullptr to the current node
//! Saves a nullptr to the current node
void saveValue(std::nullptr_t) { itsWriter.Null(); }
void saveValue(std::nullptr_t) { itsWriter.Null(); }
private:
private:
// Some compilers/OS have difficulty disambiguating the above for various flavors of longs, so we provide
// Some compilers/OS have difficulty disambiguating the above for various flavors of longs, so we provide
// special overloads to handle these cases.
// special overloads to handle these cases.
//! 32 bit signed long saving to current node
//! 32 bit signed long saving to current node
template <class T, traits::EnableIf<sizeof(T) == sizeof(std::int32_t),
template <class T, traits::EnableIf<sizeof(T) == sizeof(std::int32_t),
std::is_signed<T>::value> = traits::sfinae> inline
std::is_signed<T>::value> = traits::sfinae> inline
void saveLong(T l){ saveValue( static_cast<std::int32_t>( l ) ); }
void saveLong(T l){ saveValue( static_cast<std::int32_t>( l ) ); }
//! non 32 bit signed long saving to current node
//! non 32 bit signed long saving to current node
template <class T, traits::EnableIf<sizeof(T) != sizeof(std::int32_t),
template <class T, traits::EnableIf<sizeof(T) != sizeof(std::int32_t),
std::is_signed<T>::value> = traits::sfinae> inline
std::is_signed<T>::value> = traits::sfinae> inline
void saveLong(T l){ saveValue( static_cast<std::int64_t>( l ) ); }
void saveLong(T l){ saveValue( static_cast<std::int64_t>( l ) ); }
//! 32 bit unsigned long saving to current node
//! 32 bit unsigned long saving to current node
template <class T, traits::EnableIf<sizeof(T) == sizeof(std::int32_t),
template <class T, traits::EnableIf<sizeof(T) == sizeof(std::int32_t),
std::is_unsigned<T>::value> = traits::sfinae> inline
std::is_unsigned<T>::value> = traits::sfinae> inline
void saveLong(T lu){ saveValue( static_cast<std::uint32_t>( lu ) ); }
void saveLong(T lu){ saveValue( static_cast<std::uint32_t>( lu ) ); }
//! non 32 bit unsigned long saving to current node
//! non 32 bit unsigned long saving to current node
template <class T, traits::EnableIf<sizeof(T) != sizeof(std::int32_t),
template <class T, traits::EnableIf<sizeof(T) != sizeof(std::int32_t),
std::is_unsigned<T>::value> = traits::sfinae> inline
std::is_unsigned<T>::value> = traits::sfinae> inline
void saveLong(T lu){ saveValue( static_cast<std::uint64_t>( lu ) ); }
void saveLong(T lu){ saveValue( static_cast<std::uint64_t>( lu ) ); }
public:
public:
#ifdef _MSC_VER
#ifdef _MSC_VER
//! MSVC only long overload to current node
//! MSVC only long overload to current node
void saveValue( unsigned long lu ){ saveLong( lu ); };
void saveValue( unsigned long lu ){ saveLong( lu ); };
#else // _MSC_VER
#else // _MSC_VER
//! Serialize a long if it would not be caught otherwise
//! Serialize a long if it would not be caught otherwise
template <class T, traits::EnableIf<std::is_same<T, long>::value,
template <class T, traits::EnableIf<std::is_same<T, long>::value,
!std::is_same<T, std::int32_t>::value,
!std::is_same<T, std::int32_t>::value,
!std::is_same<T, std::int64_t>::value> = traits::sfinae> inline
!std::is_same<T, std::int64_t>::value> = traits::sfinae> inline
void saveValue( T t ){ saveLong( t ); }
void saveValue( T t ){ saveLong( t ); }
//! Serialize an unsigned long if it would not be caught otherwise
//! Serialize an unsigned long if it would not be caught otherwise
template <class T, traits::EnableIf<std::is_same<T, unsigned long>::value,
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::uint32_t>::value,
!std::is_same<T, std::uint64_t>::value> = traits::sfinae> inline
!std::is_same<T, std::uint64_t>::value> = traits::sfinae> inline
void saveValue( T t ){ saveLong( t ); }
void saveValue( T t ){ saveLong( t ); }
#endif // _MSC_VER
#endif // _MSC_VER
//! Save exotic arithmetic as strings to current node
//! Save exotic arithmetic as strings to current node
/*! Handles long long (if distinct from other types), unsigned long (if distinct), and long double */
/*! Handles long long (if distinct from other types), unsigned long (if distinct), and long double */
template <class T, traits::EnableIf<std::is_arithmetic<T>::value,
template <class T, traits::EnableIf<std::is_arithmetic<T>::value,
!std::is_same<T, long>::value,
!std::is_same<T, long>::value,
!std::is_same<T, unsigned long>::value,
!std::is_same<T, unsigned long>::value,
!std::is_same<T, std::int64_t>::value,
!std::is_same<T, std::int64_t>::value,
!std::is_same<T, std::uint64_t>::value,
!std::is_same<T, std::uint64_t>::value,
(sizeof(T) >= sizeof(long double) || sizeof(T) >= sizeof(long long))> = traits::sfinae> inline
(sizeof(T) >= sizeof(long double) || sizeof(T) >= sizeof(long long))> = traits::sfinae> inline
void saveValue(T const & t)
void saveValue(T const & t)
{
{
std::stringstream ss; ss.precision( std::numeric_limits<long double>::max_digits10 );
std::stringstream ss; ss.precision( std::numeric_limits<long double>::max_digits10 );
ss << t;
ss << t;
saveValue( ss.str() );
saveValue( ss.str() );
}
}
//! Write the name of the upcoming node and prepare object/array state
//! Write the name of the upcoming node and prepare object/array state
/*! Since writeName is called for every value that is output, regardless of
/*! 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
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.
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:
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
1. (optional) Set the name for the next node to be created, usually done by an NVP
2. Start the node
2. Start the node
3. (if there is data to save) Write the name of the node (this function)
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)
4. (if there is data to save) Save the data (with saveValue)
5. Finish the node
5. Finish the node
*/
*/
void writeName()
void writeName()
{
{
NodeType const & nodeType = itsNodeStack.top();
NodeType const & nodeType = itsNodeStack.top();
// Start up either an object or an array, depending on state
// Start up either an object or an array, depending on state
if(nodeType == NodeType::StartArray)
if(nodeType == NodeType::StartArray)
{
{
itsWriter.StartArray();
itsWriter.StartArray();
itsNodeStack.top() = NodeType::InArray;
itsNodeStack.top() = NodeType::InArray;
}
}
else if(nodeType == NodeType::StartObject)
else if(nodeType == NodeType::StartObject)
{
{
itsNodeStack.top() = NodeType::InObject;
itsNodeStack.top() = NodeType::InObject;
itsWriter.StartObject();
itsWriter.StartObject();
}
}
// Array types do not output names
// Array types do not output names
if(nodeType == NodeType::InArray) return;
if(nodeType == NodeType::InArray) return;
if(itsNextName == nullptr)
if(itsNextName == nullptr)
{
{
std::string name = "value" + std::to_string( itsNameCounter.top()++ ) + "\0";
std::string name = "value" + std::to_string( itsNameCounter.top()++ ) + "\0";
saveValue(name);
saveValue(name);
}
}
else
else
{
{
saveValue(itsNextName);
saveValue(itsNextName);
itsNextName = nullptr;
itsNextName = nullptr;
}
}
}
}
//! Designates that the current node should be output as an array, not an object
//! Designates that the current node should be output as an array, not an object
void makeArray()
void makeArray()
{
{
itsNodeStack.top() = NodeType::StartArray;
itsNodeStack.top() = NodeType::StartArray;
}
}
//! @}
//! @}
private:
private:
WriteStream itsWriteStream; //!< Rapidjson write stream
WriteStream itsWriteStream; //!< Rapidjson write stream
JSONWriter itsWriter; //!< Rapidjson writer
JSONWriter itsWriter; //!< Rapidjson writer
char const * itsNextName; //!< The next name
char const * itsNextName; //!< The next name
std::stack<uint32_t> itsNameCounter; //!< Counter for creating unique names for unnamed nodes
std::stack<uint32_t> itsNameCounter; //!< Counter for creating unique names for unnamed nodes
std::stack<NodeType> itsNodeStack;
std::stack<NodeType> itsNodeStack;
}; // JSONOutputArchive
}; // JSONOutputArchive
// ######################################################################
// ######################################################################
//! An input archive designed to load data from JSON
//! An input archive designed to load data from JSON
/*! This archive uses RapidJSON to read in a JSON archive.
/*! 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
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.
an RAII fashion, ensuring its destruction after all data has been read.
Input JSON should have been produced by the JSONOutputArchive. Data can
Input JSON should have been produced by the JSONOutputArchive. Data can
only be added to dynamically sized containers (marked by JSON arrays) -
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.
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
Only JSON originating from a JSONOutputArchive is officially supported, but data
from other sources may work if properly formatted.
from other sources may work if properly formatted.
The JSONInputArchive does not require that nodes are loaded in the same
The JSONInputArchive does not require that nodes are loaded in the same
order they were saved by JSONOutputArchive. Using name value pairs (NVPs),
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
it is possible to load in an out of order fashion or otherwise skip/select
specific nodes to load.
specific nodes to load.
The default behavior of the input archive is to read sequentially starting
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
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
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
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
order node, the archive will then proceed back to loading sequentially from
its new position.
its new position.
Consider this simple example where loading of some data is skipped:
Consider this simple example where loading of some data is skipped:
@code{cpp}
@code{cpp}
// imagine the input file has someData(1-9) saved in order at the top level node
// 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( someData1, someData2, someData3 ); // XML loads in the order it sees in the file
ar( cereal::make_nvp( "hello", someData6 ) ); // NVP given does not
ar( cereal::make_nvp( "hello", someData6 ) ); // NVP given does not
// match expected NVP name, so we search
// match expected NVP name, so we search
// for the given NVP and load that value
// for the given NVP and load that value
ar( someData7, someData8, someData9 ); // with no NVP given, loading resumes at its
ar( someData7, someData8, someData9 ); // with no NVP given, loading resumes at its
// current location, proceeding sequentially
// current location, proceeding sequentially
@endcode
@endcode
\ingroup Archives */
\ingroup Archives */
class JSONInputArchive : public InputArchive<JSONInputArchive>, public traits::TextArchive
class JSONInputArchive : public InputArchive<JSONInputArchive>, public traits::TextArchive
{
{
private:
private:
using ReadStream = CEREAL_RAPIDJSON_NAMESPACE::IStreamWrapper;
using ReadStream = CEREAL_RAPIDJSON_NAMESPACE::IStreamWrapper;
typedef CEREAL_RAPIDJSON_NAMESPACE::GenericValue<CEREAL_RAPIDJSON_NAMESPACE::UTF8<>> JSONValue;
typedef CEREAL_RAPIDJSON_NAMESPACE::GenericValue<CEREAL_RAPIDJSON_NAMESPACE::UTF8<>> JSONValue;
typedef JSONValue::ConstMemberIterator MemberIterator;
typedef JSONValue::ConstMemberIterator MemberIterator;
typedef JSONValue::ConstValueIterator ValueIterator;
typedef JSONValue::ConstValueIterator ValueIterator;
typedef CEREAL_RAPIDJSON_NAMESPACE::Document::GenericValue GenericValue;
typedef CEREAL_RAPIDJSON_NAMESPACE::Document::GenericValue GenericValue;
public:
public:
/*! @name Common Functionality
/*! @name Common Functionality
Common use cases for directly interacting with an JSONInputArchive */
Common use cases for directly interacting with an JSONInputArchive */
//! @{
//! @{
//! Construct, reading from the provided stream
//! Construct, reading from the provided stream
/*! @param stream The stream to read from */
/*! @param stream The stream to read from */
JSONInputArchive(std::istream & stream) :
JSONInputArchive(std::istream & stream) :
InputArchive<JSONInputArchive>(this),
InputArchive<JSONInputArchive>(this),
itsNextName( nullptr ),
itsNextName( nullptr ),
itsReadStream(stream)
itsReadStream(stream)
{
{
itsDocument.ParseStream<>(itsReadStream);
itsDocument.ParseStream<>(itsReadStream);
if (itsDocument.IsArray())
if (itsDocument.IsArray())
itsIteratorStack.emplace_back(itsDocument.Begin(), itsDocument.End());
itsIteratorStack.emplace_back(itsDocument.Begin(), itsDocument.End());
else
else
itsIteratorStack.emplace_back(itsDocument.MemberBegin(), itsDocument.MemberEnd());
itsIteratorStack.emplace_back(itsDocument.MemberBegin(), itsDocument.MemberEnd());
}
}
~JSONInputArchive() CEREAL_NOEXCEPT = default;
~JSONInputArchive() CEREAL_NOEXCEPT = default;
//! Loads some binary data, encoded as a base64 string
//! 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
/*! This will automatically start and finish a node to load the data, and can be called directly by
users.
users.
Note that this follows the same ordering rules specified in the class description in regards
Note that this follows the same ordering rules specified in the class description in regards
to loading in/out of order */
to loading in/out of order */
void loadBinaryValue( void * data, size_t size, const char * name = nullptr )
void loadBinaryValue( void * data, size_t size, const char * name = nullptr )
{
{
itsNextName = name;
itsNextName = name;
std::string encoded;
std::string encoded;
loadValue( encoded );
loadValue( encoded );
auto decoded = base64::decode( encoded );
auto decoded = base64::decode( encoded );
if( size != decoded.size() )
if( size != decoded.size() )
throw Exception("Decoded binary data size does not match specified size");
throw Exception("Decoded binary data size does not match specified size");
std::memcpy( data, decoded.data(), decoded.size() );
std::memcpy( data, decoded.data(), decoded.size() );
itsNextName = nullptr;
itsNextName = nullptr;
};
};
private:
private:
//! @}
//! @}
/*! @name Internal Functionality
/*! @name Internal Functionality
Functionality designed for use by those requiring control over the inner mechanisms of
Functionality designed for use by those requiring control over the inner mechanisms of
the JSONInputArchive */
the JSONInputArchive */
//! @{
//! @{
//! An internal iterator that handles both array and object types
//! An internal iterator that handles both array and object types
/*! This class is a variant and holds both types of iterators that
/*! This class is a variant and holds both types of iterators that
rapidJSON supports - one for arrays and one for objects. */
rapidJSON supports - one for arrays and one for objects. */
class Iterator
class Iterator
{
{
public:
public:
Iterator() : itsIndex( 0 ), itsType(Null_) {}
Iterator() : itsIndex( 0 ), itsType(Null_) {}
Iterator(MemberIterator begin, MemberIterator end) :
Iterator(MemberIterator begin, MemberIterator end) :
itsMemberItBegin(begin), itsMemberItEnd(end), itsIndex(0), itsType(Member)
itsMemberItBegin(begin), itsMemberItEnd(end), itsIndex(0), itsType(Member)
{
{
if( std::distance( begin, end ) == 0 )
if( std::distance( begin, end ) == 0 )
itsType = Null_;
itsType = Null_;
}
}
Iterator(ValueIterator begin, ValueIterator end) :
Iterator(ValueIterator begin, ValueIterator end) :
itsValueItBegin(begin), itsValueItEnd(end), itsIndex(0), itsType(Value)
itsValueItBegin(begin), itsValueItEnd(end), itsIndex(0), itsType(Value)
{
{
if( std::distance( begin, end ) == 0 )
if( std::distance( begin, end ) == 0 )
itsType = Null_;
itsType = Null_;
}
}
//! Advance to the next node
//! Advance to the next node
Iterator & operator++()
Iterator & operator++()
{
{
++itsIndex;
++itsIndex;
return *this;
return *this;
}
}
//! Get the value of the current node
//! Get the value of the current node
GenericValue const & value()
GenericValue const & value()
{
{
switch(itsType)
switch(itsType)
{
{
case Value : return itsValueItBegin[itsIndex];
case Value : return itsValueItBegin[itsIndex];
case Member: return itsMemberItBegin[itsIndex].value;
case Member: return itsMemberItBegin[itsIndex].value;
default: throw cereal::Exception("JSONInputArchive internal error: null or empty iterator to object or array!");
default: throw cereal::Exception("JSONInputArchive internal error: null or empty iterator to object or array!");
}
}
}
}
//! Get the name of the current node, or nullptr if it has no name
//! Get the name of the current node, or nullptr if it has no name
const char * name() const
const char * name() const
{
{
if( itsType == Member && (itsMemberItBegin + itsIndex) != itsMemberItEnd )
if( itsType == Member && (itsMemberItBegin + itsIndex) != itsMemberItEnd )
return itsMemberItBegin[itsIndex].name.GetString();
return itsMemberItBegin[itsIndex].name.GetString();
else
else
return nullptr;
return nullptr;
}
}
//! Adjust our position such that we are at the node with the given name
//! Adjust our position such that we are at the node with the given name
/*! @throws Exception if no such named node exists */
/*! @throws Exception if no such named node exists */
inline void search( const char * searchName )
inline void search( const char * searchName )
{
{
const auto len = std::strlen( searchName );
const auto len = std::strlen( searchName );
size_t index = 0;
size_t index = 0;
for( auto it = itsMemberItBegin; it != itsMemberItEnd; ++it, ++index )
for( auto it = itsMemberItBegin; it != itsMemberItEnd; ++it, ++index )
{
{
const auto currentName = it->name.GetString();
const auto currentName = it->name.GetString();
if( ( std::strncmp( searchName, currentName, len ) == 0 ) &&
if( ( std::strncmp( searchName, currentName, len ) == 0 ) &&
( std::strlen( currentName ) == len ) )
( std::strlen( currentName ) == len ) )
{
{
itsIndex = index;
itsIndex = index;
return;
return;
}
}
}
}
throw Exception("JSON Parsing failed - provided NVP (" + std::string(searchName) + ") not found");
throw Exception("JSON Parsing failed - provided NVP (" + std::string(searchName) + ") not found");
}
}
private:
private:
MemberIterator itsMemberItBegin, itsMemberItEnd; //!< The member iterator (object)
MemberIterator itsMemberItBegin, itsMemberItEnd; //!< The member iterator (object)
ValueIterator itsValueItBegin, itsValueItEnd; //!< The value iterator (array)
ValueIterator itsValueItBegin, itsValueItEnd; //!< The value iterator (array)
size_t itsIndex; //!< The current index of this iterator
size_t itsIndex; //!< The current index of this iterator
enum Type {Value, Member, Null_} itsType; //!< Whether this holds values (array) or members (objects) or nothing
enum Type {Value, Member, Null_} itsType; //!< Whether this holds values (array) or members (objects) or nothing
};
};
//! Searches for the expectedName node if it doesn't match the actualName
//! 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
/*! 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
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.
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.
If the name is not found, an exception will be thrown.
Resets the NVP name after called.
Resets the NVP name after called.
@throws Exception if an expectedName is given and not found */
@throws Exception if an expectedName is given and not found */
inline void search()
inline void search()
{
{
// The name an NVP provided with setNextName()
// The name an NVP provided with setNextName()
if( itsNextName )
if( itsNextName )
{
{
// The actual name of the current node
// The actual name of the current node
auto const actualName = itsIteratorStack.back().name();
auto const actualName = itsIteratorStack.back().name();
// Do a search if we don't see a name coming up, or if the names don't match
// Do a search if we don't see a name coming up, or if the names don't match
if( !actualName || std::strcmp( itsNextName, actualName ) != 0 )
if( !actualName || std::strcmp( itsNextName, actualName ) != 0 )
itsIteratorStack.back().search( itsNextName );
itsIteratorSt
}
itsNextName = nullptr;
}
public:
//! Starts a new node, going into its proper iterator
/*! This places an iterator for the next node to be pa