1 /*! \file OicJsonSerializer.hpp
2 \brief JSON input and output archives.
3 Note: this has been customized by Intel(R) for usage in the OIC project.
4 Nearly the entire file is from Cereal (see copyright notice below) other than specified
7 #include of AttributeValue Type
8 JSONOutputArchive::saveValue() to add JSON null value
9 loadAttributeValues to get attribute values out of a map (implemented in OCRepresentation)
13 Copyright (c) 2014, Randolph Voorhies, Shane Grant
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are met:
18 * Redistributions of source code must retain the above copyright
19 notice, this list of conditions and the following disclaimer.
20 * Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23 * Neither the name of cereal nor the
24 names of its contributors may be used to endorse or promote products
25 derived from this software without specific prior written permission.
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
28 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
31 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #ifndef CEREAL_ARCHIVES_JSON_HPP_
40 #define CEREAL_ARCHIVES_JSON_HPP_
42 #include <AttributeValue.h>
43 #include <cereal/cereal.hpp>
44 #include <cereal/details/util.hpp>
47 //! An exception thrown when rapidjson fails an internal assertion
48 /*! @ingroup Utility */
49 struct RapidJSONException : Exception
50 { RapidJSONException( const char * what_ ) : Exception( what_ ) {} };
53 // Override rapidjson assertions to throw exceptions by default
54 #ifndef RAPIDJSON_ASSERT
55 #define RAPIDJSON_ASSERT(x) if(!(x)){ \
56 throw ::cereal::RapidJSONException("rapidjson internal assertion failure: " #x); }
57 #endif // RAPIDJSON_ASSERT
59 #include <cereal/external/rapidjson/writer.h>
60 #include <cereal/external/rapidjson/genericstream.h>
61 #include <cereal/external/rapidjson/reader.h>
62 #include <cereal/external/rapidjson/document.h>
63 #include <cereal/external/base64.hpp>
73 // ######################################################################
74 //! An output archive designed to save data to JSON
75 /*! This archive uses RapidJSON to build serialized data to JSON.
77 JSON archives provides a human readable output but at decreased
78 performance (both in time and space) compared to binary archives.
80 JSON benefits greatly from name-value pairs, which if present, will
81 name the nodes in the output. If these are not present, each level
82 of the output will be given an automatically generated delimited name.
84 The precision of the output archive controls the number of decimals output
85 for floating point numbers and should be sufficiently large (i.e. at least 20)
86 if there is a desire to have binary equality between the numbers output and
87 those read in. In general you should expect a loss of precision when going
88 from floating point to text and back.
90 JSON archives do not output the size information for any dynamically sized structure
91 and instead infer it from the number of children for a node. This means that data
92 can be hand edited for dynamic sized structures and will still be readable. This
93 is accomplished through the cereal::SizeTag object, which will cause the archive
94 to output the data as a JSON array (e.g. marked by [] instead of {}), which indicates
95 that the container is variable sized and may be edited.
98 class JSONOutputArchive : public OutputArchive<JSONOutputArchive>
100 enum class NodeType { StartObject, InObject, StartArray, InArray };
102 typedef rapidjson::GenericWriteStream WriteStream;
103 typedef rapidjson::Writer<WriteStream> JSONWriter;
106 /*! @name Common Functionality
107 Common use cases for directly interacting with an JSONOutputArchive */
110 //! A class containing various advanced options for the JSON archive
115 static Options Default(){ return Options(); }
117 //! Specify specific options for the JSONOutputArchive
118 /*! @param precision The precision used for floating point numbers*/
119 explicit Options( int precision = std::numeric_limits<double>::max_digits10) :
120 itsPrecision( precision ) { }
123 friend class JSONOutputArchive;
127 //! Construct, outputting to the provided stream
128 /*! @param stream The stream to output to.
129 @param options The JSON specific options to use. See the Options struct
130 for the values of default parameters */
131 JSONOutputArchive(std::ostream & stream, Options const & options = Options::Default() ) :
132 OutputArchive<JSONOutputArchive>(this),
133 itsWriteStream(stream),
134 itsWriter(itsWriteStream, options.itsPrecision),
137 itsNameCounter.push(0);
138 itsNodeStack.push(NodeType::StartObject);
141 //! Destructor, flushes the JSON
144 itsWriter.EndObject();
147 //! Saves some binary data, encoded as a base64 string, with an optional name
148 /*! This will create a new node, optionally named, and insert a value that consists of
149 the data encoded as a base64 string */
150 void saveBinaryValue( const void * data, size_t size, const char * name = nullptr )
155 auto base64string = base64::encode( reinterpret_cast<const unsigned char *>( data ), size );
156 saveValue( base64string );
160 /*! @name Internal Functionality
161 Functionality designed for use by those requiring control over the inner mechanisms of
162 the JSONOutputArchive */
165 //! Starts a new node in the JSON output
166 /*! The node can optionally be given a name by calling setNextName prior
169 Nodes only need to be started for types that are themselves objects or arrays */
173 itsNodeStack.push(NodeType::StartObject);
174 itsNameCounter.push(0);
177 //! Designates the most recently added node as finished
180 // if we ended up serializing an empty object or array, writeName
181 // will never have been called - so start and then immediately end
184 // We'll also end any object/arrays we happen to be in
185 switch(itsNodeStack.top())
187 case NodeType::StartArray:
188 itsWriter.StartArray();
189 case NodeType::InArray:
190 itsWriter.EndArray();
192 case NodeType::StartObject:
193 itsWriter.StartObject();
194 case NodeType::InObject:
195 itsWriter.EndObject();
200 itsNameCounter.pop();
203 //! Sets the name for the next node created with startNode
204 void setNextName( const char * name )
209 //! Saves a null to the current node, added by Intel
210 void saveValue() { itsWriter.Null_(); }
211 //! Saves a bool to the current node
212 void saveValue(bool b) { itsWriter.Bool_(b); }
213 //! Saves an int to the current node
214 void saveValue(int i) { itsWriter.Int(i); }
215 //! Saves a uint to the current node
216 void saveValue(unsigned u) { itsWriter.Uint(u); }
217 //! Saves an int64 to the current node
218 void saveValue(int64_t i64) { itsWriter.Int64(i64); }
219 //! Saves a uint64 to the current node
220 void saveValue(uint64_t u64) { itsWriter.Uint64(u64); }
221 //! Saves a double to the current node
222 void saveValue(double d) { itsWriter.Double(d); }
223 //! Saves a string to the current node
224 void saveValue(std::string const & s) { itsWriter.String(s.c_str(), static_cast<rapidjson::SizeType>( s.size() )); }
225 //! Saves a const char * to the current node
226 void saveValue(char const * s) { itsWriter.String(s); }
230 //! MSVC only long overload to current node
231 void saveValue( unsigned long lu ){ saveLong( lu ); };
233 //! Serialize a long if it would not be caught otherwise
234 template <class T> inline
235 typename std::enable_if<std::is_same<T, long>::value &&
236 !std::is_same<T, std::int32_t>::value &&
237 !std::is_same<T, std::int64_t>::value, void>::type
244 //! Serialize an unsigned long if it would not be caught otherwise
245 template <class T> inline
246 typename std::enable_if<std::is_same<T, unsigned long>::value &&
247 !std::is_same<T, std::uint32_t>::value &&
248 !std::is_same<T, std::uint64_t>::value, void>::type
256 //! Save exotic arithmetic as strings to current node
257 /*! Handles long long (if distinct from other types), unsigned long (if distinct), and long double */
258 template<class T> inline
259 typename std::enable_if<std::is_arithmetic<T>::value &&
260 !std::is_same<T, long>::value &&
261 !std::is_same<T, unsigned long>::value &&
262 !std::is_same<T, std::int64_t>::value &&
263 !std::is_same<T, std::uint64_t>::value &&
264 (sizeof(T) >= sizeof(long double) || sizeof(T) >= sizeof(long long)), void>::type
265 saveValue(T const & t)
267 std::stringstream ss; ss.precision( std::numeric_limits<long double>::max_digits10 );
269 saveValue( ss.str() );
273 //! Write the name of the upcoming node and prepare object/array state
274 /*! Since writeName is called for every value that is output, regardless of
275 whether it has a name or not, it is the place where we will do a deferred
276 check of our node state and decide whether we are in an array or an object.
278 The general workflow of saving to the JSON archive is:
280 1. (optional) Set the name for the next node to be created, usually done by an NVP
282 3. (if there is data to save) Write the name of the node (this function)
283 4. (if there is data to save) Save the data (with saveValue)
288 NodeType const & nodeType = itsNodeStack.top();
290 // Start up either an object or an array, depending on state
291 if(nodeType == NodeType::StartArray)
293 itsWriter.StartArray();
294 itsNodeStack.top() = NodeType::InArray;
296 else if(nodeType == NodeType::StartObject)
298 itsNodeStack.top() = NodeType::InObject;
299 itsWriter.StartObject();
302 // Array types do not output names
303 if(nodeType == NodeType::InArray) return;
305 if(itsNextName == nullptr)
307 std::string name = "value" + std::to_string( itsNameCounter.top()++ ) + "\0";
312 saveValue(itsNextName);
313 itsNextName = nullptr;
317 //! Designates that the current node should be output as an array, not an object
320 itsNodeStack.top() = NodeType::StartArray;
326 WriteStream itsWriteStream; //!< Rapidjson write stream
327 JSONWriter itsWriter; //!< Rapidjson writer
328 char const * itsNextName; //!< The next name
329 std::stack<uint32_t> itsNameCounter; //!< Counter for creating unique names for unnamed nodes
330 std::stack<NodeType> itsNodeStack;
331 }; // JSONOutputArchive
333 // ######################################################################
334 //! An input archive designed to load data from JSON
335 /*! This archive uses RapidJSON to read in a JSON archive.
337 Input JSON should have been produced by the JSONOutputArchive. Data can
338 only be added to dynamically sized containers (marked by JSON arrays) -
339 the input archive will determine their size by looking at the number of child nodes.
340 Only JSON originating from a JSONOutputArchive is officially supported, but data
341 from other sources may work if properly formatted.
343 The JSONInputArchive does not require that nodes are loaded in the same
344 order they were saved by JSONOutputArchive. Using name value pairs (NVPs),
345 it is possible to load in an out of order fashion or otherwise skip/select
346 specific nodes to load.
348 The default behavior of the input archive is to read sequentially starting
349 with the first node and exploring its children. When a given NVP does
350 not match the read in name for a node, the archive will search for that
351 node at the current level and load it if it exists. After loading an out of
352 order node, the archive will then proceed back to loading sequentially from
355 Consider this simple example where loading of some data is skipped:
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
368 class JSONInputArchive : public InputArchive<JSONInputArchive>
371 typedef rapidjson::GenericReadStream ReadStream;
372 typedef rapidjson::GenericValue<rapidjson::UTF8<>> JSONValue;
373 typedef JSONValue::ConstMemberIterator MemberIterator;
374 typedef JSONValue::ConstValueIterator ValueIterator;
375 typedef rapidjson::Document::GenericValue GenericValue;
378 /*! @name Common Functionality
379 Common use cases for directly interacting with an JSONInputArchive */
382 //! Construct, reading from the provided stream
383 /*! @param stream The stream to read from */
384 JSONInputArchive(std::istream & stream) :
385 InputArchive<JSONInputArchive>(this),
386 itsNextName( nullptr ),
387 itsReadStream(stream)
389 itsDocument.ParseStream<0>(itsReadStream);
390 itsIteratorStack.emplace_back(itsDocument.MemberBegin(), itsDocument.MemberEnd());
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
397 Note that this follows the same ordering rules specified in the class description in regards
398 to loading in/out of order */
399 void loadBinaryValue( void * data, size_t size, const char * name = nullptr )
404 loadValue( encoded );
405 auto decoded = base64::decode( encoded );
407 if( size != decoded.size() )
408 throw Exception("Decoded binary data size does not match specified size");
410 std::memcpy( data, decoded.data(), decoded.size() );
411 itsNextName = nullptr;
414 // Intel Added this as a custom parsing hook for the AttributeValue map
415 void loadAttributeValues(std::map<std::string, OC::AttributeValue>& map);
419 /*! @name Internal Functionality
420 Functionality designed for use by those requiring control over the inner mechanisms of
421 the JSONInputArchive */
424 //! An internal iterator that handles both array and object types
425 /*! This class is a variant and holds both types of iterators that
426 rapidJSON supports - one for arrays and one for objects. */
430 friend class cereal::JSONInputArchive;
431 Iterator() : itsIndex( 0 ), itsType(Null_) {}
433 Iterator(MemberIterator begin, MemberIterator end) :
434 itsMemberItBegin(begin), itsMemberItEnd(end), itsIndex(0), itsType(Member)
437 Iterator(ValueIterator begin, ValueIterator end) :
438 itsValueItBegin(begin), itsValueItEnd(end), itsIndex(0), itsType(Value)
441 //! Advance to the next node
442 Iterator & operator++()
448 //! Get the value of the current node
449 GenericValue const & value()
453 case Value : return itsValueItBegin[itsIndex];
454 case Member: return itsMemberItBegin[itsIndex].value;
455 default: throw cereal::Exception("Invalid Iterator Type!");
459 //! Get the name of the current node, or nullptr if it has no name
460 const char * name() const
462 if( itsType == Member && (itsMemberItBegin + itsIndex) != itsMemberItEnd )
463 return itsMemberItBegin[itsIndex].name.GetString();
468 //! Adjust our position such that we are at the node with the given name
469 /*! @throws Exception if no such named node exists */
470 inline void search( const char * searchName )
472 const auto len = std::strlen( searchName );
474 for( auto it = itsMemberItBegin; it != itsMemberItEnd; ++it, ++index )
475 if( std::strncmp( searchName, it->name.GetString(), len ) == 0 )
481 throw Exception("JSON Parsing failed - provided NVP not found");
485 MemberIterator itsMemberItBegin, itsMemberItEnd; //!< The member iterator (object)
486 ValueIterator itsValueItBegin, itsValueItEnd; //!< The value iterator (array)
487 size_t itsIndex; //!< The current index of this iterator
488 enum Type {Value, Member, Null_} itsType; //!< Whether this holds values (array) or members (objects) or nothing
491 //! Searches for the expectedName node if it doesn't match the actualName
492 /*! This needs to be called before every load or node start occurs. This function will
493 check to see if an NVP has been provided (with setNextName) and if so, see if that name matches the actual
494 next name given. If the names do not match, it will search in the current level of the JSON for that name.
495 If the name is not found, an exception will be thrown.
497 Resets the NVP name after called.
499 @throws Exception if an expectedName is given and not found */
502 // The name an NVP provided with setNextName()
505 // The actual name of the current node
506 auto const actualName = itsIteratorStack.back().name();
508 // Do a search if we don't see a name coming up, or if the names don't match
509 if( !actualName || std::strcmp( itsNextName, actualName ) != 0 )
510 itsIteratorStack.back().search( itsNextName );
513 itsNextName = nullptr;
517 //! Starts a new node, going into its proper iterator
518 /*! This places an iterator for the next node to be parsed onto the iterator stack. If the next
519 node is an array, this will be a value iterator, otherwise it will be a member iterator.
521 By default our strategy is to start with the document root node and then recursively iterate through
522 all children in the order they show up in the document.
523 We don't need to know NVPs to do this; we'll just blindly load in the order things appear in.
525 If we were given an NVP, we will search for it if it does not match our the name of the next node
526 that would normally be loaded. This functionality is provided by search(). */
531 if(itsIteratorStack.back().value().IsArray())
532 itsIteratorStack.emplace_back(itsIteratorStack.back().value().Begin(), itsIteratorStack.back().value().End());
534 itsIteratorStack.emplace_back(itsIteratorStack.back().value().MemberBegin(), itsIteratorStack.back().value().MemberEnd());
537 //! Finishes the most recently started node
540 itsIteratorStack.pop_back();
541 ++itsIteratorStack.back();
544 //! Sets the name for the next node created with startNode
545 void setNextName( const char * name )
550 //! Loads a value from the current node - small signed overload
551 template<class T> inline
552 typename std::enable_if<std::is_signed<T>::value && sizeof(T) < sizeof(int64_t), void>::type
557 val = itsIteratorStack.back().value().GetInt();
558 ++itsIteratorStack.back();
561 //! Loads a value from the current node - small unsigned overload
562 template<class T> inline
563 typename std::enable_if<(std::is_unsigned<T>::value && sizeof(T) < sizeof(uint64_t)) &&
564 !std::is_same<bool, T>::value, void>::type
569 val = itsIteratorStack.back().value().GetUint();
570 ++itsIteratorStack.back();
573 //! Loads a value from the current node - bool overload
574 void loadValue(bool & val) { search(); val = itsIteratorStack.back().value().GetBool_(); ++itsIteratorStack.back(); }
575 //! Loads a value from the current node - int64 overload
576 void loadValue(int64_t & val) { search(); val = itsIteratorStack.back().value().GetInt64(); ++itsIteratorStack.back(); }
577 //! Loads a value from the current node - uint64 overload
578 void loadValue(uint64_t & val) { search(); val = itsIteratorStack.back().value().GetUint64(); ++itsIteratorStack.back(); }
579 //! Loads a value from the current node - float overload
580 void loadValue(float & val) { search(); val = static_cast<float>(itsIteratorStack.back().value().GetDouble()); ++itsIteratorStack.back(); }
581 //! Loads a value from the current node - double overload
582 void loadValue(double & val) { search(); val = itsIteratorStack.back().value().GetDouble(); ++itsIteratorStack.back(); }
583 //! Loads a value from the current node - string overload
584 void loadValue(std::string & val) { search(); val = itsIteratorStack.back().value().GetString(); ++itsIteratorStack.back(); }
587 //! Convert a string to a long long
588 void stringToNumber( std::string const & str, long long & val ) { val = std::stoll( str ); }
589 //! Convert a string to an unsigned long long
590 void stringToNumber( std::string const & str, unsigned long long & val ) { val = std::stoull( str ); }
591 //! Convert a string to a long double
592 void stringToNumber( std::string const & str, long double & val ) { val = std::stold( str ); }
595 //! Loads a value from the current node - long double and long long overloads
596 template<class T> inline
597 typename std::enable_if<std::is_arithmetic<T>::value &&
598 !std::is_same<T, long>::value &&
599 !std::is_same<T, unsigned long>::value &&
600 !std::is_same<T, std::int64_t>::value &&
601 !std::is_same<T, std::uint64_t>::value &&
602 (sizeof(T) >= sizeof(long double) || sizeof(T) >= sizeof(long long)), void>::type
606 loadValue( encoded );
607 stringToNumber( encoded, val );
610 //! Loads the size for a SizeTag
611 void loadSize(size_type & size)
613 size = (itsIteratorStack.rbegin() + 1)->value().Size();
619 const char * itsNextName; //!< Next name set by NVP
620 ReadStream itsReadStream; //!< Rapidjson write stream
621 std::vector<Iterator> itsIteratorStack; //!< 'Stack' of rapidJSON iterators
622 rapidjson::Document itsDocument; //!< Rapidjson document
625 // ######################################################################
626 // JSONArchive prologue and epilogue functions
627 // ######################################################################
629 // ######################################################################
630 //! Prologue for NVPs for JSON archives
631 /*! NVPs do not start or finish nodes - they just set up the names */
632 template <class T> inline
633 void prologue( JSONOutputArchive &, NameValuePair<T> const & )
636 //! Prologue for NVPs for JSON archives
637 template <class T> inline
638 void prologue( JSONInputArchive &, NameValuePair<T> const & )
641 // ######################################################################
642 //! Epilogue for NVPs for JSON archives
643 /*! NVPs do not start or finish nodes - they just set up the names */
644 template <class T> inline
645 void epilogue( JSONOutputArchive &, NameValuePair<T> const & )
648 //! Epilogue for NVPs for JSON archives
649 /*! NVPs do not start or finish nodes - they just set up the names */
650 template <class T> inline
651 void epilogue( JSONInputArchive &, NameValuePair<T> const & )
654 // ######################################################################
655 //! Prologue for SizeTags for JSON archives
656 /*! SizeTags are strictly ignored for JSON, they just indicate
657 that the current node should be made into an array */
658 template <class T> inline
659 void prologue( JSONOutputArchive & ar, SizeTag<T> const & )
664 //! Prologue for SizeTags for JSON archives
665 template <class T> inline
666 void prologue( JSONInputArchive &, SizeTag<T> const & )
669 // ######################################################################
670 //! Epilogue for SizeTags for JSON archives
671 /*! SizeTags are strictly ignored for JSON */
672 template <class T> inline
673 void epilogue( JSONOutputArchive &, SizeTag<T> const & )
676 //! Epilogue for SizeTags for JSON archives
677 template <class T> inline
678 void epilogue( JSONInputArchive &, SizeTag<T> const & )
681 // ######################################################################
682 //! Prologue for all other types for JSON archives (except minimal types)
683 /*! Starts a new node, named either automatically or by some NVP,
684 that may be given data by the type about to be archived
686 Minimal types do not start or finish nodes */
687 template <class T> inline
688 typename std::enable_if<!std::is_arithmetic<T>::value &&
689 !traits::has_minimal_output_serialization<T, JSONOutputArchive>::value, void>::type
690 prologue( JSONOutputArchive & ar, T const & )
695 //! Prologue for all other types for JSON archives
696 template <class T> inline
697 typename std::enable_if<!std::is_arithmetic<T>::value &&
698 !traits::has_minimal_input_serialization<T, JSONOutputArchive>::value, void>::type
699 prologue( JSONInputArchive & ar, T const & )
704 // ######################################################################
705 //! Epilogue for all other types other for JSON archives (except minimal types
706 /*! Finishes the node created in the prologue
708 Minimal types do not start or finish nodes */
709 template <class T> inline
710 typename std::enable_if<!std::is_arithmetic<T>::value &&
711 !traits::has_minimal_output_serialization<T, JSONOutputArchive>::value, void>::type
712 epilogue( JSONOutputArchive & ar, T const & )
717 //! Epilogue for all other types other for JSON archives
718 template <class T> inline
719 typename std::enable_if<!std::is_arithmetic<T>::value &&
720 !traits::has_minimal_input_serialization<T, JSONOutputArchive>::value, void>::type
721 epilogue( JSONInputArchive & ar, T const & )
726 // ######################################################################
727 //! Prologue for arithmetic types for JSON archives
728 template <class T> inline
729 typename std::enable_if<std::is_arithmetic<T>::value, void>::type
730 prologue( JSONOutputArchive & ar, T const & )
735 //! Prologue for arithmetic types for JSON archives
736 template <class T> inline
737 typename std::enable_if<std::is_arithmetic<T>::value, void>::type
738 prologue( JSONInputArchive &, T const & )
741 // ######################################################################
742 //! Epilogue for arithmetic types for JSON archives
743 template <class T> inline
744 typename std::enable_if<std::is_arithmetic<T>::value, void>::type
745 epilogue( JSONOutputArchive &, T const & )
748 //! Epilogue for arithmetic types for JSON archives
749 template <class T> inline
750 typename std::enable_if<std::is_arithmetic<T>::value, void>::type
751 epilogue( JSONInputArchive &, T const & )
754 // ######################################################################
755 //! Prologue for strings for JSON archives
756 template<class CharT, class Traits, class Alloc> inline
757 void prologue(JSONOutputArchive & ar, std::basic_string<CharT, Traits, Alloc> const &)
762 //! Prologue for strings for JSON archives
763 template<class CharT, class Traits, class Alloc> inline
764 void prologue(JSONInputArchive &, std::basic_string<CharT, Traits, Alloc> const &)
767 // ######################################################################
768 //! Epilogue for strings for JSON archives
769 template<class CharT, class Traits, class Alloc> inline
770 void epilogue(JSONOutputArchive &, std::basic_string<CharT, Traits, Alloc> const &)
773 //! Epilogue for strings for JSON archives
774 template<class CharT, class Traits, class Alloc> inline
775 void epilogue(JSONInputArchive &, std::basic_string<CharT, Traits, Alloc> const &)
778 // ######################################################################
779 // Common JSONArchive serialization functions
780 // ######################################################################
782 //! Serializing NVP types to JSON
783 template <class T> inline
784 void save( JSONOutputArchive & ar, NameValuePair<T> const & t )
786 ar.setNextName( t.name );
790 template <class T> inline
791 void load( JSONInputArchive & ar, NameValuePair<T> & t )
793 ar.setNextName( t.name );
797 //! Saving for arithmetic to JSON
798 template<class T> inline
799 typename std::enable_if<std::is_arithmetic<T>::value, void>::type
800 save(JSONOutputArchive & ar, T const & t)
805 //! Loading arithmetic from JSON
806 template<class T> inline
807 typename std::enable_if<std::is_arithmetic<T>::value, void>::type
808 load(JSONInputArchive & ar, T & t)
813 //! saving string to JSON
814 template<class CharT, class Traits, class Alloc> inline
815 void save(JSONOutputArchive & ar, std::basic_string<CharT, Traits, Alloc> const & str)
820 //! loading string from JSON
821 template<class CharT, class Traits, class Alloc> inline
822 void load(JSONInputArchive & ar, std::basic_string<CharT, Traits, Alloc> & str)
827 // ######################################################################
828 //! Saving SizeTags to JSON
829 template <class T> inline
830 void save( JSONOutputArchive &, SizeTag<T> const & )
832 // nothing to do here, we don't explicitly save the size
835 //! Loading SizeTags from JSON
836 template <class T> inline
837 void load( JSONInputArchive & ar, SizeTag<T> & st )
839 ar.loadSize( st.size );
841 } // namespace cereal
843 // register archives for polymorphic support
844 CEREAL_REGISTER_ARCHIVE(cereal::JSONInputArchive)
845 CEREAL_REGISTER_ARCHIVE(cereal::JSONOutputArchive)
847 #endif // CEREAL_ARCHIVES_JSON_HPP_