3
3
#include < memory>
4
4
#include < sstream>
5
5
#include < json/json.h>
6
- // #include <pybind11/pybind11.h>
7
6
#include " reflect.hpp"
8
7
9
8
@@ -38,7 +37,7 @@ struct Student {
38
37
};
39
38
40
39
41
- std::string toString (Json::Value root) {
40
+ std::string jsonToStr (Json::Value root) {
42
41
Json::StreamWriterBuilder builder;
43
42
builder[" indentation" ] = " " ;
44
43
std::unique_ptr<Json::StreamWriter> writer (builder.newStreamWriter ());
@@ -47,48 +46,41 @@ std::string toString(Json::Value root) {
47
46
return os.str ();
48
47
}
49
48
50
- Json::Value fromString (std::string const &json) {
49
+ Json::Value strToJson (std::string const &json) {
51
50
Json::Value root;
52
51
Json::Reader reader;
53
52
reader.parse (json, root);
54
53
return root;
55
54
}
56
55
57
- template <class T > requires (!reflect_trait<T> ::has_member())
58
- Json::Value serialize (T const &object) {
56
+ template <class T , std:: enable_if_t <!reflect ::has_member<T>(), int > = 0 >
57
+ Json::Value objToJson (T const &object) {
59
58
return object;
60
59
}
61
60
62
- template <class T > requires (reflect_trait<T> ::has_member())
63
- Json::Value serialize (T const &object) {
61
+ template <class T , std:: enable_if_t <reflect ::has_member<T>(), int > = 0 >
62
+ Json::Value objToJson (T const &object) {
64
63
Json::Value root;
65
- foreach_member (object, [&](const char *key, auto &value) {
66
- root[key] = serialize (value);
64
+ reflect:: foreach_member (object, [&](const char *key, auto &value) {
65
+ root[key] = objToJson (value);
67
66
});
68
67
return root;
69
68
}
70
69
71
- template <class T > requires (!reflect_trait<T> ::has_member())
72
- T deserialize (Json::Value const &root) {
70
+ template <class T , std:: enable_if_t <!reflect ::has_member<T>(), int > = 0 >
71
+ T jsonToObj (Json::Value const &root) {
73
72
return root.as <T>();
74
73
}
75
74
76
- template <class T > requires (reflect_trait<T> ::has_member())
77
- T deserialize (Json::Value const &root) {
75
+ template <class T , std:: enable_if_t <reflect ::has_member<T>(), int > = 0 >
76
+ T jsonToObj (Json::Value const &root) {
78
77
T object;
79
- foreach_member (object, [&](const char *key, auto &value) {
80
- value = deserialize <std::decay_t <decltype (value)>>(root[key]);
78
+ reflect:: foreach_member (object, [&](const char *key, auto &value) {
79
+ value = jsonToObj <std::decay_t <decltype (value)>>(root[key]);
81
80
});
82
81
return object;
83
82
}
84
83
85
- // PYBIND11_MODULE(pyreflect, m) {
86
- // pybind11::class_<Address> b(m, "Address");
87
- // reflect_trait<Address>::foreach_member_ptr([&] (const char *name, auto member) {
88
- // b.def(name, member);
89
- // });
90
- // }
91
-
92
84
int main () {
93
85
Student stu = {
94
86
.name = " Peng" ,
@@ -99,13 +91,21 @@ int main() {
99
91
.city = " Shanghai" ,
100
92
}
101
93
};
102
- std::string bin = toString ( serialize (stu));
94
+ std::string bin = jsonToStr ( objToJson (stu));
103
95
std::cout << bin << ' \n ' ;
104
- auto stuDes = deserialize <Student>(fromString (bin));
96
+ auto stuDes = jsonToObj <Student>(strToJson (bin));
105
97
std::cout << stuDes.name << ' \n ' ;
106
98
std::cout << stuDes.age << ' \n ' ;
107
99
std::cout << stuDes.addr .country << ' \n ' ;
108
100
std::cout << stuDes.addr .province << ' \n ' ;
109
101
std::cout << stuDes.addr .city << ' \n ' ;
110
102
return 0 ;
111
103
}
104
+
105
+ // #include <pybind11/pybind11.h>
106
+ // PYBIND11_MODULE(pyreflect, m) {
107
+ // pybind11::class_<Address> b(m, "Address");
108
+ // foreach_member_ptr<Address>([&] (const char *name, auto member) {
109
+ // b.def(name, member);
110
+ // });
111
+ // }
0 commit comments