29 #ifndef CEREAL_ARCHIVES_JSON_HPP_
30 #define CEREAL_ARCHIVES_JSON_HPP_
44 #ifndef CEREAL_RAPIDJSON_ASSERT_THROWS
45 #define CEREAL_RAPIDJSON_ASSERT_THROWS
49 #ifndef CEREAL_RAPIDJSON_ASSERT
50 #define CEREAL_RAPIDJSON_ASSERT(x) if(!(x)){ \
51 throw ::cereal::RapidJSONException("rapidjson internal assertion failure: " #x); }
55 #ifndef CEREAL_RAPIDJSON_WRITE_DEFAULT_FLAGS
56 #define CEREAL_RAPIDJSON_WRITE_DEFAULT_FLAGS kWriteNanAndInfFlag
60 #ifndef CEREAL_RAPIDJSON_PARSE_DEFAULT_FLAGS
61 #define CEREAL_RAPIDJSON_PARSE_DEFAULT_FLAGS kParseFullPrecisionFlag | kParseNanAndInfFlag
111 using JSONWriter = CEREAL_RAPIDJSON_NAMESPACE::PrettyWriter<WriteStream>;
142 explicit Options(
int precision = JSONWriter::kDefaultMaxDecimalPlaces,
144 unsigned int indentLength = 4 ) :
166 itsWriter.SetMaxDecimalPlaces( options.itsPrecision );
167 itsWriter.SetIndent( options.itsIndentChar, options.itsIndentLength );
189 auto base64string =
base64::encode(
reinterpret_cast<const unsigned char *
>( data ), size );
264 template <
class T>
inline
267 template <
class T>
inline
296 #if defined(_MSC_VER) && _MSC_VER < 1916
301 template <class T, traits::EnableIf<std::is_same<T, long>::value,
322 (
sizeof(T) >=
sizeof(
long double) ||
sizeof(T) >=
sizeof(
long long))> =
traits::sfinae>
inline
325 std::stringstream ss; ss.precision( std::numeric_limits<long double>::max_digits10 );
432 typedef CEREAL_RAPIDJSON_NAMESPACE::GenericValue<CEREAL_RAPIDJSON_NAMESPACE::UTF8<>>
JSONValue;
435 typedef CEREAL_RAPIDJSON_NAMESPACE::Document::GenericValue
GenericValue;
472 if( size != decoded.size() )
473 throw Exception(
"Decoded binary data size does not match specified size");
475 std::memcpy( data, decoded.data(), decoded.size() );
525 default:
throw cereal::Exception(
"JSONInputArchive internal error: null or empty iterator to object or array!");
540 inline void search(
const char * searchName )
542 const auto len = std::strlen( searchName );
546 const auto currentName = it->name.GetString();
547 if( ( std::strncmp( searchName, currentName, len ) == 0 ) &&
548 ( std::strlen( currentName ) == len ) )
555 throw Exception(
"JSON Parsing failed - provided NVP (" + std::string(searchName) +
") not found");
587 if( !actualName || std::strcmp( localNextName, actualName ) != 0 )
671 template <
class T>
inline
674 template <
class T>
inline
682 template <
class T>
inline
687 template <
class T>
inline
692 template <
class T>
inline
697 template <
class T>
inline
703 template <
class T>
inline
710 template <
class T>
inline
719 void stringToNumber( std::string
const & str,
long long & val ) { val = std::stoll( str ); }
721 void stringToNumber( std::string
const & str,
unsigned long long & val ) { val = std::stoull( str ); }
723 void stringToNumber( std::string
const & str,
long double & val ) { val = std::stold( str ); }
734 (
sizeof(T) >=
sizeof(
long double) ||
sizeof(T) >=
sizeof(
long long))> =
traits::sfinae>
767 template <
class T>
inline
772 template <
class T>
inline
779 template <
class T>
inline
785 template <
class T>
inline
792 template <
class T>
inline
797 template <
class T>
inline
804 template <
class T>
inline
810 template <
class T>
inline
818 template <
class T>
inline
825 template <
class T>
inline
832 template <
class T>
inline
837 template <
class T>
inline
913 void prologue( JSONOutputArchive & ar, T
const & )
920 void prologue( JSONInputArchive &, T
const & )
926 void epilogue( JSONOutputArchive &, T
const & )
931 void epilogue( JSONInputArchive &, T
const & )
936 template<
class CharT,
class Traits,
class Alloc>
inline
943 template<
class CharT,
class Traits,
class Alloc>
inline
949 template<
class CharT,
class Traits,
class Alloc>
inline
954 template<
class CharT,
class Traits,
class Alloc>
inline
962 template <
class T>
inline
969 template <
class T>
inline
1005 template<
class CharT,
class Traits,
class Alloc>
inline
1012 template<
class CharT,
class Traits,
class Alloc>
inline
1020 template <
class T>
inline
1027 template <
class T>
inline
#define CEREAL_RAPIDJSON_ASSERT(x)
Definition: json.hpp:50
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
Represents a JSON value. Use Value for UTF8 encoding and default allocator.
Definition: document.h:585
A wrapper around data that should be serialized after all non-deferred data.
Definition: helpers.hpp:233
A class containing various advanced options for the JSON archive.
Definition: json.hpp:120
static Options NoIndent()
Default options with no indentation.
Definition: json.hpp:126
Options(int precision=JSONWriter::kDefaultMaxDecimalPlaces, IndentChar indentChar=IndentChar::space, unsigned int indentLength=4)
Specify specific options for the JSONOutputArchive.
Definition: json.hpp:142
unsigned int itsIndentLength
Definition: json.hpp:153
char itsIndentChar
Definition: json.hpp:152
static Options Default()
Default options.
Definition: json.hpp:123
int itsPrecision
Definition: json.hpp:151
IndentChar
The character to use for indenting.
Definition: json.hpp:130
An output archive designed to save data to JSON.
Definition: json.hpp:107
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: json.hpp:184
std::enable_if<!std::is_same< T, uint64_t >::value &&std::is_same< T, unsigned long long >::value, void >::type saveValue(T val)
Definition: json.hpp:269
void saveValue(bool b)
Saves a bool to the current node.
Definition: json.hpp:246
void finishNode()
Designates the most recently added node as finished.
Definition: json.hpp:212
CEREAL_RAPIDJSON_NAMESPACE::OStreamWrapper WriteStream
Definition: json.hpp:110
void setNextName(const char *name)
Sets the name for the next node created with startNode.
Definition: json.hpp:240
CEREAL_RAPIDJSON_NAMESPACE::PrettyWriter< WriteStream > JSONWriter
Definition: json.hpp:111
std::stack< NodeType > itsNodeStack
Definition: json.hpp:387
void writeName()
Write the name of the upcoming node and prepare object/array state.
Definition: json.hpp:343
void makeArray()
Designates that the current node should be output as an array, not an object.
Definition: json.hpp:375
std::stack< uint32_t > itsNameCounter
Counter for creating unique names for unnamed nodes.
Definition: json.hpp:386
void saveValue(int64_t i64)
Saves an int64 to the current node.
Definition: json.hpp:252
void saveValue(T t)
Serialize a long if it would not be caught otherwise.
Definition: json.hpp:304
void saveValue(int i)
Saves an int to the current node.
Definition: json.hpp:248
~JSONOutputArchive() CEREAL_NOEXCEPT
Destructor, flushes the JSON.
Definition: json.hpp:173
void saveValue(std::nullptr_t)
Saves a nullptr to the current node.
Definition: json.hpp:262
JSONOutputArchive(std::ostream &stream, Options const &options=Options::Default())
Construct, outputting to the provided stream.
Definition: json.hpp:160
NodeType
Definition: json.hpp:108
WriteStream itsWriteStream
Rapidjson write stream.
Definition: json.hpp:383
void saveValue(double d)
Saves a double to the current node.
Definition: json.hpp:256
void saveValue(unsigned u)
Saves a uint to the current node.
Definition: json.hpp:250
char const * itsNextName
The next name.
Definition: json.hpp:385
void saveValue(T const &t)
Save exotic arithmetic as strings to current node.
Definition: json.hpp:323
void startNode()
Starts a new node in the JSON output.
Definition: json.hpp:204
void saveValue(char const *s)
Saves a const char * to the current node.
Definition: json.hpp:260
std::enable_if<!std::is_same< T, int64_t >::value &&std::is_same< T, long long >::value, void >::type saveValue(T val)
Definition: json.hpp:266
void saveLong(T lu)
32 bit unsigned long saving to current node
Definition: json.hpp:288
void saveValue(std::string const &s)
Saves a string to the current node.
Definition: json.hpp:258
void saveLong(T l)
32 bit signed long saving to current node
Definition: json.hpp:278
JSONWriter itsWriter
Rapidjson writer.
Definition: json.hpp:384
void saveValue(uint64_t u64)
Saves a uint64 to the current node.
Definition: json.hpp:254
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
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition: document.h:2513
BasicIStreamWrapper< std::istream > IStreamWrapper
Definition: istreamwrapper.h:119
#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
type
The type the bitset is encoded with.
Definition: bitset.hpp:44
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
CEREAL_SIZE_TYPE size_type
The size type used by cereal.
Definition: helpers.hpp:61
void prologue(JSONOutputArchive &, NameValuePair< T > const &)
Prologue for NVPs for JSON archives.
Definition: json.hpp:768
Definition: json.hpp:5678
BasicOStreamWrapper< std::ostream > OStreamWrapper
Definition: ostreamwrapper.h:72
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1282
CEREAL_RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:384
signed __int64 int64_t
Definition: stdint.h:135
unsigned int uint32_t
Definition: stdint.h:126
signed int int32_t
Definition: stdint.h:123
unsigned __int64 uint64_t
Definition: stdint.h:136
An exception class thrown when things go wrong at runtime.
Definition: helpers.hpp:49
Exception(const std::string &what_)
Definition: helpers.hpp:50
An exception thrown when rapidjson fails an internal assertion.
Definition: json.hpp:40
RapidJSONException(const char *what_)
Definition: json.hpp:40
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
typename detail::EnableIfHelper< Conditions... >::type EnableIf
Provides a way to enable a function if conditions are met.
Definition: traits.hpp:116
static const detail::sfinae sfinae
Used as the default value for EnableIf and DisableIf template parameters.
Definition: traits.hpp:88
#define const
Definition: zconf.h:233