29 #ifndef CEREAL_ARCHIVES_XML_HPP_
30 #define CEREAL_ARCHIVES_XML_HPP_
50 #ifndef CEREAL_XML_STRING_VALUE
54 #define CEREAL_XML_STRING_VALUE "cereal"
63 return c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r';
117 explicit Options(
int precision_ = std::numeric_limits<double>::max_digits10,
119 bool outputType_ =
false,
120 bool sizeAttributes_ =
true ) :
183 itsStream.precision( options.itsPrecision );
184 itsOS << std::boolalpha;
185 itsOS.precision( options.itsPrecision );
206 auto base64string =
base64::encode(
reinterpret_cast<const unsigned char *
>( data ), size );
230 const auto nameString =
itsNodes.top().getValueName();
237 itsNodes.top().node->append_node( node );
257 template <
class T>
inline
260 itsOS.clear();
itsOS.seekp( 0, std::ios::beg );
263 auto strValue =
itsOS.str();
268 strValue.resize(std::strlen(strValue.c_str()));
271 const auto len = strValue.length();
297 template <
class T>
inline
304 const auto nameString = util::demangledName<T>();
327 const char * nm =
nullptr ) :
419 itsData(
std::istreambuf_iterator<char>( stream ),
std::istreambuf_iterator<char>() )
435 throw Exception(
"XML Parsing failed - likely due to invalid characters or invalid naming");
440 if( root ==
nullptr )
441 throw Exception(
"Could not detect cereal root node - likely due to empty or invalid input");
464 if( size != decoded.size() )
465 throw Exception(
"Decoded binary data size does not match specified size");
467 std::memcpy( data, decoded.data(), decoded.size() );
491 auto const expectedName =
itsNodes.top().name;
496 if( expectedName && ( next ==
nullptr || std::strcmp( next->name(), expectedName ) != 0 ) )
498 next =
itsNodes.top().search( expectedName );
500 if( next ==
nullptr )
501 throw Exception(
"XML Parsing failed - provided NVP (" + std::string(expectedName) +
") not found");
524 return itsNodes.top().getChildName();
538 std::istringstream is(
itsNodes.top().node->value() );
539 is.setf( std::ios::boolalpha );
549 value = *
reinterpret_cast<T*
>(
itsNodes.top().node->value() );
572 value =
static_cast<T
>( std::stoul(
itsNodes.top().node->value() ) );
581 value =
static_cast<T
>( std::stoull(
itsNodes.top().node->value() ) );
590 value =
static_cast<T
>( std::stoi(
itsNodes.top().node->value() ) );
595 (
sizeof(T) >
sizeof(
int)),
599 value =
static_cast<T
>( std::stol(
itsNodes.top().node->value() ) );
604 (
sizeof(T) >
sizeof(
long)),
608 value =
static_cast<T
>( std::stoll(
itsNodes.top().node->value() ) );
618 catch( std::out_of_range
const & )
621 std::istringstream is(
itsNodes.top().node->value() );
623 if( std::fpclassify(
value ) != FP_SUBNORMAL )
635 catch( std::out_of_range
const & )
638 std::istringstream is(
itsNodes.top().node->value() );
640 if( std::fpclassify(
value ) != FP_SUBNORMAL )
652 catch( std::out_of_range
const & )
655 std::istringstream is(
itsNodes.top().node->value() );
657 if( std::fpclassify(
value ) != FP_SUBNORMAL )
663 template<
class CharT,
class Traits,
class Alloc>
inline
664 void loadValue( std::basic_string<CharT, Traits, Alloc> & str )
666 std::basic_istringstream<CharT, Traits> is(
itsNodes.top().node->value() );
668 str.assign( std::istreambuf_iterator<CharT, Traits>( is ),
669 std::istreambuf_iterator<CharT, Traits>() );
673 template <
class T>
inline
686 while( node !=
nullptr )
702 child( n->first_node() ),
726 const size_t name_size = rapidxml::internal::measure( searchName );
730 if( rapidxml::internal::compare( new_child->name(), new_child->name_size(), searchName, name_size,
true ) )
771 template <
class T>
inline
776 template <
class T>
inline
783 template <
class T>
inline
788 template <
class T>
inline
795 template <
class T>
inline
800 template <
class T>
inline
807 template <
class T>
inline
813 template <
class T>
inline
820 template <
class T>
inline
829 template <
class T>
inline
835 template <
class T>
inline
839 template <
class T>
inline
890 template <
class T>
inline
898 template <
class T>
inline
907 template <
class T>
inline
912 template <
class T>
inline
935 template<
class CharT,
class Traits,
class Alloc>
inline
942 template<
class CharT,
class Traits,
class Alloc>
inline
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:25318
Main cereal functionality.
#define CEREAL_REGISTER_ARCHIVE(Archive)
Registers a specific Archive type with cereal.
Definition: cereal.hpp:195
A wrapper around data that should be serialized after all non-deferred data.
Definition: helpers.hpp:233
For holding name value pairs.
Definition: helpers.hpp:140
char const * name
Definition: helpers.hpp:167
Type value
Definition: helpers.hpp:168
The base output archive class.
Definition: cereal.hpp:319
A wrapper around size metadata.
Definition: helpers.hpp:313
Type size
Definition: helpers.hpp:326
A class containing various advanced options for the XML archive.
Definition: xml.hpp:107
bool itsOutputType
Definition: xml.hpp:154
bool itsIndent
Definition: xml.hpp:153
Options & precision(int value)
Sets the precision used for floating point numbers.
Definition: xml.hpp:140
Options & sizeAttributes(bool enable)
Whether dynamically sized containers (e.g. vector) output the size=dynamic attribute.
Definition: xml.hpp:146
int itsPrecision
Definition: xml.hpp:152
Options & outputType(bool enable)
Whether to output the type of each serialized object as an attribute.
Definition: xml.hpp:144
Options & indent(bool enable)
Whether to indent each line of XML.
Definition: xml.hpp:142
static Options Default()
Default options.
Definition: xml.hpp:110
Options(int precision_=std::numeric_limits< double >::max_digits10, bool indent_=true, bool outputType_=false, bool sizeAttributes_=true)
Specify specific options for the XMLOutputArchive.
Definition: xml.hpp:117
bool itsSizeAttributes
Definition: xml.hpp:155
An output archive designed to save data to XML.
Definition: xml.hpp:97
rapidxml::xml_document itsXML
The XML document.
Definition: xml.hpp:358
void saveValue(int8_t const &value)
Overload for int8_t prevents them from being serialized as characters.
Definition: xml.hpp:291
bool itsSizeAttributes
Controls whether lists have a size attribute.
Definition: xml.hpp:363
~XMLOutputArchive() CEREAL_NOEXCEPT
Destructor, flushes the XML.
Definition: xml.hpp:189
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.
Definition: xml.hpp:200
void saveValue(uint8_t const &value)
Overload for uint8_t prevents them from being serialized as characters.
Definition: xml.hpp:285
XMLOutputArchive(std::ostream &stream, Options const &options=Options::Default())
Construct, outputting to the provided stream upon destruction.
Definition: xml.hpp:163
void startNode()
Creates a new node that is a child of the node at the top of the stack.
Definition: xml.hpp:227
std::ostream & itsStream
The output stream.
Definition: xml.hpp:357
void insertType()
Causes the type to be appended as an attribute to the most recently made node if output type is set t...
Definition: xml.hpp:298
std::stack< NodeInfo > itsNodes
A stack of nodes added to the document.
Definition: xml.hpp:359
bool hasSizeAttributes() const
Definition: xml.hpp:320
bool itsOutputType
Controls whether type information is printed.
Definition: xml.hpp:361
std::ostringstream itsOS
Used to format strings internally.
Definition: xml.hpp:360
void saveValue(T const &value)
Saves some data, encoded as a string, into the current top level node.
Definition: xml.hpp:258
void setNextName(const char *name)
Sets the name for the next node created with startNode.
Definition: xml.hpp:248
void finishNode()
Designates the most recently added node as finished.
Definition: xml.hpp:242
bool itsIndent
Controls whether indenting is used.
Definition: xml.hpp:362
void appendAttribute(const char *name, const char *value)
Appends an attribute to the current top level node.
Definition: xml.hpp:313
xml_node< Ch > * allocate_node(node_type type, const Ch *name=0, const Ch *value=0, std::size_t name_size=0, std::size_t value_size=0)
Definition: rapidxml.hpp:428
Ch * allocate_string(const Ch *source=0, std::size_t size=0)
Definition: rapidxml.hpp:489
xml_attribute< Ch > * allocate_attribute(const Ch *name=0, const Ch *value=0, std::size_t name_size=0, std::size_t value_size=0)
Definition: rapidxml.hpp:460
Definition: rapidxml.hpp:75
Ch * name() const
Definition: rapidxml.hpp:686
Definition: rapidxml.hpp:1372
void clear()
Definition: rapidxml.hpp:1428
void parse(Ch *text)
Definition: rapidxml.hpp:1394
Definition: rapidxml.hpp:904
void append_attribute(xml_attribute< Ch > *attribute)
Definition: rapidxml.hpp:1230
xml_node< Ch > * next_sibling(const Ch *name_=0, std::size_t name_size_=0, bool case_sensitive=true) const
Definition: rapidxml.hpp:1017
void append_node(xml_node< Ch > *child)
Definition: rapidxml.hpp:1110
xml_node< Ch > * first_node(const Ch *name_=0, std::size_t name_size_=0, bool case_sensitive=true) const
Definition: rapidxml.hpp:949
#define CEREAL_NOEXCEPT
Defines the CEREAL_NOEXCEPT macro to use instead of noexcept.
Definition: macros.hpp:130
std::string decode(std::string const &encoded_string)
Definition: base64.hpp:89
std::string encode(unsigned char const *bytes_to_encode, size_t in_len)
Definition: base64.hpp:48
const int parse_no_data_nodes
Definition: rapidxml.hpp:168
const int print_no_indenting
Printer flag instructing the printer to suppress indenting of XML. See print() function.
Definition: rapidxml_print.hpp:23
@ node_declaration
A declaration node. Name and value are empty. Declaration parameters (version, encoding and standalon...
Definition: rapidxml.hpp:155
@ node_data
A data node. Name is empty. Value contains data text.
Definition: rapidxml.hpp:152
@ node_element
An element node. Name contains element name. Value contains text of first data node.
Definition: rapidxml.hpp:151
const int parse_trim_whitespace
Definition: rapidxml.hpp:243
OutIt print(OutIt out, const xml_node< Ch > &node, int flags=0)
Definition: rapidxml_print.hpp:394
const int parse_declaration_node
Definition: rapidxml.hpp:205
bool isWhitespace(char c)
Returns true if the character is whitespace.
Definition: xml.hpp:61
static const char * CEREAL_XML_STRING
The name given to the root node in a cereal xml archive.
Definition: xml.hpp:58
in certain simple scenarios. They should probably not be used if maximizing performance is the main o...
Definition: access.hpp:42
void epilogue(JSONOutputArchive &, NameValuePair< T > const &)
Epilogue for NVPs for JSON archives.
Definition: json.hpp:780
std::enable_if< std::is_arithmetic< T >::value, void >::type CEREAL_LOAD_FUNCTION_NAME(BinaryInputArchive &ar, T &t)
Loading for POD types from binary.
Definition: binary.hpp:126
std::enable_if< std::is_arithmetic< T >::value, void >::type CEREAL_SAVE_FUNCTION_NAME(BinaryOutputArchive &ar, T const &t)
Saving for POD types to binary.
Definition: binary.hpp:118
void prologue(JSONOutputArchive &, NameValuePair< T > const &)
Prologue for NVPs for JSON archives.
Definition: json.hpp:768
Definition: json.hpp:5678
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1282
unsigned int uint32_t
Definition: stdint.h:126
signed int int32_t
Definition: stdint.h:123
unsigned char uint8_t
Definition: stdint.h:124
signed char int8_t
Definition: stdint.h:121
An exception class thrown when things go wrong at runtime.
Definition: helpers.hpp:49
A struct that contains metadata about a node.
Definition: xml.hpp:325
rapidxml::xml_node * node
A pointer to this node.
Definition: xml.hpp:333
std::string getValueName()
Gets the name for the next child node created from this node.
Definition: xml.hpp:341
const char * name
The name for the next child node.
Definition: xml.hpp:335
size_t counter
The counter for naming child nodes.
Definition: xml.hpp:334
NodeInfo(rapidxml::xml_node<> *n=nullptr, const char *nm=nullptr)
Definition: xml.hpp:326
Type traits only struct used to mark an archive as human readable (text based)
Definition: traits.hpp:1321
#define CEREAL_SETUP_ARCHIVE_TRAITS(InputArchive, OutputArchive)
Sets up traits that relate an input archive to an output archive.
Definition: traits.hpp:169
static const detail::sfinae sfinae
Used as the default value for EnableIf and DisableIf template parameters.
Definition: traits.hpp:88
#define CEREAL_XML_STRING_VALUE
The default name for the root node in a cereal xml archive.
Definition: xml.hpp:54
#define const
Definition: zconf.h:233