Adding cereal lib
[platform/upstream/iotivity.git] / extlibs / cereal / sandbox / sandbox_json.cpp
1 /*
2   Copyright (c) 2014, Randolph Voorhies, Shane Grant
3   All rights reserved.
4
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.
15
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.
26 */
27
28 #include <cereal/cereal.hpp>
29 #include <cereal/archives/binary.hpp>
30 #include <cereal/archives/json.hpp>
31
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>
40
41 #include <cereal/external/rapidjson/filestream.h>
42
43 #include <sstream>
44 #include <fstream>
45 #include <cassert>
46 #include <complex>
47 #include <iostream>
48 #include <iomanip>
49 #include <string>
50
51 // ###################################
52 struct Test1
53 {
54   int a;
55
56   private:
57     friend class cereal::access;
58     template<class Archive>
59     void serialize(Archive & ar)
60     {
61       ar(CEREAL_NVP(a));
62     }
63 };
64
65 // ###################################
66 class Test2
67 {
68   public:
69     Test2() {}
70     Test2( int x ) : a( x ) {}
71     int a;
72
73   private:
74     friend class cereal::access;
75
76     template<class Archive>
77       void save(Archive & ar) const
78       {
79         ar(a);
80       }
81
82     template<class Archive>
83       void load(Archive & ar)
84       {
85         ar(a);
86       }
87 };
88
89 // ###################################
90 struct Test3
91 {
92   int a;
93 };
94
95 template<class Archive>
96 void serialize(Archive & ar, Test3 & t)
97 {
98   ar(CEREAL_NVP(t.a));
99 }
100
101 namespace test4
102 {
103   // ###################################
104   struct Test4
105   {
106     int a;
107   };
108
109   template<class Archive>
110   void save(Archive & ar, Test4 const & t)
111   {
112     ar(CEREAL_NVP(t.a));
113   }
114
115   template<class Archive>
116   void load(Archive & ar, Test4 & t)
117   {
118     ar(CEREAL_NVP(t.a));
119   }
120 }
121
122 class Private
123 {
124   public:
125     Private() : a('z') {}
126
127   private:
128     char a;
129
130     friend class cereal::access;
131
132     template<class Archive>
133       void serialize(Archive & ar)
134       {
135         ar(a);
136       }
137 };
138
139 struct Everything
140 {
141   int x;
142   int y;
143   Test1 t1;
144   Test2 t2;
145   Test3 t3;
146   test4::Test4 t4;
147   std::string s;
148
149   template<class Archive>
150   void serialize(Archive & ar)
151   {
152     ar(CEREAL_NVP(x));
153     ar(CEREAL_NVP(y));
154     ar(CEREAL_NVP(t1));
155     ar(CEREAL_NVP(t2));
156     ar(CEREAL_NVP(t3));
157     ar(CEREAL_NVP(t4));
158     ar(CEREAL_NVP(s));
159   }
160
161   bool operator==(Everything const & o)
162   {
163     return
164       x == o.x &&
165       y == o.y &&
166       t1.a == o.t1.a &&
167       t2.a == o.t2.a &&
168       t3.a == o.t3.a &&
169       t4.a == o.t4.a &&
170       s == o.s;
171   }
172 };
173
174
175 struct SubFixture
176 {
177   SubFixture() : a( 3 ),
178     b( 9999 ),
179     c( 100.1f ),
180     d( 2000.9 ),
181     s( "hello, world!" )
182   {}
183
184   int a;
185   uint64_t b;
186   float c;
187   double d;
188   std::string s;
189
190     template<class Archive>
191       void serialize(Archive & ar)
192       {
193         ar( CEREAL_NVP(a),
194             b,
195             c,
196             CEREAL_NVP(d),
197             CEREAL_NVP(s) );
198       }
199     void change()
200     {
201       a = 4;
202       b = 4;
203       c = 4;
204       d = 4;
205       s = "4";
206     }
207 };
208
209 struct Fixture
210 {
211   SubFixture f1, f2, f3;
212   int array[4];
213
214   Fixture()
215   {
216     array[0] = 1;
217     array[1] = 2;
218     array[2] = 3;
219     array[3] = 4;
220   }
221
222   template<class Archive>
223   void save(Archive & ar) const
224   {
225     ar( f1,
226         CEREAL_NVP(f2),
227         f3 );
228     ar.saveBinaryValue( array, sizeof(int)*4, "cool array man" );
229   }
230
231   template<class Archive>
232   void load(Archive & ar)
233   {
234     ar( f1,
235         CEREAL_NVP(f2),
236         f3 );
237     ar.loadBinaryValue( array, sizeof(int)*4 );
238   }
239
240     void change()
241     {
242       f1.change();
243       f2.change();
244       f3.change();
245     }
246 };
247
248 struct AAA
249 {
250   AAA() : one( 1 ), two( 2 ), three( { {1, 2, 3}, { 4, 5, 6 }, {} } ) {}
251   int one, two;
252
253   std::vector<std::vector<int>> three;
254
255   template<class Archive>
256     void serialize(Archive & ar)
257     {
258       ar( CEREAL_NVP(one), CEREAL_NVP(two) );
259       //ar( CEREAL_NVP(three) );
260     }
261 };
262
263 class Stuff
264 {
265   public:
266     Stuff() {}
267
268     void fillData()
269     {
270       std::vector<std::complex<float>> t1{ {0, -1.0f},
271                           { 0, -2.9932f },
272                           { 0, -3.5f } };
273       std::vector<std::complex<float>> t2{ {1.0f, 0},
274                      { 2.2f, 0 },
275                      { 3.3f, 0 } };
276       data["imaginary"] = t1;
277       data["real"] = t2;
278     }
279
280   private:
281     std::map<std::string, std::vector<std::complex<float>>> data;
282
283     friend class cereal::access;
284
285     template <class Archive>
286     void serialize( Archive & ar )
287     {
288       ar( CEREAL_NVP(data) );
289     }
290 };
291
292 struct OOJson
293 {
294   OOJson() = default;
295   OOJson( int aa, int bb, bool cc, double dd ) :
296     a( aa ), b( bb ), c{ cc, dd }
297   {
298     d[0] = 0; d[1] = 1; d[2] = 2;
299   }
300
301   int a;
302   int b;
303   std::pair<bool, double> c;
304   float d[3];
305
306   template <class Archive>
307   void serialize( Archive & ar )
308   {
309     ar( CEREAL_NVP(c) );
310     ar( CEREAL_NVP(a) );
311     ar( b );
312     ar( CEREAL_NVP(d) );
313   }
314 };
315
316 enum Bla
317 {
318   x,
319   y
320 };
321
322 template <class Archive>
323 void save( Archive & ar, Bla const & b )
324 {
325   ar( (const int &)b );
326 }
327
328 template <class Archive>
329 void load( Archive & ar, Bla & b )
330 {
331   ar( (int&)b );
332 }
333
334 CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES( Bla, cereal::specialization::non_member_load_save )
335
336 //namespace cereal
337 //{
338 //  //template <class Archive> struct specialize<Archive, Bla, cereal::specialization::non_member_load_save> {};
339 //}
340
341 // ######################################################################
342 int main()
343 {
344   std::cout << std::boolalpha << std::endl;
345
346   {
347     std::ofstream os("file.json");
348     cereal::JSONOutputArchive oar( os );
349
350     //auto f = std::make_shared<Fixture>();
351     //auto f2 = f;
352     //oar( f );
353     //oar( f2 );
354     Stuff s; s.fillData();
355     oar( cereal::make_nvp("best data ever", s) );
356   }
357
358   {
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;
362   }
363
364   // playground
365   {
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),
370         arr );
371     auto f = std::make_shared<Fixture>();
372     auto f2 = f;
373     archive( f );
374     archive( f2 );
375
376     archive( Bla::x );
377   }
378
379   // test out of order
380   std::stringstream oos;
381   {
382     cereal::JSONOutputArchive ar(oos);
383     cereal::JSONOutputArchive ar2(std::cout,
384         cereal::JSONOutputArchive::Options(2, cereal::JSONOutputArchive::Options::IndentChar::space, 2) );
385
386     ar( cereal::make_nvp( "1", 1 ),
387         cereal::make_nvp( "2", 2 ),
388         3,
389         0, // unused
390         cereal::make_nvp( "4", 4 ),
391         cereal::make_nvp( "5", 5 ) );
392
393     int x = 33;
394     ar.saveBinaryValue( &x, sizeof(int), "bla" );
395
396     ar2( cereal::make_nvp( "1", 1 ),
397          cereal::make_nvp( "2", 2 ),
398          3,
399          0, // unused
400          cereal::make_nvp( "4", 4 ),
401          cereal::make_nvp( "5", 5 ) );
402     ar2.saveBinaryValue( &x, sizeof(int), "bla" );
403
404     OOJson oo( 1, 2, true, 4.2 );
405     ar( CEREAL_NVP(oo) );
406     ar2( CEREAL_NVP(oo) );
407
408     // boost stuff
409     ar & cereal::make_nvp("usingop&", oo ) & 6;
410     ar << 5 << 4 << 3;
411
412     ar2 & cereal::make_nvp("usingop&", oo ) & 6;
413     ar2 << 5 << 4 << 3;
414
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();
418
419     ar( CEREAL_NVP(ld),
420         CEREAL_NVP(ll),
421         CEREAL_NVP(ull) );
422
423     ar2( CEREAL_NVP(ld),
424          CEREAL_NVP(ll),
425          CEREAL_NVP(ull) );
426   }
427
428   {
429     cereal::JSONInputArchive ar(oos);
430     int i1, i2, i3, i4, i5, x;
431
432     ar( i1 );
433     ar( cereal::make_nvp( "2", i2 ), i3 );
434
435     ar( cereal::make_nvp( "4", i4 ),
436         i5 );
437
438     ar.loadBinaryValue( &x, sizeof(int) );
439
440     OOJson ii;
441     ar( cereal::make_nvp("oo", ii) );
442     ar( cereal::make_nvp( "2", i2 ) );
443
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 << " ";
447     for( auto z : ii.d )
448       std::cout << z << " ";
449     std::cout << std::endl;
450
451     OOJson oo;
452     ar >> cereal::make_nvp("usingop&", oo );
453     std::cout << oo.a << " " << oo.b << " " << oo.c.first << " " << oo.c.second << " ";
454     for( auto z : oo.d )
455       std::cout << z << " ";
456
457     int aa, a, b, c;
458     ar & aa & a & b & c;
459     std::cout << aa << " " << a << " " << b << " " << c << std::endl;
460
461     long double ld;
462     long long ll;
463     unsigned long long ull;
464
465     ar( CEREAL_NVP(ld),
466         CEREAL_NVP(ll),
467         CEREAL_NVP(ull) );
468
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;
472   }
473
474   return 0;
475 }