Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 51 lines (42 sloc) 1.717 kb
68852d4 Martin Added more comments and README
martin-seomoz authored
1 #include <iostream>
2 #include <string>
3 #include "serialize/json.h"
4
5 /* A class that you want to serialize. */
6 class MyClass
7 {
8 int data1;
9 float data2;
10 std::string data3;
11
12 // This is only required if the members are private.
13 friend struct ThorsAnvil::Serialize::Json::JsonSerializeTraits<MyClass>;
14 public:
15 MyClass(int a, float b, std::string const& c): data1(a), data2(b), data3(c) {}
16 friend std::ostream& operator<<(std::ostream& stream, MyClass const& value)
17 {
18 // Not need for serialization but we need to print out the results to show it worked.
19 return stream << value.data1 << " : " << value.data2 << " : " << value.data3;
20 }
21 };
22
23 /*
24 * Though there is no code involved, you do need to set up
25 * this structure to tell the library what fields need to be serialized.
26 */
27 namespace ThorsAnvil { namespace Serialize { namespace Json {
28 template<>
29 struct JsonSerializeTraits<MyClass>
30 {
50c50ff Martin Put back original test.cpp
martin-seomoz authored
31 static JsonSerializeType const type = Map;
68852d4 Martin Added more comments and README
martin-seomoz authored
32
33 THORSANVIL_SERIALIZE_JsonAttribute(MyClass, data1);
34 THORSANVIL_SERIALIZE_JsonAttribute(MyClass, data2);
35 THORSANVIL_SERIALIZE_JsonAttribute(MyClass, data3);
36 typedef boost::mpl::vector<data1, data2, data3> SerializeInfo;
37 };
38 }}}
39
40 int main()
41 {
42 MyClass obj(56, 23.456, "Hi there");
43 std::cout << obj << "\n";
44 std::cout << ThorsAnvil::Serialize::jsonExport(obj) << "\n";
45
46 std::cin >> ThorsAnvil::Serialize::jsonImport(obj);
47 std::cout << obj << "\n";
48 }
49
50
Something went wrong with that request. Please try again.