json.hpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /*! \file json.hpp
  2. \brief JSON input and output archives */
  3. /*
  4. Copyright (c) 2014, Randolph Voorhies, Shane Grant
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. * Neither the name of the copyright holder nor the
  14. names of its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  20. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef CEREAL_ARCHIVES_JSON_HPP_
  28. #define CEREAL_ARCHIVES_JSON_HPP_
  29. #include "cereal/cereal.hpp"
  30. #include "cereal/details/util.hpp"
  31. namespace cereal
  32. {
  33. //! An exception thrown when rapidjson fails an internal assertion
  34. /*! @ingroup Utility */
  35. struct RapidJSONException : Exception
  36. { RapidJSONException( const char * what_ ) : Exception( what_ ) {} };
  37. }
  38. // Inform rapidjson that assert will throw
  39. #ifndef CEREAL_RAPIDJSON_ASSERT_THROWS
  40. #define CEREAL_RAPIDJSON_ASSERT_THROWS
  41. #endif // CEREAL_RAPIDJSON_ASSERT_THROWS
  42. // Override rapidjson assertions to throw exceptions by default
  43. #ifndef CEREAL_RAPIDJSON_ASSERT
  44. #define CEREAL_RAPIDJSON_ASSERT(x) if(!(x)){ \
  45. throw ::cereal::RapidJSONException("rapidjson internal assertion failure: " #x); }
  46. #endif // RAPIDJSON_ASSERT
  47. // Enable support for parsing of nan, inf, -inf
  48. #ifndef CEREAL_RAPIDJSON_WRITE_DEFAULT_FLAGS
  49. #define CEREAL_RAPIDJSON_WRITE_DEFAULT_FLAGS kWriteNanAndInfFlag
  50. #endif
  51. // Enable support for parsing of nan, inf, -inf
  52. #ifndef CEREAL_RAPIDJSON_PARSE_DEFAULT_FLAGS
  53. #define CEREAL_RAPIDJSON_PARSE_DEFAULT_FLAGS kParseFullPrecisionFlag | kParseNanAndInfFlag
  54. #endif
  55. #include "cereal/external/rapidjson/prettywriter.h"
  56. #include "cereal/external/rapidjson/ostreamwrapper.h"
  57. #include "cereal/external/rapidjson/istreamwrapper.h"
  58. #include "cereal/external/rapidjson/document.h"
  59. #include "cereal/external/base64.hpp"
  60. #include <limits>
  61. #include <sstream>
  62. #include <stack>
  63. #include <vector>
  64. #include <string>
  65. namespace cereal
  66. {
  67. // ######################################################################
  68. //! An output archive designed to save data to JSON
  69. /*! This archive uses RapidJSON to build serialize data to JSON.
  70. JSON archives provides a human readable output but at decreased
  71. performance (both in time and space) compared to binary archives.
  72. JSON archives are only guaranteed to finish flushing their contents
  73. upon destruction and should thus be used in an RAII fashion.
  74. JSON benefits greatly from name-value pairs, which if present, will
  75. name the nodes in the output. If these are not present, each level
  76. of the output will be given an automatically generated delimited name.
  77. The precision of the output archive controls the number of decimals output
  78. for floating point numbers and should be sufficiently large (i.e. at least 20)
  79. if there is a desire to have binary equality between the numbers output and
  80. those read in. In general you should expect a loss of precision when going
  81. from floating point to text and back.
  82. JSON archives do not output the size information for any dynamically sized structure
  83. and instead infer it from the number of children for a node. This means that data
  84. can be hand edited for dynamic sized structures and will still be readable. This
  85. is accomplished through the cereal::SizeTag object, which will cause the archive
  86. to output the data as a JSON array (e.g. marked by [] instead of {}), which indicates
  87. that the container is variable sized and may be edited.
  88. \ingroup Archives */
  89. class JSONOutputArchive : public OutputArchive<JSONOutputArchive>, public traits::TextArchive
  90. {
  91. enum class NodeType { StartObject, InObject, StartArray, InArray };
  92. using WriteStream = CEREAL_RAPIDJSON_NAMESPACE::OStreamWrapper;
  93. using JSONWriter = CEREAL_RAPIDJSON_NAMESPACE::PrettyWriter<WriteStream>;
  94. public:
  95. /*! @name Common Functionality
  96. Common use cases for directly interacting with an JSONOutputArchive */
  97. //! @{
  98. //! A class containing various advanced options for the JSON archive
  99. class Options
  100. {
  101. public:
  102. //! Default options
  103. static Options Default(){ return Options(); }
  104. //! Default options with no indentation
  105. static Options NoIndent(){ return Options( JSONWriter::kDefaultMaxDecimalPlaces, IndentChar::space, 0 ); }
  106. //! The character to use for indenting
  107. enum class IndentChar : char
  108. {
  109. space = ' ',
  110. tab = '\t',
  111. newline = '\n',
  112. carriage_return = '\r'
  113. };
  114. //! Specify specific options for the JSONOutputArchive
  115. /*! @param precision The precision used for floating point numbers
  116. @param indentChar The type of character to indent with
  117. @param indentLength The number of indentChar to use for indentation
  118. (0 corresponds to no indentation) */
  119. explicit Options( int precision = JSONWriter::kDefaultMaxDecimalPlaces,
  120. IndentChar indentChar = IndentChar::space,
  121. unsigned int indentLength = 4 ) :
  122. itsPrecision( precision ),
  123. itsIndentChar( static_cast<char>(indentChar) ),
  124. itsIndentLength( indentLength ) { }
  125. private:
  126. friend class JSONOutputArchive;
  127. int itsPrecision;
  128. char itsIndentChar;
  129. unsigned int itsIndentLength;
  130. };
  131. //! Construct, outputting to the provided stream
  132. /*! @param stream The stream to output to.
  133. @param options The JSON specific options to use. See the Options struct
  134. for the values of default parameters */
  135. JSONOutputArchive(std::ostream & stream, Options const & options = Options::Default() ) :
  136. OutputArchive<JSONOutputArchive>(this),
  137. itsWriteStream(stream),
  138. itsWriter(itsWriteStream),
  139. itsNextName(nullptr)
  140. {
  141. itsWriter.SetMaxDecimalPlaces( options.itsPrecision );
  142. itsWriter.SetIndent( options.itsIndentChar, options.itsIndentLength );
  143. itsNameCounter.push(0);
  144. itsNodeStack.push(NodeType::StartObject);
  145. }
  146. //! Destructor, flushes the JSON
  147. ~JSONOutputArchive() CEREAL_NOEXCEPT
  148. {
  149. if (itsNodeStack.top() == NodeType::InObject)
  150. itsWriter.EndObject();
  151. else if (itsNodeStack.top() == NodeType::InArray)
  152. itsWriter.EndArray();
  153. }
  154. //! Saves some binary data, encoded as a base64 string, with an optional name
  155. /*! This will create a new node, optionally named, and insert a value that consists of
  156. the data encoded as a base64 string */
  157. void saveBinaryValue( const void * data, size_t size, const char * name = nullptr )
  158. {
  159. setNextName( name );
  160. writeName();
  161. auto base64string = base64::encode( reinterpret_cast<const unsigned char *>( data ), size );
  162. saveValue( base64string );
  163. }
  164. //! @}
  165. /*! @name Internal Functionality
  166. Functionality designed for use by those requiring control over the inner mechanisms of
  167. the JSONOutputArchive */
  168. //! @{
  169. //! Starts a new node in the JSON output
  170. /*! The node can optionally be given a name by calling setNextName prior
  171. to creating the node
  172. Nodes only need to be started for types that are themselves objects or arrays */
  173. void startNode()
  174. {
  175. writeName();
  176. itsNodeStack.push(NodeType::StartObject);
  177. itsNameCounter.push(0);
  178. }
  179. //! Designates the most recently added node as finished
  180. void finishNode()
  181. {
  182. // if we ended up serializing an empty object or array, writeName
  183. // will never have been called - so start and then immediately end
  184. // the object/array.
  185. //
  186. // We'll also end any object/arrays we happen to be in
  187. switch(itsNodeStack.top())
  188. {
  189. case NodeType::StartArray:
  190. itsWriter.StartArray();
  191. // fall through
  192. case NodeType::InArray:
  193. itsWriter.EndArray();
  194. break;
  195. case NodeType::StartObject:
  196. itsWriter.StartObject();
  197. // fall through
  198. case NodeType::InObject:
  199. itsWriter.EndObject();
  200. break;
  201. }
  202. itsNodeStack.pop();
  203. itsNameCounter.pop();
  204. }
  205. //! Sets the name for the next node created with startNode
  206. void setNextName( const char * name )
  207. {
  208. itsNextName = name;
  209. }
  210. //! Saves a bool to the current node
  211. void saveValue(bool b) { itsWriter.Bool(b); }
  212. //! Saves an int to the current node
  213. void saveValue(int i) { itsWriter.Int(i); }
  214. //! Saves a uint to the current node
  215. void saveValue(unsigned u) { itsWriter.Uint(u); }
  216. //! Saves an int64 to the current node
  217. void saveValue(int64_t i64) { itsWriter.Int64(i64); }
  218. //! Saves a uint64 to the current node
  219. void saveValue(uint64_t u64) { itsWriter.Uint64(u64); }
  220. //! Saves a double to the current node
  221. void saveValue(double d) { itsWriter.Double(d); }
  222. //! Saves a string to the current node
  223. void saveValue(std::string const & s) { itsWriter.String(s.c_str(), static_cast<CEREAL_RAPIDJSON_NAMESPACE::SizeType>( s.size() )); }
  224. //! Saves a const char * to the current node
  225. void saveValue(char const * s) { itsWriter.String(s); }
  226. //! Saves a nullptr to the current node
  227. void saveValue(std::nullptr_t) { itsWriter.Null(); }
  228. template <class T> inline
  229. typename std::enable_if<!std::is_same<T, int64_t>::value && std::is_same<T, long long>::value, void>::type
  230. saveValue(T val) { itsWriter.Int64(val); }
  231. template <class T> inline
  232. typename std::enable_if<!std::is_same<T, uint64_t>::value && std::is_same<T, unsigned long long>::value, void>::type
  233. saveValue(T val) { itsWriter.Uint64(val); }
  234. private:
  235. // Some compilers/OS have difficulty disambiguating the above for various flavors of longs, so we provide
  236. // special overloads to handle these cases.
  237. //! 32 bit signed long saving to current node
  238. template <class T, traits::EnableIf<sizeof(T) == sizeof(std::int32_t),
  239. std::is_signed<T>::value> = traits::sfinae> inline
  240. void saveLong(T l){ saveValue( static_cast<std::int32_t>( l ) ); }
  241. //! non 32 bit signed long saving to current node
  242. template <class T, traits::EnableIf<sizeof(T) != sizeof(std::int32_t),
  243. std::is_signed<T>::value> = traits::sfinae> inline
  244. void saveLong(T l){ saveValue( static_cast<std::int64_t>( l ) ); }
  245. //! 32 bit unsigned long saving to current node
  246. template <class T, traits::EnableIf<sizeof(T) == sizeof(std::int32_t),
  247. std::is_unsigned<T>::value> = traits::sfinae> inline
  248. void saveLong(T lu){ saveValue( static_cast<std::uint32_t>( lu ) ); }
  249. //! non 32 bit unsigned long saving to current node
  250. template <class T, traits::EnableIf<sizeof(T) != sizeof(std::int32_t),
  251. std::is_unsigned<T>::value> = traits::sfinae> inline
  252. void saveLong(T lu){ saveValue( static_cast<std::uint64_t>( lu ) ); }
  253. public:
  254. #if defined(_MSC_VER) && _MSC_VER < 1916
  255. //! MSVC only long overload to current node
  256. void saveValue( unsigned long lu ){ saveLong( lu ); };
  257. #else // _MSC_VER
  258. //! Serialize a long if it would not be caught otherwise
  259. template <class T, traits::EnableIf<std::is_same<T, long>::value,
  260. !std::is_same<T, int>::value,
  261. !std::is_same<T, std::int64_t>::value> = traits::sfinae> inline
  262. void saveValue( T t ){ saveLong( t ); }
  263. //! Serialize an unsigned long if it would not be caught otherwise
  264. template <class T, traits::EnableIf<std::is_same<T, unsigned long>::value,
  265. !std::is_same<T, unsigned>::value,
  266. !std::is_same<T, std::uint64_t>::value> = traits::sfinae> inline
  267. void saveValue( T t ){ saveLong( t ); }
  268. #endif // _MSC_VER
  269. //! Save exotic arithmetic as strings to current node
  270. /*! Handles long long (if distinct from other types), unsigned long (if distinct), and long double */
  271. template <class T, traits::EnableIf<std::is_arithmetic<T>::value,
  272. !std::is_same<T, long>::value,
  273. !std::is_same<T, unsigned long>::value,
  274. !std::is_same<T, std::int64_t>::value,
  275. !std::is_same<T, std::uint64_t>::value,
  276. !std::is_same<T, long long>::value,
  277. !std::is_same<T, unsigned long long>::value,
  278. (sizeof(T) >= sizeof(long double) || sizeof(T) >= sizeof(long long))> = traits::sfinae> inline
  279. void saveValue(T const & t)
  280. {
  281. std::stringstream ss; ss.precision( std::numeric_limits<long double>::max_digits10 );
  282. ss << t;
  283. saveValue( ss.str() );
  284. }
  285. //! Write the name of the upcoming node and prepare object/array state
  286. /*! Since writeName is called for every value that is output, regardless of
  287. whether it has a name or not, it is the place where we will do a deferred
  288. check of our node state and decide whether we are in an array or an object.
  289. The general workflow of saving to the JSON archive is:
  290. 1. (optional) Set the name for the next node to be created, usually done by an NVP
  291. 2. Start the node
  292. 3. (if there is data to save) Write the name of the node (this function)
  293. 4. (if there is data to save) Save the data (with saveValue)
  294. 5. Finish the node
  295. */
  296. void writeName()
  297. {
  298. NodeType const & nodeType = itsNodeStack.top();
  299. // Start up either an object or an array, depending on state
  300. if(nodeType == NodeType::StartArray)
  301. {
  302. itsWriter.StartArray();
  303. itsNodeStack.top() = NodeType::InArray;
  304. }
  305. else if(nodeType == NodeType::StartObject)
  306. {
  307. itsNodeStack.top() = NodeType::InObject;
  308. itsWriter.StartObject();
  309. }
  310. // Array types do not output names
  311. if(nodeType == NodeType::InArray) return;
  312. if(itsNextName == nullptr)
  313. {
  314. std::string name = "value" + std::to_string( itsNameCounter.top()++ ) + "\0";
  315. saveValue(name);
  316. }
  317. else
  318. {
  319. saveValue(itsNextName);
  320. itsNextName = nullptr;
  321. }
  322. }
  323. //! Designates that the current node should be output as an array, not an object
  324. void makeArray()
  325. {
  326. itsNodeStack.top() = NodeType::StartArray;
  327. }
  328. //! @}
  329. private:
  330. WriteStream itsWriteStream; //!< Rapidjson write stream
  331. JSONWriter itsWriter; //!< Rapidjson writer
  332. char const * itsNextName; //!< The next name
  333. std::stack<uint32_t> itsNameCounter; //!< Counter for creating unique names for unnamed nodes
  334. std::stack<NodeType> itsNodeStack;
  335. }; // JSONOutputArchive
  336. // ######################################################################
  337. //! An input archive designed to load data from JSON
  338. /*! This archive uses RapidJSON to read in a JSON archive.
  339. As with the output JSON archive, the preferred way to use this archive is in
  340. an RAII fashion, ensuring its destruction after all data has been read.
  341. Input JSON should have been produced by the JSONOutputArchive. Data can
  342. only be added to dynamically sized containers (marked by JSON arrays) -
  343. the input archive will determine their size by looking at the number of child nodes.
  344. Only JSON originating from a JSONOutputArchive is officially supported, but data
  345. from other sources may work if properly formatted.
  346. The JSONInputArchive does not require that nodes are loaded in the same
  347. order they were saved by JSONOutputArchive. Using name value pairs (NVPs),
  348. it is possible to load in an out of order fashion or otherwise skip/select
  349. specific nodes to load.
  350. The default behavior of the input archive is to read sequentially starting
  351. with the first node and exploring its children. When a given NVP does
  352. not match the read in name for a node, the archive will search for that
  353. node at the current level and load it if it exists. After loading an out of
  354. order node, the archive will then proceed back to loading sequentially from
  355. its new position.
  356. Consider this simple example where loading of some data is skipped:
  357. @code{cpp}
  358. // imagine the input file has someData(1-9) saved in order at the top level node
  359. ar( someData1, someData2, someData3 ); // XML loads in the order it sees in the file
  360. ar( cereal::make_nvp( "hello", someData6 ) ); // NVP given does not
  361. // match expected NVP name, so we search
  362. // for the given NVP and load that value
  363. ar( someData7, someData8, someData9 ); // with no NVP given, loading resumes at its
  364. // current location, proceeding sequentially
  365. @endcode
  366. \ingroup Archives */
  367. class JSONInputArchive : public InputArchive<JSONInputArchive>, public traits::TextArchive
  368. {
  369. private:
  370. using ReadStream = CEREAL_RAPIDJSON_NAMESPACE::IStreamWrapper;
  371. typedef CEREAL_RAPIDJSON_NAMESPACE::GenericValue<CEREAL_RAPIDJSON_NAMESPACE::UTF8<>> JSONValue;
  372. typedef JSONValue::ConstMemberIterator MemberIterator;
  373. typedef JSONValue::ConstValueIterator ValueIterator;
  374. typedef CEREAL_RAPIDJSON_NAMESPACE::Document::GenericValue GenericValue;
  375. public:
  376. /*! @name Common Functionality
  377. Common use cases for directly interacting with an JSONInputArchive */
  378. //! @{
  379. //! Construct, reading from the provided stream
  380. /*! @param stream The stream to read from */
  381. JSONInputArchive(std::istream & stream) :
  382. InputArchive<JSONInputArchive>(this),
  383. itsNextName( nullptr ),
  384. itsReadStream(stream)
  385. {
  386. itsDocument.ParseStream<>(itsReadStream);
  387. if (itsDocument.IsArray())
  388. itsIteratorStack.emplace_back(itsDocument.Begin(), itsDocument.End());
  389. else
  390. itsIteratorStack.emplace_back(itsDocument.MemberBegin(), itsDocument.MemberEnd());
  391. }
  392. ~JSONInputArchive() CEREAL_NOEXCEPT = default;
  393. //! Loads some binary data, encoded as a base64 string
  394. /*! This will automatically start and finish a node to load the data, and can be called directly by
  395. users.
  396. Note that this follows the same ordering rules specified in the class description in regards
  397. to loading in/out of order */
  398. void loadBinaryValue( void * data, size_t size, const char * name = nullptr )
  399. {
  400. itsNextName = name;
  401. std::string encoded;
  402. loadValue( encoded );
  403. auto decoded = base64::decode( encoded );
  404. if( size != decoded.size() )
  405. throw Exception("Decoded binary data size does not match specified size");
  406. std::memcpy( data, decoded.data(), decoded.size() );
  407. itsNextName = nullptr;
  408. }
  409. private:
  410. //! @}
  411. /*! @name Internal Functionality
  412. Functionality designed for use by those requiring control over the inner mechanisms of
  413. the JSONInputArchive */
  414. //! @{
  415. //! An internal iterator that handles both array and object types
  416. /*! This class is a variant and holds both types of iterators that
  417. rapidJSON supports - one for arrays and one for objects. */
  418. class Iterator
  419. {
  420. public:
  421. Iterator() : itsIndex( 0 ), itsType(Null_) {}
  422. Iterator(MemberIterator begin, MemberIterator end) :
  423. itsMemberItBegin(begin), itsMemberItEnd(end), itsIndex(0), itsSize(std::distance(begin, end)), itsType(Member)
  424. {
  425. if( itsSize == 0 )
  426. itsType = Null_;
  427. }
  428. Iterator(ValueIterator begin, ValueIterator end) :
  429. itsValueItBegin(begin), itsIndex(0), itsSize(std::distance(begin, end)), itsType(Value)
  430. {
  431. if( itsSize == 0 )
  432. itsType = Null_;
  433. }
  434. //! Advance to the next node
  435. Iterator & operator++()
  436. {
  437. ++itsIndex;
  438. return *this;
  439. }
  440. //! Get the value of the current node
  441. GenericValue const & value()
  442. {
  443. if( itsIndex >= itsSize )
  444. throw cereal::Exception("No more objects in input");
  445. switch(itsType)
  446. {
  447. case Value : return itsValueItBegin[itsIndex];
  448. case Member: return itsMemberItBegin[itsIndex].value;
  449. default: throw cereal::Exception("JSONInputArchive internal error: null or empty iterator to object or array!");
  450. }
  451. }
  452. //! Get the name of the current node, or nullptr if it has no name
  453. const char * name() const
  454. {
  455. if( itsType == Member && (itsMemberItBegin + itsIndex) != itsMemberItEnd )
  456. return itsMemberItBegin[itsIndex].name.GetString();
  457. else
  458. return nullptr;
  459. }
  460. //! Adjust our position such that we are at the node with the given name
  461. /*! @throws Exception if no such named node exists */
  462. inline void search( const char * searchName )
  463. {
  464. const auto len = std::strlen( searchName );
  465. size_t index = 0;
  466. for( auto it = itsMemberItBegin; it != itsMemberItEnd; ++it, ++index )
  467. {
  468. const auto currentName = it->name.GetString();
  469. if( ( std::strncmp( searchName, currentName, len ) == 0 ) &&
  470. ( std::strlen( currentName ) == len ) )
  471. {
  472. itsIndex = index;
  473. return;
  474. }
  475. }
  476. throw Exception("JSON Parsing failed - provided NVP (" + std::string(searchName) + ") not found");
  477. }
  478. private:
  479. MemberIterator itsMemberItBegin, itsMemberItEnd; //!< The member iterator (object)
  480. ValueIterator itsValueItBegin; //!< The value iterator (array)
  481. size_t itsIndex, itsSize; //!< The current index of this iterator
  482. enum Type {Value, Member, Null_} itsType; //!< Whether this holds values (array) or members (objects) or nothing
  483. };
  484. //! Searches for the expectedName node if it doesn't match the actualName
  485. /*! This needs to be called before every load or node start occurs. This function will
  486. check to see if an NVP has been provided (with setNextName) and if so, see if that name matches the actual
  487. next name given. If the names do not match, it will search in the current level of the JSON for that name.
  488. If the name is not found, an exception will be thrown.
  489. Resets the NVP name after called.
  490. @throws Exception if an expectedName is given and not found */
  491. inline void search()
  492. {
  493. // store pointer to itsNextName locally and reset to nullptr in case search() throws
  494. auto localNextName = itsNextName;
  495. itsNextName = nullptr;
  496. // The name an NVP provided with setNextName()
  497. if( localNextName )
  498. {
  499. // The actual name of the current node
  500. auto const actualName = itsIteratorStack.back().name();
  501. // Do a search if we don't see a name coming up, or if the names don't match
  502. if( !actualName || std::strcmp( localNextName, actualName ) != 0 )
  503. itsIteratorStack.back().search( localNextName );
  504. }
  505. }
  506. public:
  507. //! Starts a new node, going into its proper iterator
  508. /*! This places an iterator for the next node to be parsed onto the iterator stack. If the next
  509. node is an array, this will be a value iterator, otherwise it will be a member iterator.
  510. By default our strategy is to start with the document root node and then recursively iterate through
  511. all children in the order they show up in the document.
  512. We don't need to know NVPs to do this; we'll just blindly load in the order things appear in.
  513. If we were given an NVP, we will search for it if it does not match our the name of the next node
  514. that would normally be loaded. This functionality is provided by search(). */
  515. void startNode()
  516. {
  517. search();
  518. if(itsIteratorStack.back().value().IsArray())
  519. itsIteratorStack.emplace_back(itsIteratorStack.back().value().Begin(), itsIteratorStack.back().value().End());
  520. else
  521. itsIteratorStack.emplace_back(itsIteratorStack.back().value().MemberBegin(), itsIteratorStack.back().value().MemberEnd());
  522. }
  523. //! Finishes the most recently started node
  524. void finishNode()
  525. {
  526. itsIteratorStack.pop_back();
  527. ++itsIteratorStack.back();
  528. }
  529. //! Retrieves the current node name
  530. /*! @return nullptr if no name exists */
  531. const char * getNodeName() const
  532. {
  533. return itsIteratorStack.back().name();
  534. }
  535. //! Sets the name for the next node created with startNode
  536. void setNextName( const char * name )
  537. {
  538. itsNextName = name;
  539. }
  540. //! Loads a value from the current node - small signed overload
  541. template <class T, traits::EnableIf<std::is_signed<T>::value,
  542. sizeof(T) < sizeof(int64_t)> = traits::sfinae> inline
  543. void loadValue(T & val)
  544. {
  545. search();
  546. val = static_cast<T>( itsIteratorStack.back().value().GetInt() );
  547. ++itsIteratorStack.back();
  548. }
  549. //! Loads a value from the current node - small unsigned overload
  550. template <class T, traits::EnableIf<std::is_unsigned<T>::value,
  551. sizeof(T) < sizeof(uint64_t),
  552. !std::is_same<bool, T>::value> = traits::sfinae> inline
  553. void loadValue(T & val)
  554. {
  555. search();
  556. val = static_cast<T>( itsIteratorStack.back().value().GetUint() );
  557. ++itsIteratorStack.back();
  558. }
  559. //! Loads a value from the current node - bool overload
  560. void loadValue(bool & val) { search(); val = itsIteratorStack.back().value().GetBool(); ++itsIteratorStack.back(); }
  561. //! Loads a value from the current node - int64 overload
  562. void loadValue(int64_t & val) { search(); val = itsIteratorStack.back().value().GetInt64(); ++itsIteratorStack.back(); }
  563. //! Loads a value from the current node - uint64 overload
  564. void loadValue(uint64_t & val) { search(); val = itsIteratorStack.back().value().GetUint64(); ++itsIteratorStack.back(); }
  565. //! Loads a value from the current node - float overload
  566. void loadValue(float & val) { search(); val = static_cast<float>(itsIteratorStack.back().value().GetDouble()); ++itsIteratorStack.back(); }
  567. //! Loads a value from the current node - double overload
  568. void loadValue(double & val) { search(); val = itsIteratorStack.back().value().GetDouble(); ++itsIteratorStack.back(); }
  569. //! Loads a value from the current node - string overload
  570. void loadValue(std::string & val) { search(); val = itsIteratorStack.back().value().GetString(); ++itsIteratorStack.back(); }
  571. //! Loads a nullptr from the current node
  572. void loadValue(std::nullptr_t&) { search(); CEREAL_RAPIDJSON_ASSERT(itsIteratorStack.back().value().IsNull()); ++itsIteratorStack.back(); }
  573. template <class T> inline
  574. typename std::enable_if<!std::is_same<T, int64_t>::value && std::is_same<T, long long>::value, void>::type
  575. loadValue(T & val) { search(); val = itsIteratorStack.back().value().GetInt64(); ++itsIteratorStack.back(); }
  576. template <class T> inline
  577. typename std::enable_if<!std::is_same<T, uint64_t>::value && std::is_same<T, unsigned long long>::value, void>::type
  578. loadValue(T & val) { search(); val = itsIteratorStack.back().value().GetUint64(); ++itsIteratorStack.back(); }
  579. // Special cases to handle various flavors of long, which tend to conflict with
  580. // the int32_t or int64_t on various compiler/OS combinations. MSVC doesn't need any of this.
  581. #ifndef _MSC_VER
  582. private:
  583. //! 32 bit signed long loading from current node
  584. template <class T> inline
  585. typename std::enable_if<sizeof(T) == sizeof(std::int32_t) && std::is_signed<T>::value, void>::type
  586. loadLong(T & l){ loadValue( reinterpret_cast<std::int32_t&>( l ) ); }
  587. //! non 32 bit signed long loading from current node
  588. template <class T> inline
  589. typename std::enable_if<sizeof(T) == sizeof(std::int64_t) && std::is_signed<T>::value, void>::type
  590. loadLong(T & l){ loadValue( reinterpret_cast<std::int64_t&>( l ) ); }
  591. //! 32 bit unsigned long loading from current node
  592. template <class T> inline
  593. typename std::enable_if<sizeof(T) == sizeof(std::uint32_t) && !std::is_signed<T>::value, void>::type
  594. loadLong(T & lu){ loadValue( reinterpret_cast<std::uint32_t&>( lu ) ); }
  595. //! non 32 bit unsigned long loading from current node
  596. template <class T> inline
  597. typename std::enable_if<sizeof(T) == sizeof(std::uint64_t) && !std::is_signed<T>::value, void>::type
  598. loadLong(T & lu){ loadValue( reinterpret_cast<std::uint64_t&>( lu ) ); }
  599. public:
  600. //! Serialize a long if it would not be caught otherwise
  601. template <class T> inline
  602. typename std::enable_if<std::is_same<T, long>::value &&
  603. sizeof(T) >= sizeof(std::int64_t) &&
  604. !std::is_same<T, std::int64_t>::value, void>::type
  605. loadValue( T & t ){ loadLong(t); }
  606. //! Serialize an unsigned long if it would not be caught otherwise
  607. template <class T> inline
  608. typename std::enable_if<std::is_same<T, unsigned long>::value &&
  609. sizeof(T) >= sizeof(std::uint64_t) &&
  610. !std::is_same<T, std::uint64_t>::value, void>::type
  611. loadValue( T & t ){ loadLong(t); }
  612. #endif // _MSC_VER
  613. private:
  614. //! Convert a string to a long long
  615. void stringToNumber( std::string const & str, long long & val ) { val = std::stoll( str ); }
  616. //! Convert a string to an unsigned long long
  617. void stringToNumber( std::string const & str, unsigned long long & val ) { val = std::stoull( str ); }
  618. //! Convert a string to a long double
  619. void stringToNumber( std::string const & str, long double & val ) { val = std::stold( str ); }
  620. public:
  621. //! Loads a value from the current node - long double and long long overloads
  622. template <class T, traits::EnableIf<std::is_arithmetic<T>::value,
  623. !std::is_same<T, long>::value,
  624. !std::is_same<T, unsigned long>::value,
  625. !std::is_same<T, std::int64_t>::value,
  626. !std::is_same<T, std::uint64_t>::value,
  627. !std::is_same<T, long long>::value,
  628. !std::is_same<T, unsigned long long>::value,
  629. (sizeof(T) >= sizeof(long double) || sizeof(T) >= sizeof(long long))> = traits::sfinae>
  630. inline void loadValue(T & val)
  631. {
  632. std::string encoded;
  633. loadValue( encoded );
  634. stringToNumber( encoded, val );
  635. }
  636. //! Loads the size for a SizeTag
  637. void loadSize(size_type & size)
  638. {
  639. if (itsIteratorStack.size() == 1)
  640. size = itsDocument.Size();
  641. else
  642. size = (itsIteratorStack.rbegin() + 1)->value().Size();
  643. }
  644. //! @}
  645. private:
  646. const char * itsNextName; //!< Next name set by NVP
  647. ReadStream itsReadStream; //!< Rapidjson write stream
  648. std::vector<Iterator> itsIteratorStack; //!< 'Stack' of rapidJSON iterators
  649. CEREAL_RAPIDJSON_NAMESPACE::Document itsDocument; //!< Rapidjson document
  650. };
  651. // ######################################################################
  652. // JSONArchive prologue and epilogue functions
  653. // ######################################################################
  654. // ######################################################################
  655. //! Prologue for NVPs for JSON archives
  656. /*! NVPs do not start or finish nodes - they just set up the names */
  657. template <class T> inline
  658. void prologue( JSONOutputArchive &, NameValuePair<T> const & )
  659. { }
  660. //! Prologue for NVPs for JSON archives
  661. template <class T> inline
  662. void prologue( JSONInputArchive &, NameValuePair<T> const & )
  663. { }
  664. // ######################################################################
  665. //! Epilogue for NVPs for JSON archives
  666. /*! NVPs do not start or finish nodes - they just set up the names */
  667. template <class T> inline
  668. void epilogue( JSONOutputArchive &, NameValuePair<T> const & )
  669. { }
  670. //! Epilogue for NVPs for JSON archives
  671. /*! NVPs do not start or finish nodes - they just set up the names */
  672. template <class T> inline
  673. void epilogue( JSONInputArchive &, NameValuePair<T> const & )
  674. { }
  675. // ######################################################################
  676. //! Prologue for deferred data for JSON archives
  677. /*! Do nothing for the defer wrapper */
  678. template <class T> inline
  679. void prologue( JSONOutputArchive &, DeferredData<T> const & )
  680. { }
  681. //! Prologue for deferred data for JSON archives
  682. template <class T> inline
  683. void prologue( JSONInputArchive &, DeferredData<T> const & )
  684. { }
  685. // ######################################################################
  686. //! Epilogue for deferred for JSON archives
  687. /*! NVPs do not start or finish nodes - they just set up the names */
  688. template <class T> inline
  689. void epilogue( JSONOutputArchive &, DeferredData<T> const & )
  690. { }
  691. //! Epilogue for deferred for JSON archives
  692. /*! Do nothing for the defer wrapper */
  693. template <class T> inline
  694. void epilogue( JSONInputArchive &, DeferredData<T> const & )
  695. { }
  696. // ######################################################################
  697. //! Prologue for SizeTags for JSON archives
  698. /*! SizeTags are strictly ignored for JSON, they just indicate
  699. that the current node should be made into an array */
  700. template <class T> inline
  701. void prologue( JSONOutputArchive & ar, SizeTag<T> const & )
  702. {
  703. ar.makeArray();
  704. }
  705. //! Prologue for SizeTags for JSON archives
  706. template <class T> inline
  707. void prologue( JSONInputArchive &, SizeTag<T> const & )
  708. { }
  709. // ######################################################################
  710. //! Epilogue for SizeTags for JSON archives
  711. /*! SizeTags are strictly ignored for JSON */
  712. template <class T> inline
  713. void epilogue( JSONOutputArchive &, SizeTag<T> const & )
  714. { }
  715. //! Epilogue for SizeTags for JSON archives
  716. template <class T> inline
  717. void epilogue( JSONInputArchive &, SizeTag<T> const & )
  718. { }
  719. // ######################################################################
  720. //! Prologue for all other types for JSON archives (except minimal types)
  721. /*! Starts a new node, named either automatically or by some NVP,
  722. that may be given data by the type about to be archived
  723. Minimal types do not start or finish nodes */
  724. template <class T, traits::EnableIf<!std::is_arithmetic<T>::value,
  725. !traits::has_minimal_base_class_serialization<T, traits::has_minimal_output_serialization, JSONOutputArchive>::value,
  726. !traits::has_minimal_output_serialization<T, JSONOutputArchive>::value> = traits::sfinae>
  727. inline void prologue( JSONOutputArchive & ar, T const & )
  728. {
  729. ar.startNode();
  730. }
  731. //! Prologue for all other types for JSON archives
  732. template <class T, traits::EnableIf<!std::is_arithmetic<T>::value,
  733. !traits::has_minimal_base_class_serialization<T, traits::has_minimal_input_serialization, JSONInputArchive>::value,
  734. !traits::has_minimal_input_serialization<T, JSONInputArchive>::value> = traits::sfinae>
  735. inline void prologue( JSONInputArchive & ar, T const & )
  736. {
  737. ar.startNode();
  738. }
  739. // ######################################################################
  740. //! Epilogue for all other types other for JSON archives (except minimal types)
  741. /*! Finishes the node created in the prologue
  742. Minimal types do not start or finish nodes */
  743. template <class T, traits::EnableIf<!std::is_arithmetic<T>::value,
  744. !traits::has_minimal_base_class_serialization<T, traits::has_minimal_output_serialization, JSONOutputArchive>::value,
  745. !traits::has_minimal_output_serialization<T, JSONOutputArchive>::value> = traits::sfinae>
  746. inline void epilogue( JSONOutputArchive & ar, T const & )
  747. {
  748. ar.finishNode();
  749. }
  750. //! Epilogue for all other types other for JSON archives
  751. template <class T, traits::EnableIf<!std::is_arithmetic<T>::value,
  752. !traits::has_minimal_base_class_serialization<T, traits::has_minimal_input_serialization, JSONInputArchive>::value,
  753. !traits::has_minimal_input_serialization<T, JSONInputArchive>::value> = traits::sfinae>
  754. inline void epilogue( JSONInputArchive & ar, T const & )
  755. {
  756. ar.finishNode();
  757. }
  758. // ######################################################################
  759. //! Prologue for arithmetic types for JSON archives
  760. inline
  761. void prologue( JSONOutputArchive & ar, std::nullptr_t const & )
  762. {
  763. ar.writeName();
  764. }
  765. //! Prologue for arithmetic types for JSON archives
  766. inline
  767. void prologue( JSONInputArchive &, std::nullptr_t const & )
  768. { }
  769. // ######################################################################
  770. //! Epilogue for arithmetic types for JSON archives
  771. inline
  772. void epilogue( JSONOutputArchive &, std::nullptr_t const & )
  773. { }
  774. //! Epilogue for arithmetic types for JSON archives
  775. inline
  776. void epilogue( JSONInputArchive &, std::nullptr_t const & )
  777. { }
  778. // ######################################################################
  779. //! Prologue for arithmetic types for JSON archives
  780. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  781. void prologue( JSONOutputArchive & ar, T const & )
  782. {
  783. ar.writeName();
  784. }
  785. //! Prologue for arithmetic types for JSON archives
  786. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  787. void prologue( JSONInputArchive &, T const & )
  788. { }
  789. // ######################################################################
  790. //! Epilogue for arithmetic types for JSON archives
  791. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  792. void epilogue( JSONOutputArchive &, T const & )
  793. { }
  794. //! Epilogue for arithmetic types for JSON archives
  795. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  796. void epilogue( JSONInputArchive &, T const & )
  797. { }
  798. // ######################################################################
  799. //! Prologue for strings for JSON archives
  800. template<class CharT, class Traits, class Alloc> inline
  801. void prologue(JSONOutputArchive & ar, std::basic_string<CharT, Traits, Alloc> const &)
  802. {
  803. ar.writeName();
  804. }
  805. //! Prologue for strings for JSON archives
  806. template<class CharT, class Traits, class Alloc> inline
  807. void prologue(JSONInputArchive &, std::basic_string<CharT, Traits, Alloc> const &)
  808. { }
  809. // ######################################################################
  810. //! Epilogue for strings for JSON archives
  811. template<class CharT, class Traits, class Alloc> inline
  812. void epilogue(JSONOutputArchive &, std::basic_string<CharT, Traits, Alloc> const &)
  813. { }
  814. //! Epilogue for strings for JSON archives
  815. template<class CharT, class Traits, class Alloc> inline
  816. void epilogue(JSONInputArchive &, std::basic_string<CharT, Traits, Alloc> const &)
  817. { }
  818. // ######################################################################
  819. // Common JSONArchive serialization functions
  820. // ######################################################################
  821. //! Serializing NVP types to JSON
  822. template <class T> inline
  823. void CEREAL_SAVE_FUNCTION_NAME( JSONOutputArchive & ar, NameValuePair<T> const & t )
  824. {
  825. ar.setNextName( t.name );
  826. ar( t.value );
  827. }
  828. template <class T> inline
  829. void CEREAL_LOAD_FUNCTION_NAME( JSONInputArchive & ar, NameValuePair<T> & t )
  830. {
  831. ar.setNextName( t.name );
  832. ar( t.value );
  833. }
  834. //! Saving for nullptr to JSON
  835. inline
  836. void CEREAL_SAVE_FUNCTION_NAME(JSONOutputArchive & ar, std::nullptr_t const & t)
  837. {
  838. ar.saveValue( t );
  839. }
  840. //! Loading arithmetic from JSON
  841. inline
  842. void CEREAL_LOAD_FUNCTION_NAME(JSONInputArchive & ar, std::nullptr_t & t)
  843. {
  844. ar.loadValue( t );
  845. }
  846. //! Saving for arithmetic to JSON
  847. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  848. void CEREAL_SAVE_FUNCTION_NAME(JSONOutputArchive & ar, T const & t)
  849. {
  850. ar.saveValue( t );
  851. }
  852. //! Loading arithmetic from JSON
  853. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  854. void CEREAL_LOAD_FUNCTION_NAME(JSONInputArchive & ar, T & t)
  855. {
  856. ar.loadValue( t );
  857. }
  858. //! saving string to JSON
  859. template<class CharT, class Traits, class Alloc> inline
  860. void CEREAL_SAVE_FUNCTION_NAME(JSONOutputArchive & ar, std::basic_string<CharT, Traits, Alloc> const & str)
  861. {
  862. ar.saveValue( str );
  863. }
  864. //! loading string from JSON
  865. template<class CharT, class Traits, class Alloc> inline
  866. void CEREAL_LOAD_FUNCTION_NAME(JSONInputArchive & ar, std::basic_string<CharT, Traits, Alloc> & str)
  867. {
  868. ar.loadValue( str );
  869. }
  870. // ######################################################################
  871. //! Saving SizeTags to JSON
  872. template <class T> inline
  873. void CEREAL_SAVE_FUNCTION_NAME( JSONOutputArchive &, SizeTag<T> const & )
  874. {
  875. // nothing to do here, we don't explicitly save the size
  876. }
  877. //! Loading SizeTags from JSON
  878. template <class T> inline
  879. void CEREAL_LOAD_FUNCTION_NAME( JSONInputArchive & ar, SizeTag<T> & st )
  880. {
  881. ar.loadSize( st.size );
  882. }
  883. } // namespace cereal
  884. // register archives for polymorphic support
  885. CEREAL_REGISTER_ARCHIVE(cereal::JSONInputArchive)
  886. CEREAL_REGISTER_ARCHIVE(cereal::JSONOutputArchive)
  887. // tie input and output archives together
  888. CEREAL_SETUP_ARCHIVE_TRAITS(cereal::JSONInputArchive, cereal::JSONOutputArchive)
  889. #endif // CEREAL_ARCHIVES_JSON_HPP_