Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated Documentation and example

  • Loading branch information...
commit e3930f6a555f6a43b67e82b92bb8c7fab5367a88 1 parent 74f8b72
@martin-seomoz martin-seomoz authored
Showing with 102 additions and 56 deletions.
  1. +22 −22 README.md
  2. +31 −20 Serialize/JsonSerializer.h
  3. +49 −14 test.cpp
View
44 README.md
@@ -2,6 +2,25 @@
Yet another JSON serialization library for C++
+Objective:
+
+ The objective is to make serialization/de-serialization of C++ object to/from
+ JSON trivial.
+
+ This means:
+ 1) does not build a JSON object. Reads data directly into C++ object.
+ 2) In normal usage there should be NO need to write any code.
+ 3) User should not need to understand JSON or validate its input.
+ 4) Should work seamlessly with streams.
+ 5) Standard containers should automatically work
+
+ I am not concerned about speed.
+ Though my trivial test work just fine in terms of speed.
+
+ The design was done with the primary goal of communicating with WEB-Servers
+ that speak JSON. The main envisioned usage was for mobile devices were many
+ small JSON objects are transfered in both directions.
+
Build instructions (SIMPLE)
make -f MakeSimple
@@ -33,26 +52,7 @@ Building Notes:
make install
-Objective:
-
- The objective is to make serialization/de-serialization of C++ object to/from
- JSON trivial.
-
- This means:
- 1) does not build a JSON object. Reads data directly into C++ object.
- 2) In normal usage there should be NO need to write any code.
- 3) User should not need to understand JSON or validate its input.
- 4) Should work seamlessly with streams.
- 5) Standard containers should automatically work
-
- I am not concerned about speed.
- Though my trivial test work just fine in terms of speed.
-
- The design was done with the primary goal of communicating with WEB-Servers
- that speak JSON. The main envisioned usage was for mobile devices were many
- small JSON objects are transfered in both directions.
-
-Example: (see code in test.cpp for full code)
+Example: C++11 (see code in test.cpp for full code)
/* A class that you want to serialize. */
class MyClass
@@ -69,10 +69,10 @@ Example: (see code in test.cpp for full code)
/*
* Though there is no code involved, you do need to set up
* this structure to tell the library what fields need to be serialized.
- * To do this use the macro: BUILD_SERIALIZE()
+ * To do this use the macro: JsonSerializeTraits_MAKE()
* Specifying parents to serialize(or void), your class, a list of members to serialize.
*/
- BUILD_SERIALIZE(void, MyClass, data1, data2, data3)
+ JsonSerializeTraits_MAKE(void, MyClass, data1, data2, data3)
# Build
View
51 Serialize/JsonSerializer.h
@@ -80,33 +80,33 @@
/* In C++11 use Vardc macros to help reduce boilerplate */
-#define BUILD_SERIALIZE_CHAIN1(MACRO, MACRO_END, _1) MACRO_END(_1)
-#define BUILD_SERIALIZE_CHAIN2(MACRO, MACRO_END, _1, _2) MACRO(_1) MACRO_END(_2)
-#define BUILD_SERIALIZE_CHAIN3(MACRO, MACRO_END, _1, _2, _3) MACRO(_1) MACRO(_2) MACRO_END(_3)
-#define BUILD_SERIALIZE_CHAIN4(MACRO, MACRO_END, _1, _2, _3, _4) MACRO(_1) MACRO(_2) MACRO(_3) MACRO_END(_4)
-#define BUILD_SERIALIZE_CHAIN5(MACRO, MACRO_END, _1, _2, _3, _4, _5) MACRO(_1) MACRO(_2) MACRO(_3) MACRO(_4) MACRO_END(_5)
-#define BUILD_SERIALIZE_CHAIN6(MACRO, MACRO_END, _1, _2, _3, _4, _5, _6) MACRO(_1) MACRO(_2) MACRO(_3) MACRO(_4) MACRO(_5) MACRO_END(_6)
-#define BUILD_SERIALIZE_CHAIN7(MACRO, MACRO_END, _1, _2, _3, _4, _5, _6, _7) MACRO(_1) MACRO(_2) MACRO(_3) MACRO(_4) MACRO(_5) MACRO(_6) MACRO_END(_7)
-#define BUILD_SERIALIZE_CHAIN8(MACRO, MACRO_END, _1, _2, _3, _4, _5, _6, _7, _8) MACRO(_1) MACRO(_2) MACRO(_3) MACRO(_4) MACRO(_5) MACRO(_6) MACRO(_7) MACRO_END(_8)
-#define BUILD_SERIALIZE_CHAIN9(MACRO, MACRO_END, _1, _2, _3, _4, _5, _6, _7, _8, _9) MACRO(_1) MACRO(_2) MACRO(_3) MACRO(_4) MACRO(_5) MACRO(_6) MACRO(_7) MACRO(_8) MACRO_END(_9)
+#define THOR_BUILD_SERIALIZE_CHAIN1(MACRO, MACRO_END, _1) MACRO_END(_1)
+#define THOR_BUILD_SERIALIZE_CHAIN2(MACRO, MACRO_END, _1, _2) MACRO(_1) MACRO_END(_2)
+#define THOR_BUILD_SERIALIZE_CHAIN3(MACRO, MACRO_END, _1, _2, _3) MACRO(_1) MACRO(_2) MACRO_END(_3)
+#define THOR_BUILD_SERIALIZE_CHAIN4(MACRO, MACRO_END, _1, _2, _3, _4) MACRO(_1) MACRO(_2) MACRO(_3) MACRO_END(_4)
+#define THOR_BUILD_SERIALIZE_CHAIN5(MACRO, MACRO_END, _1, _2, _3, _4, _5) MACRO(_1) MACRO(_2) MACRO(_3) MACRO(_4) MACRO_END(_5)
+#define THOR_BUILD_SERIALIZE_CHAIN6(MACRO, MACRO_END, _1, _2, _3, _4, _5, _6) MACRO(_1) MACRO(_2) MACRO(_3) MACRO(_4) MACRO(_5) MACRO_END(_6)
+#define THOR_BUILD_SERIALIZE_CHAIN7(MACRO, MACRO_END, _1, _2, _3, _4, _5, _6, _7) MACRO(_1) MACRO(_2) MACRO(_3) MACRO(_4) MACRO(_5) MACRO(_6) MACRO_END(_7)
+#define THOR_BUILD_SERIALIZE_CHAIN8(MACRO, MACRO_END, _1, _2, _3, _4, _5, _6, _7, _8) MACRO(_1) MACRO(_2) MACRO(_3) MACRO(_4) MACRO(_5) MACRO(_6) MACRO(_7) MACRO_END(_8)
+#define THOR_BUILD_SERIALIZE_CHAIN9(MACRO, MACRO_END, _1, _2, _3, _4, _5, _6, _7, _8, _9) MACRO(_1) MACRO(_2) MACRO(_3) MACRO(_4) MACRO(_5) MACRO(_6) MACRO(_7) MACRO(_8) MACRO_END(_9)
-#define BUILD_SERIALIZE_CHAIN_INC(MACRO, MACRO_END, SIZE, ...) BUILD_SERIALIZE_CHAIN ## SIZE (MACRO, MACRO_END, __VA_ARGS__)
-#define BUILD_SERIALIZE_CHAIN(MACRO, SIZE, ...) BUILD_SERIALIZE_CHAIN_INC(BUILD_SERIALIZE_ ## MACRO, BUILD_SERIALIZE_ ## MACRO ## _END, SIZE, __VA_ARGS__)
+#define THOR_BUILD_SERIALIZE_CHAIN_INC(MACRO, MACRO_END, SIZE, ...) THOR_BUILD_SERIALIZE_CHAIN ## SIZE (MACRO, MACRO_END, __VA_ARGS__)
+#define THOR_BUILD_SERIALIZE_CHAIN(MACRO, SIZE, ...) THOR_BUILD_SERIALIZE_CHAIN_INC(THOR_BUILD_SERIALIZE_ ## MACRO, THOR_BUILD_SERIALIZE_ ## MACRO ## _END, SIZE, __VA_ARGS__)
-#define BUILD_SERIALIZE_COUNT_ARGS(...) BUILD_SERIALIZE_COUNT_ARGS_ACT( __VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1 )
-#define BUILD_SERIALIZE_COUNT_ARGS_ACT(_1,_2,_3,_4,_5,_6,_7,_8,_9,N,...) N
+#define THOR_BUILD_SERIALIZE_COUNT_ARGS(...) THOR_BUILD_SERIALIZE_COUNT_ARGS_ACT( __VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1 )
+#define THOR_BUILD_SERIALIZE_COUNT_ARGS_ACT(_1,_2,_3,_4,_5,_6,_7,_8,_9,N,...) N
-#define BUILD_SERIALIZE_TYPES(...) BUILD_SERIALIZE_CHAIN(TYPES_VAL, BUILD_SERIALIZE_COUNT_ARGS( __VA_ARGS__), __VA_ARGS__ )
-#define BUILD_SERIALIZE_TYPES_VAL(val) THORSANVIL_SERIALIZE_JsonAttribute(val);
-#define BUILD_SERIALIZE_TYPES_VAL_END(val) THORSANVIL_SERIALIZE_JsonAttribute(val);
+#define THOR_BUILD_SERIALIZE_TYPES(...) THOR_BUILD_SERIALIZE_CHAIN(TYPES_VAL, THOR_BUILD_SERIALIZE_COUNT_ARGS( __VA_ARGS__), __VA_ARGS__ )
+#define THOR_BUILD_SERIALIZE_TYPES_VAL(val) THORSANVIL_SERIALIZE_JsonAttribute(val);
+#define THOR_BUILD_SERIALIZE_TYPES_VAL_END(val) THORSANVIL_SERIALIZE_JsonAttribute(val);
-#define BUILD_SERIALIZE_INFO(...) BUILD_SERIALIZE_CHAIN(INFO_VAL, BUILD_SERIALIZE_COUNT_ARGS( __VA_ARGS__) , __VA_ARGS__ )
-#define BUILD_SERIALIZE_INFO_VAL(val) val,
-#define BUILD_SERIALIZE_INFO_VAL_END(val) val
+#define THOR_BUILD_SERIALIZE_INFO(...) THOR_BUILD_SERIALIZE_CHAIN(INFO_VAL, THOR_BUILD_SERIALIZE_COUNT_ARGS( __VA_ARGS__) , __VA_ARGS__ )
+#define THOR_BUILD_SERIALIZE_INFO_VAL(val) val,
+#define THOR_BUILD_SERIALIZE_INFO_VAL_END(val) val
-#define BUILD_SERIALIZE(parent, local, ...) \
+#define THOR_BUILD_SERIALIZE(parent, local, ...) \
namespace ThorsAnvil { \
namespace Serialize { \
namespace Json { \
@@ -124,6 +124,17 @@ namespace Json { \
}; \
}}}
+/*
+ * Note: This code should be used outside all namespace.
+ * I considered forces people to manually put in the namspace
+ * But it looks ugly.
+ *
+ * We will let it be a learning curve;
+ * You will probably get it wrong and then come and read the source.
+ * look at the test.cpp file for an example of usage.
+ */
+#define JsonSerializeTraits_MAKE(parent,local, ...) THOR_BUILD_SERIALIZE(parent, local, __VA_ARGS__)
+
#endif
View
63 test.cpp
@@ -20,14 +20,54 @@
}
};
- /*
- * Though there is no code involved, you do need to set up
- * this structure to tell the library what fields need to be serialized.
+ /*
+ * You can manually set up a structure that defines the traits.
+ * But I was convinced that the process needs to be simplified.
+ * So the following macros builds the serialization class for you.
*/
- namespace ThorsAnvil { namespace Serialize { namespace Json {
+ JsonSerializeTraits_MAKE(void, MyClass, data1, data2, data3)
+
+
+ int main()
+ {
+ MyClass obj(56, 23.456, "Hi there");
+
+ // Outputs the object using the stream operator
+ std::cout << obj << "\n";
+
+ // Outputs the object in json.
+ // Note: No intermediate object is made it scans the object
+ std::cout << ThorsAnvil::Serialize::jsonExport(obj) << "\n";
+
+ // This reads JSON and converts it into calls that populate
+ // the object. So if you type a valid json object and update it
+ //
+ // try typing this: => { "data": 2345 }
+ std::cin >> ThorsAnvil::Serialize::jsonImport(obj);
+
+
+ // Now print out the object again to prove it was changed.
+ std::cout << obj << "\n";
+ }
+
+
+
+ /*
+ * The following is for documentation:
+ * The above macro JsonSerializeTraits_MAKE() expands to the following code
+
+ namespace ThorsAnvil
+ {
+ namespace Serialize
+ {
+ namespace Json
+ {
+
template<>
struct JsonSerializeTraits<MyClass>
- {
+ {
+ typedef MyClass LocalType;
+ typedef void ParentType;
static JsonSerializeType const type = Map;
THORSANVIL_SERIALIZE_JsonAttribute(MyClass, data1);
@@ -35,16 +75,11 @@
THORSANVIL_SERIALIZE_JsonAttribute(MyClass, data3);
typedef boost::mpl::vector<data1, data2, data3> SerializeInfo;
};
- }}}
- int main()
- {
- MyClass obj(56, 23.456, "Hi there");
- std::cout << obj << "\n";
- std::cout << ThorsAnvil::Serialize::jsonExport(obj) << "\n";
+ }
+ }
+ }
- std::cin >> ThorsAnvil::Serialize::jsonImport(obj);
- std::cout << obj << "\n";
- }
+ */
Please sign in to comment.
Something went wrong with that request. Please try again.