json.hpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. private:
  229. // Some compilers/OS have difficulty disambiguating the above for various flavors of longs, so we provide
  230. // special overloads to handle these cases.
  231. //! 32 bit signed long saving to current node
  232. template <class T, traits::EnableIf<sizeof(T) == sizeof(std::int32_t),
  233. std::is_signed<T>::value> = traits::sfinae> inline
  234. void saveLong(T l){ saveValue( static_cast<std::int32_t>( l ) ); }
  235. //! non 32 bit signed long saving to current node
  236. template <class T, traits::EnableIf<sizeof(T) != sizeof(std::int32_t),
  237. std::is_signed<T>::value> = traits::sfinae> inline
  238. void saveLong(T l){ saveValue( static_cast<std::int64_t>( l ) ); }
  239. //! 32 bit unsigned long saving to current node
  240. template <class T, traits::EnableIf<sizeof(T) == sizeof(std::int32_t),
  241. std::is_unsigned<T>::value> = traits::sfinae> inline
  242. void saveLong(T lu){ saveValue( static_cast<std::uint32_t>( lu ) ); }
  243. //! non 32 bit unsigned long saving to current node
  244. template <class T, traits::EnableIf<sizeof(T) != sizeof(std::int32_t),
  245. std::is_unsigned<T>::value> = traits::sfinae> inline
  246. void saveLong(T lu){ saveValue( static_cast<std::uint64_t>( lu ) ); }
  247. public:
  248. #if defined(_MSC_VER) && _MSC_VER < 1916
  249. //! MSVC only long overload to current node
  250. void saveValue( unsigned long lu ){ saveLong( lu ); };
  251. #else // _MSC_VER
  252. //! Serialize a long if it would not be caught otherwise
  253. template <class T, traits::EnableIf<std::is_same<T, long>::value,
  254. !std::is_same<T, int>::value,
  255. !std::is_same<T, std::int64_t>::value> = traits::sfinae> inline
  256. void saveValue( T t ){ saveLong( t ); }
  257. //! Serialize an unsigned long if it would not be caught otherwise
  258. template <class T, traits::EnableIf<std::is_same<T, unsigned long>::value,
  259. !std::is_same<T, unsigned>::value,
  260. !std::is_same<T, std::uint64_t>::value> = traits::sfinae> inline
  261. void saveValue( T t ){ saveLong( t ); }
  262. #endif // _MSC_VER
  263. //! Save exotic arithmetic as strings to current node
  264. /*! Handles long long (if distinct from other types), unsigned long (if distinct), and long double */
  265. template <class T, traits::EnableIf<std::is_arithmetic<T>::value,
  266. !std::is_same<T, long>::value,
  267. !std::is_same<T, unsigned long>::value,
  268. !std::is_same<T, std::int64_t>::value,
  269. !std::is_same<T, std::uint64_t>::value,
  270. (sizeof(T) >= sizeof(long double) || sizeof(T) >= sizeof(long long))> = traits::sfinae> inline
  271. void saveValue(T const & t)
  272. {
  273. std::stringstream ss; ss.precision( std::numeric_limits<long double>::max_digits10 );
  274. ss << t;
  275. saveValue( ss.str() );
  276. }
  277. //! Write the name of the upcoming node and prepare object/array state
  278. /*! Since writeName is called for every value that is output, regardless of
  279. whether it has a name or not, it is the place where we will do a deferred
  280. check of our node state and decide whether we are in an array or an object.
  281. The general workflow of saving to the JSON archive is:
  282. 1. (optional) Set the name for the next node to be created, usually done by an NVP
  283. 2. Start the node
  284. 3. (if there is data to save) Write the name of the node (this function)
  285. 4. (if there is data to save) Save the data (with saveValue)
  286. 5. Finish the node
  287. */
  288. void writeName()
  289. {
  290. NodeType const & nodeType = itsNodeStack.top();
  291. // Start up either an object or an array, depending on state
  292. if(nodeType == NodeType::StartArray)
  293. {
  294. itsWriter.StartArray();
  295. itsNodeStack.top() = NodeType::InArray;
  296. }
  297. else if(nodeType == NodeType::StartObject)
  298. {
  299. itsNodeStack.top() = NodeType::InObject;
  300. itsWriter.StartObject();
  301. }
  302. // Array types do not output names
  303. if(nodeType == NodeType::InArray) return;
  304. if(itsNextName == nullptr)
  305. {
  306. std::string name = "value" + std::to_string( itsNameCounter.top()++ ) + "\0";
  307. saveValue(name);
  308. }
  309. else
  310. {
  311. saveValue(itsNextName);
  312. itsNextName = nullptr;
  313. }
  314. }
  315. //! Designates that the current node should be output as an array, not an object
  316. void makeArray()
  317. {
  318. itsNodeStack.top() = NodeType::StartArray;
  319. }
  320. //! @}
  321. private:
  322. WriteStream itsWriteStream; //!< Rapidjson write stream
  323. JSONWriter itsWriter; //!< Rapidjson writer
  324. char const * itsNextName; //!< The next name
  325. std::stack<uint32_t> itsNameCounter; //!< Counter for creating unique names for unnamed nodes
  326. std::stack<NodeType> itsNodeStack;
  327. }; // JSONOutputArchive
  328. // ######################################################################
  329. //! An input archive designed to load data from JSON
  330. /*! This archive uses RapidJSON to read in a JSON archive.
  331. As with the output JSON archive, the preferred way to use this archive is in
  332. an RAII fashion, ensuring its destruction after all data has been read.
  333. Input JSON should have been produced by the JSONOutputArchive. Data can
  334. only be added to dynamically sized containers (marked by JSON arrays) -
  335. the input archive will determine their size by looking at the number of child nodes.
  336. Only JSON originating from a JSONOutputArchive is officially supported, but data
  337. from other sources may work if properly formatted.
  338. The JSONInputArchive does not require that nodes are loaded in the same
  339. order they were saved by JSONOutputArchive. Using name value pairs (NVPs),
  340. it is possible to load in an out of order fashion or otherwise skip/select
  341. specific nodes to load.
  342. The default behavior of the input archive is to read sequentially starting
  343. with the first node and exploring its children. When a given NVP does
  344. not match the read in name for a node, the archive will search for that
  345. node at the current level and load it if it exists. After loading an out of
  346. order node, the archive will then proceed back to loading sequentially from
  347. its new position.
  348. Consider this simple example where loading of some data is skipped:
  349. @code{cpp}
  350. // imagine the input file has someData(1-9) saved in order at the top level node
  351. ar( someData1, someData2, someData3 ); // XML loads in the order it sees in the file
  352. ar( cereal::make_nvp( "hello", someData6 ) ); // NVP given does not
  353. // match expected NVP name, so we search
  354. // for the given NVP and load that value
  355. ar( someData7, someData8, someData9 ); // with no NVP given, loading resumes at its
  356. // current location, proceeding sequentially
  357. @endcode
  358. \ingroup Archives */
  359. class JSONInputArchive : public InputArchive<JSONInputArchive>, public traits::TextArchive
  360. {
  361. private:
  362. using ReadStream = CEREAL_RAPIDJSON_NAMESPACE::IStreamWrapper;
  363. typedef CEREAL_RAPIDJSON_NAMESPACE::GenericValue<CEREAL_RAPIDJSON_NAMESPACE::UTF8<>> JSONValue;
  364. typedef JSONValue::ConstMemberIterator MemberIterator;
  365. typedef JSONValue::ConstValueIterator ValueIterator;
  366. typedef CEREAL_RAPIDJSON_NAMESPACE::Document::GenericValue GenericValue;
  367. public:
  368. /*! @name Common Functionality
  369. Common use cases for directly interacting with an JSONInputArchive */
  370. //! @{
  371. //! Construct, reading from the provided stream
  372. /*! @param stream The stream to read from */
  373. JSONInputArchive(std::istream & stream) :
  374. InputArchive<JSONInputArchive>(this),
  375. itsNextName( nullptr ),
  376. itsReadStream(stream)
  377. {
  378. itsDocument.ParseStream<>(itsReadStream);
  379. if (itsDocument.IsArray())
  380. itsIteratorStack.emplace_back(itsDocument.Begin(), itsDocument.End());
  381. else
  382. itsIteratorStack.emplace_back(itsDocument.MemberBegin(), itsDocument.MemberEnd());
  383. }
  384. ~JSONInputArchive() CEREAL_NOEXCEPT = default;
  385. //! Loads some binary data, encoded as a base64 string
  386. /*! This will automatically start and finish a node to load the data, and can be called directly by
  387. users.
  388. Note that this follows the same ordering rules specified in the class description in regards
  389. to loading in/out of order */
  390. void loadBinaryValue( void * data, size_t size, const char * name = nullptr )
  391. {
  392. itsNextName = name;
  393. std::string encoded;
  394. loadValue( encoded );
  395. auto decoded = base64::decode( encoded );
  396. if( size != decoded.size() )
  397. throw Exception("Decoded binary data size does not match specified size");
  398. std::memcpy( data, decoded.data(), decoded.size() );
  399. itsNextName = nullptr;
  400. };
  401. private:
  402. //! @}
  403. /*! @name Internal Functionality
  404. Functionality designed for use by those requiring control over the inner mechanisms of
  405. the JSONInputArchive */
  406. //! @{
  407. //! An internal iterator that handles both array and object types
  408. /*! This class is a variant and holds both types of iterators that
  409. rapidJSON supports - one for arrays and one for objects. */
  410. class Iterator
  411. {
  412. public:
  413. Iterator() : itsIndex( 0 ), itsType(Null_) {}
  414. Iterator(MemberIterator begin, MemberIterator end) :
  415. itsMemberItBegin(begin), itsMemberItEnd(end), itsIndex(0), itsSize(std::distance(begin, end)), itsType(Member)
  416. {
  417. if( itsSize == 0 )
  418. itsType = Null_;
  419. }
  420. Iterator(ValueIterator begin, ValueIterator end) :
  421. itsValueItBegin(begin), itsIndex(0), itsSize(std::distance(begin, end)), itsType(Value)
  422. {
  423. if( itsSize == 0 )
  424. itsType = Null_;
  425. }
  426. //! Advance to the next node
  427. Iterator & operator++()
  428. {
  429. ++itsIndex;
  430. return *this;
  431. }
  432. //! Get the value of the current node
  433. GenericValue const & value()
  434. {
  435. if( itsIndex >= itsSize )
  436. throw cereal::Exception("No more objects in input");
  437. switch(itsType)
  438. {
  439. case Value : return itsValueItBegin[itsIndex];
  440. case Member: return itsMemberItBegin[itsIndex].value;
  441. default: throw cereal::Exception("JSONInputArchive internal error: null or empty iterator to object or array!");
  442. }
  443. }
  444. //! Get the name of the current node, or nullptr if it has no name
  445. const char * name() const
  446. {
  447. if( itsType == Member && (itsMemberItBegin + itsIndex) != itsMemberItEnd )
  448. return itsMemberItBegin[itsIndex].name.GetString();
  449. else
  450. return nullptr;
  451. }
  452. //! Adjust our position such that we are at the node with the given name
  453. /*! @throws Exception if no such named node exists */
  454. inline void search( const char * searchName )
  455. {
  456. const auto len = std::strlen( searchName );
  457. size_t index = 0;
  458. for( auto it = itsMemberItBegin; it != itsMemberItEnd; ++it, ++index )
  459. {
  460. const auto currentName = it->name.GetString();
  461. if( ( std::strncmp( searchName, currentName, len ) == 0 ) &&
  462. ( std::strlen( currentName ) == len ) )
  463. {
  464. itsIndex = index;
  465. return;
  466. }
  467. }
  468. throw Exception("JSON Parsing failed - provided NVP (" + std::string(searchName) + ") not found");
  469. }
  470. private:
  471. MemberIterator itsMemberItBegin, itsMemberItEnd; //!< The member iterator (object)
  472. ValueIterator itsValueItBegin; //!< The value iterator (array)
  473. size_t itsIndex, itsSize; //!< The current index of this iterator
  474. enum Type {Value, Member, Null_} itsType; //!< Whether this holds values (array) or members (objects) or nothing
  475. };
  476. //! Searches for the expectedName node if it doesn't match the actualName
  477. /*! This needs to be called before every load or node start occurs. This function will
  478. check to see if an NVP has been provided (with setNextName) and if so, see if that name matches the actual
  479. next name given. If the names do not match, it will search in the current level of the JSON for that name.
  480. If the name is not found, an exception will be thrown.
  481. Resets the NVP name after called.
  482. @throws Exception if an expectedName is given and not found */
  483. inline void search()
  484. {
  485. // store pointer to itsNextName locally and reset to nullptr in case search() throws
  486. auto localNextName = itsNextName;
  487. itsNextName = nullptr;
  488. // The name an NVP provided with setNextName()
  489. if( localNextName )
  490. {
  491. // The actual name of the current node
  492. auto const actualName = itsIteratorStack.back().name();
  493. // Do a search if we don't see a name coming up, or if the names don't match
  494. if( !actualName || std::strcmp( localNextName, actualName ) != 0 )
  495. itsIteratorStack.back().search( localNextName );
  496. }
  497. }
  498. public:
  499. //! Starts a new node, going into its proper iterator
  500. /*! This places an iterator for the next node to be parsed onto the iterator stack. If the next
  501. node is an array, this will be a value iterator, otherwise it will be a member iterator.
  502. By default our strategy is to start with the document root node and then recursively iterate through
  503. all children in the order they show up in the document.
  504. We don't need to know NVPs to do this; we'll just blindly load in the order things appear in.
  505. If we were given an NVP, we will search for it if it does not match our the name of the next node
  506. that would normally be loaded. This functionality is provided by search(). */
  507. void startNode()
  508. {
  509. search();
  510. if(itsIteratorStack.back().value().IsArray())
  511. itsIteratorStack.emplace_back(itsIteratorStack.back().value().Begin(), itsIteratorStack.back().value().End());
  512. else
  513. itsIteratorStack.emplace_back(itsIteratorStack.back().value().MemberBegin(), itsIteratorStack.back().value().MemberEnd());
  514. }
  515. //! Finishes the most recently started node
  516. void finishNode()
  517. {
  518. itsIteratorStack.pop_back();
  519. ++itsIteratorStack.back();
  520. }
  521. //! Retrieves the current node name
  522. /*! @return nullptr if no name exists */
  523. const char * getNodeName() const
  524. {
  525. return itsIteratorStack.back().name();
  526. }
  527. //! Sets the name for the next node created with startNode
  528. void setNextName( const char * name )
  529. {
  530. itsNextName = name;
  531. }
  532. //! Loads a value from the current node - small signed overload
  533. template <class T, traits::EnableIf<std::is_signed<T>::value,
  534. sizeof(T) < sizeof(int64_t)> = traits::sfinae> inline
  535. void loadValue(T & val)
  536. {
  537. search();
  538. val = static_cast<T>( itsIteratorStack.back().value().GetInt() );
  539. ++itsIteratorStack.back();
  540. }
  541. //! Loads a value from the current node - small unsigned overload
  542. template <class T, traits::EnableIf<std::is_unsigned<T>::value,
  543. sizeof(T) < sizeof(uint64_t),
  544. !std::is_same<bool, T>::value> = traits::sfinae> inline
  545. void loadValue(T & val)
  546. {
  547. search();
  548. val = static_cast<T>( itsIteratorStack.back().value().GetUint() );
  549. ++itsIteratorStack.back();
  550. }
  551. //! Loads a value from the current node - bool overload
  552. void loadValue(bool & val) { search(); val = itsIteratorStack.back().value().GetBool(); ++itsIteratorStack.back(); }
  553. //! Loads a value from the current node - int64 overload
  554. void loadValue(int64_t & val) { search(); val = itsIteratorStack.back().value().GetInt64(); ++itsIteratorStack.back(); }
  555. //! Loads a value from the current node - uint64 overload
  556. void loadValue(uint64_t & val) { search(); val = itsIteratorStack.back().value().GetUint64(); ++itsIteratorStack.back(); }
  557. //! Loads a value from the current node - float overload
  558. void loadValue(float & val) { search(); val = static_cast<float>(itsIteratorStack.back().value().GetDouble()); ++itsIteratorStack.back(); }
  559. //! Loads a value from the current node - double overload
  560. void loadValue(double & val) { search(); val = itsIteratorStack.back().value().GetDouble(); ++itsIteratorStack.back(); }
  561. //! Loads a value from the current node - string overload
  562. void loadValue(std::string & val) { search(); val = itsIteratorStack.back().value().GetString(); ++itsIteratorStack.back(); }
  563. //! Loads a nullptr from the current node
  564. void loadValue(std::nullptr_t&) { search(); CEREAL_RAPIDJSON_ASSERT(itsIteratorStack.back().value().IsNull()); ++itsIteratorStack.back(); }
  565. // Special cases to handle various flavors of long, which tend to conflict with
  566. // the int32_t or int64_t on various compiler/OS combinations. MSVC doesn't need any of this.
  567. #ifndef _MSC_VER
  568. private:
  569. //! 32 bit signed long loading from current node
  570. template <class T> inline
  571. typename std::enable_if<sizeof(T) == sizeof(std::int32_t) && std::is_signed<T>::value, void>::type
  572. loadLong(T & l){ loadValue( reinterpret_cast<std::int32_t&>( l ) ); }
  573. //! non 32 bit signed long loading from current node
  574. template <class T> inline
  575. typename std::enable_if<sizeof(T) == sizeof(std::int64_t) && std::is_signed<T>::value, void>::type
  576. loadLong(T & l){ loadValue( reinterpret_cast<std::int64_t&>( l ) ); }
  577. //! 32 bit unsigned long loading from current node
  578. template <class T> inline
  579. typename std::enable_if<sizeof(T) == sizeof(std::uint32_t) && !std::is_signed<T>::value, void>::type
  580. loadLong(T & lu){ loadValue( reinterpret_cast<std::uint32_t&>( lu ) ); }
  581. //! non 32 bit unsigned long loading from current node
  582. template <class T> inline
  583. typename std::enable_if<sizeof(T) == sizeof(std::uint64_t) && !std::is_signed<T>::value, void>::type
  584. loadLong(T & lu){ loadValue( reinterpret_cast<std::uint64_t&>( lu ) ); }
  585. public:
  586. //! Serialize a long if it would not be caught otherwise
  587. template <class T> inline
  588. typename std::enable_if<std::is_same<T, long>::value &&
  589. sizeof(T) >= sizeof(std::int64_t) &&
  590. !std::is_same<T, std::int64_t>::value, void>::type
  591. loadValue( T & t ){ loadLong(t); }
  592. //! Serialize an unsigned long if it would not be caught otherwise
  593. template <class T> inline
  594. typename std::enable_if<std::is_same<T, unsigned long>::value &&
  595. sizeof(T) >= sizeof(std::uint64_t) &&
  596. !std::is_same<T, std::uint64_t>::value, void>::type
  597. loadValue( T & t ){ loadLong(t); }
  598. #endif // _MSC_VER
  599. private:
  600. //! Convert a string to a long long
  601. void stringToNumber( std::string const & str, long long & val ) { val = std::stoll( str ); }
  602. //! Convert a string to an unsigned long long
  603. void stringToNumber( std::string const & str, unsigned long long & val ) { val = std::stoull( str ); }
  604. //! Convert a string to a long double
  605. void stringToNumber( std::string const & str, long double & val ) { val = std::stold( str ); }
  606. public:
  607. //! Loads a value from the current node - long double and long long overloads
  608. template <class T, traits::EnableIf<std::is_arithmetic<T>::value,
  609. !std::is_same<T, long>::value,
  610. !std::is_same<T, unsigned long>::value,
  611. !std::is_same<T, std::int64_t>::value,
  612. !std::is_same<T, std::uint64_t>::value,
  613. (sizeof(T) >= sizeof(long double) || sizeof(T) >= sizeof(long long))> = traits::sfinae>
  614. inline void loadValue(T & val)
  615. {
  616. std::string encoded;
  617. loadValue( encoded );
  618. stringToNumber( encoded, val );
  619. }
  620. //! Loads the size for a SizeTag
  621. void loadSize(size_type & size)
  622. {
  623. if (itsIteratorStack.size() == 1)
  624. size = itsDocument.Size();
  625. else
  626. size = (itsIteratorStack.rbegin() + 1)->value().Size();
  627. }
  628. //! @}
  629. private:
  630. const char * itsNextName; //!< Next name set by NVP
  631. ReadStream itsReadStream; //!< Rapidjson write stream
  632. std::vector<Iterator> itsIteratorStack; //!< 'Stack' of rapidJSON iterators
  633. CEREAL_RAPIDJSON_NAMESPACE::Document itsDocument; //!< Rapidjson document
  634. };
  635. // ######################################################################
  636. // JSONArchive prologue and epilogue functions
  637. // ######################################################################
  638. // ######################################################################
  639. //! Prologue for NVPs for JSON archives
  640. /*! NVPs do not start or finish nodes - they just set up the names */
  641. template <class T> inline
  642. void prologue( JSONOutputArchive &, NameValuePair<T> const & )
  643. { }
  644. //! Prologue for NVPs for JSON archives
  645. template <class T> inline
  646. void prologue( JSONInputArchive &, NameValuePair<T> const & )
  647. { }
  648. // ######################################################################
  649. //! Epilogue for NVPs for JSON archives
  650. /*! NVPs do not start or finish nodes - they just set up the names */
  651. template <class T> inline
  652. void epilogue( JSONOutputArchive &, NameValuePair<T> const & )
  653. { }
  654. //! Epilogue for NVPs for JSON archives
  655. /*! NVPs do not start or finish nodes - they just set up the names */
  656. template <class T> inline
  657. void epilogue( JSONInputArchive &, NameValuePair<T> const & )
  658. { }
  659. // ######################################################################
  660. //! Prologue for deferred data for JSON archives
  661. /*! Do nothing for the defer wrapper */
  662. template <class T> inline
  663. void prologue( JSONOutputArchive &, DeferredData<T> const & )
  664. { }
  665. //! Prologue for deferred data for JSON archives
  666. template <class T> inline
  667. void prologue( JSONInputArchive &, DeferredData<T> const & )
  668. { }
  669. // ######################################################################
  670. //! Epilogue for deferred 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( JSONOutputArchive &, DeferredData<T> const & )
  674. { }
  675. //! Epilogue for deferred for JSON archives
  676. /*! Do nothing for the defer wrapper */
  677. template <class T> inline
  678. void epilogue( JSONInputArchive &, DeferredData<T> const & )
  679. { }
  680. // ######################################################################
  681. //! Prologue for SizeTags for JSON archives
  682. /*! SizeTags are strictly ignored for JSON, they just indicate
  683. that the current node should be made into an array */
  684. template <class T> inline
  685. void prologue( JSONOutputArchive & ar, SizeTag<T> const & )
  686. {
  687. ar.makeArray();
  688. }
  689. //! Prologue for SizeTags for JSON archives
  690. template <class T> inline
  691. void prologue( JSONInputArchive &, SizeTag<T> const & )
  692. { }
  693. // ######################################################################
  694. //! Epilogue for SizeTags for JSON archives
  695. /*! SizeTags are strictly ignored for JSON */
  696. template <class T> inline
  697. void epilogue( JSONOutputArchive &, SizeTag<T> const & )
  698. { }
  699. //! Epilogue for SizeTags for JSON archives
  700. template <class T> inline
  701. void epilogue( JSONInputArchive &, SizeTag<T> const & )
  702. { }
  703. // ######################################################################
  704. //! Prologue for all other types for JSON archives (except minimal types)
  705. /*! Starts a new node, named either automatically or by some NVP,
  706. that may be given data by the type about to be archived
  707. Minimal types do not start or finish nodes */
  708. template <class T, traits::EnableIf<!std::is_arithmetic<T>::value,
  709. !traits::has_minimal_base_class_serialization<T, traits::has_minimal_output_serialization, JSONOutputArchive>::value,
  710. !traits::has_minimal_output_serialization<T, JSONOutputArchive>::value> = traits::sfinae>
  711. inline void prologue( JSONOutputArchive & ar, T const & )
  712. {
  713. ar.startNode();
  714. }
  715. //! Prologue for all other types for JSON archives
  716. template <class T, traits::EnableIf<!std::is_arithmetic<T>::value,
  717. !traits::has_minimal_base_class_serialization<T, traits::has_minimal_input_serialization, JSONInputArchive>::value,
  718. !traits::has_minimal_input_serialization<T, JSONInputArchive>::value> = traits::sfinae>
  719. inline void prologue( JSONInputArchive & ar, T const & )
  720. {
  721. ar.startNode();
  722. }
  723. // ######################################################################
  724. //! Epilogue for all other types other for JSON archives (except minimal types)
  725. /*! Finishes the node created in the prologue
  726. Minimal types do not start or finish nodes */
  727. template <class T, traits::EnableIf<!std::is_arithmetic<T>::value,
  728. !traits::has_minimal_base_class_serialization<T, traits::has_minimal_output_serialization, JSONOutputArchive>::value,
  729. !traits::has_minimal_output_serialization<T, JSONOutputArchive>::value> = traits::sfinae>
  730. inline void epilogue( JSONOutputArchive & ar, T const & )
  731. {
  732. ar.finishNode();
  733. }
  734. //! Epilogue for all other types other for JSON archives
  735. template <class T, traits::EnableIf<!std::is_arithmetic<T>::value,
  736. !traits::has_minimal_base_class_serialization<T, traits::has_minimal_input_serialization, JSONInputArchive>::value,
  737. !traits::has_minimal_input_serialization<T, JSONInputArchive>::value> = traits::sfinae>
  738. inline void epilogue( JSONInputArchive & ar, T const & )
  739. {
  740. ar.finishNode();
  741. }
  742. // ######################################################################
  743. //! Prologue for arithmetic types for JSON archives
  744. inline
  745. void prologue( JSONOutputArchive & ar, std::nullptr_t const & )
  746. {
  747. ar.writeName();
  748. }
  749. //! Prologue for arithmetic types for JSON archives
  750. inline
  751. void prologue( JSONInputArchive &, std::nullptr_t const & )
  752. { }
  753. // ######################################################################
  754. //! Epilogue for arithmetic types for JSON archives
  755. inline
  756. void epilogue( JSONOutputArchive &, std::nullptr_t const & )
  757. { }
  758. //! Epilogue for arithmetic types for JSON archives
  759. inline
  760. void epilogue( JSONInputArchive &, std::nullptr_t const & )
  761. { }
  762. // ######################################################################
  763. //! Prologue for arithmetic types for JSON archives
  764. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  765. void prologue( JSONOutputArchive & ar, T const & )
  766. {
  767. ar.writeName();
  768. }
  769. //! Prologue for arithmetic types for JSON archives
  770. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  771. void prologue( JSONInputArchive &, T const & )
  772. { }
  773. // ######################################################################
  774. //! Epilogue for arithmetic types for JSON archives
  775. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  776. void epilogue( JSONOutputArchive &, T const & )
  777. { }
  778. //! Epilogue for arithmetic types for JSON archives
  779. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  780. void epilogue( JSONInputArchive &, T const & )
  781. { }
  782. // ######################################################################
  783. //! Prologue for strings for JSON archives
  784. template<class CharT, class Traits, class Alloc> inline
  785. void prologue(JSONOutputArchive & ar, std::basic_string<CharT, Traits, Alloc> const &)
  786. {
  787. ar.writeName();
  788. }
  789. //! Prologue for strings for JSON archives
  790. template<class CharT, class Traits, class Alloc> inline
  791. void prologue(JSONInputArchive &, std::basic_string<CharT, Traits, Alloc> const &)
  792. { }
  793. // ######################################################################
  794. //! Epilogue for strings for JSON archives
  795. template<class CharT, class Traits, class Alloc> inline
  796. void epilogue(JSONOutputArchive &, std::basic_string<CharT, Traits, Alloc> const &)
  797. { }
  798. //! Epilogue for strings for JSON archives
  799. template<class CharT, class Traits, class Alloc> inline
  800. void epilogue(JSONInputArchive &, std::basic_string<CharT, Traits, Alloc> const &)
  801. { }
  802. // ######################################################################
  803. // Common JSONArchive serialization functions
  804. // ######################################################################
  805. //! Serializing NVP types to JSON
  806. template <class T> inline
  807. void CEREAL_SAVE_FUNCTION_NAME( JSONOutputArchive & ar, NameValuePair<T> const & t )
  808. {
  809. ar.setNextName( t.name );
  810. ar( t.value );
  811. }
  812. template <class T> inline
  813. void CEREAL_LOAD_FUNCTION_NAME( JSONInputArchive & ar, NameValuePair<T> & t )
  814. {
  815. ar.setNextName( t.name );
  816. ar( t.value );
  817. }
  818. //! Saving for nullptr to JSON
  819. inline
  820. void CEREAL_SAVE_FUNCTION_NAME(JSONOutputArchive & ar, std::nullptr_t const & t)
  821. {
  822. ar.saveValue( t );
  823. }
  824. //! Loading arithmetic from JSON
  825. inline
  826. void CEREAL_LOAD_FUNCTION_NAME(JSONInputArchive & ar, std::nullptr_t & t)
  827. {
  828. ar.loadValue( t );
  829. }
  830. //! Saving for arithmetic to JSON
  831. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  832. void CEREAL_SAVE_FUNCTION_NAME(JSONOutputArchive & ar, T const & t)
  833. {
  834. ar.saveValue( t );
  835. }
  836. //! Loading arithmetic from JSON
  837. template <class T, traits::EnableIf<std::is_arithmetic<T>::value> = traits::sfinae> inline
  838. void CEREAL_LOAD_FUNCTION_NAME(JSONInputArchive & ar, T & t)
  839. {
  840. ar.loadValue( t );
  841. }
  842. //! saving string to JSON
  843. template<class CharT, class Traits, class Alloc> inline
  844. void CEREAL_SAVE_FUNCTION_NAME(JSONOutputArchive & ar, std::basic_string<CharT, Traits, Alloc> const & str)
  845. {
  846. ar.saveValue( str );
  847. }
  848. //! loading string from JSON
  849. template<class CharT, class Traits, class Alloc> inline
  850. void CEREAL_LOAD_FUNCTION_NAME(JSONInputArchive & ar, std::basic_string<CharT, Traits, Alloc> & str)
  851. {
  852. ar.loadValue( str );
  853. }
  854. // ######################################################################
  855. //! Saving SizeTags to JSON
  856. template <class T> inline
  857. void CEREAL_SAVE_FUNCTION_NAME( JSONOutputArchive &, SizeTag<T> const & )
  858. {
  859. // nothing to do here, we don't explicitly save the size
  860. }
  861. //! Loading SizeTags from JSON
  862. template <class T> inline
  863. void CEREAL_LOAD_FUNCTION_NAME( JSONInputArchive & ar, SizeTag<T> & st )
  864. {
  865. ar.loadSize( st.size );
  866. }
  867. } // namespace cereal
  868. // register archives for polymorphic support
  869. CEREAL_REGISTER_ARCHIVE(cereal::JSONInputArchive)
  870. CEREAL_REGISTER_ARCHIVE(cereal::JSONOutputArchive)
  871. // tie input and output archives together
  872. CEREAL_SETUP_ARCHIVE_TRAITS(cereal::JSONInputArchive, cereal::JSONOutputArchive)
  873. #endif // CEREAL_ARCHIVES_JSON_HPP_