2 Copyright (c) 2014, Randolph Voorhies, Shane Grant
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of cereal nor the
13 names of its contributors may be used to endorse or promote products
14 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 RANDOLPH VOORHIES AND SHANE GRANT 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.
28 #include <cereal/cereal.hpp>
29 #include <cereal/archives/binary.hpp>
30 #include <cereal/archives/json.hpp>
32 #include <cereal/types/string.hpp>
33 #include <cereal/types/utility.hpp>
34 #include <cereal/types/memory.hpp>
35 #include <cereal/types/complex.hpp>
36 #include <cereal/types/base_class.hpp>
37 #include <cereal/types/array.hpp>
38 #include <cereal/types/vector.hpp>
39 #include <cereal/types/map.hpp>
41 #include <cereal/external/rapidjson/filestream.h>
51 // ###################################
57 friend class cereal::access;
58 template<class Archive>
59 void serialize(Archive & ar)
65 // ###################################
70 Test2( int x ) : a( x ) {}
74 friend class cereal::access;
76 template<class Archive>
77 void save(Archive & ar) const
82 template<class Archive>
83 void load(Archive & ar)
89 // ###################################
95 template<class Archive>
96 void serialize(Archive & ar, Test3 & t)
103 // ###################################
109 template<class Archive>
110 void save(Archive & ar, Test4 const & t)
115 template<class Archive>
116 void load(Archive & ar, Test4 & t)
125 Private() : a('z') {}
130 friend class cereal::access;
132 template<class Archive>
133 void serialize(Archive & ar)
149 template<class Archive>
150 void serialize(Archive & ar)
161 bool operator==(Everything const & o)
177 SubFixture() : a( 3 ),
190 template<class Archive>
191 void serialize(Archive & ar)
211 SubFixture f1, f2, f3;
222 template<class Archive>
223 void save(Archive & ar) const
228 ar.saveBinaryValue( array, sizeof(int)*4, "cool array man" );
231 template<class Archive>
232 void load(Archive & ar)
237 ar.loadBinaryValue( array, sizeof(int)*4 );
250 AAA() : one( 1 ), two( 2 ), three( { {1, 2, 3}, { 4, 5, 6 }, {} } ) {}
253 std::vector<std::vector<int>> three;
255 template<class Archive>
256 void serialize(Archive & ar)
258 ar( CEREAL_NVP(one), CEREAL_NVP(two) );
259 //ar( CEREAL_NVP(three) );
270 std::vector<std::complex<float>> t1{ {0, -1.0f},
273 std::vector<std::complex<float>> t2{ {1.0f, 0},
276 data["imaginary"] = t1;
281 std::map<std::string, std::vector<std::complex<float>>> data;
283 friend class cereal::access;
285 template <class Archive>
286 void serialize( Archive & ar )
288 ar( CEREAL_NVP(data) );
295 OOJson( int aa, int bb, bool cc, double dd ) :
296 a( aa ), b( bb ), c{ cc, dd }
298 d[0] = 0; d[1] = 1; d[2] = 2;
303 std::pair<bool, double> c;
306 template <class Archive>
307 void serialize( Archive & ar )
322 template <class Archive>
323 void save( Archive & ar, Bla const & b )
325 ar( (const int &)b );
328 template <class Archive>
329 void load( Archive & ar, Bla & b )
334 CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES( Bla, cereal::specialization::non_member_load_save )
338 // //template <class Archive> struct specialize<Archive, Bla, cereal::specialization::non_member_load_save> {};
341 // ######################################################################
344 std::cout << std::boolalpha << std::endl;
347 std::ofstream os("file.json");
348 cereal::JSONOutputArchive oar( os );
350 //auto f = std::make_shared<Fixture>();
354 Stuff s; s.fillData();
355 oar( cereal::make_nvp("best data ever", s) );
359 std::ifstream is("file.json");
360 std::string str((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>());
361 std::cout << "---------------------" << std::endl << str << std::endl << "---------------------" << std::endl;
366 cereal::JSONOutputArchive archive( std::cout );
367 bool arr[] = {true, false};
368 std::vector<int> vec = {1, 2, 3, 4, 5};
369 archive( CEREAL_NVP(vec),
371 auto f = std::make_shared<Fixture>();
380 std::stringstream oos;
382 cereal::JSONOutputArchive ar(oos);
383 cereal::JSONOutputArchive ar2(std::cout,
384 cereal::JSONOutputArchive::Options(2, cereal::JSONOutputArchive::Options::IndentChar::space, 2) );
386 ar( cereal::make_nvp( "1", 1 ),
387 cereal::make_nvp( "2", 2 ),
390 cereal::make_nvp( "4", 4 ),
391 cereal::make_nvp( "5", 5 ) );
394 ar.saveBinaryValue( &x, sizeof(int), "bla" );
396 ar2( cereal::make_nvp( "1", 1 ),
397 cereal::make_nvp( "2", 2 ),
400 cereal::make_nvp( "4", 4 ),
401 cereal::make_nvp( "5", 5 ) );
402 ar2.saveBinaryValue( &x, sizeof(int), "bla" );
404 OOJson oo( 1, 2, true, 4.2 );
405 ar( CEREAL_NVP(oo) );
406 ar2( CEREAL_NVP(oo) );
409 ar & cereal::make_nvp("usingop&", oo ) & 6;
412 ar2 & cereal::make_nvp("usingop&", oo ) & 6;
415 long double ld = std::numeric_limits<long double>::max();
416 long long ll = std::numeric_limits<long long>::max();
417 unsigned long long ull = std::numeric_limits<unsigned long long>::max();
429 cereal::JSONInputArchive ar(oos);
430 int i1, i2, i3, i4, i5, x;
433 ar( cereal::make_nvp( "2", i2 ), i3 );
435 ar( cereal::make_nvp( "4", i4 ),
438 ar.loadBinaryValue( &x, sizeof(int) );
441 ar( cereal::make_nvp("oo", ii) );
442 ar( cereal::make_nvp( "2", i2 ) );
444 std::cout << i1 << " " << i2 << " " << i3 << " " << i4 << " " << i5 << std::endl;
445 std::cout << x << std::endl;
446 std::cout << ii.a << " " << ii.b << " " << ii.c.first << " " << ii.c.second << " ";
448 std::cout << z << " ";
449 std::cout << std::endl;
452 ar >> cereal::make_nvp("usingop&", oo );
453 std::cout << oo.a << " " << oo.b << " " << oo.c.first << " " << oo.c.second << " ";
455 std::cout << z << " ";
459 std::cout << aa << " " << a << " " << b << " " << c << std::endl;
463 unsigned long long ull;
469 std::cout << (ld == std::numeric_limits<long double>::max()) << std::endl;
470 std::cout << (ll == std::numeric_limits<long long>::max()) << std::endl;
471 std::cout << (ull == std::numeric_limits<unsigned long long>::max()) << std::endl;