-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.cpp
More file actions
134 lines (111 loc) · 4.18 KB
/
driver.cpp
File metadata and controls
134 lines (111 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include "json.h"
#include <vector>
class iJsonSerializable{
public:
iJsonSerializable(){};
virtual void serialize(Json::Value& root)=0;
virtual void deSerialize(Json::Value& root)=0;
};
class cJsonSerializer{
public:
static bool serialize(iJsonSerializable * obj,std::string & output);
static bool deSerialize(iJsonSerializable * objt, std::string & input);
private:
cJsonSerializer(void ){};
};
class testClassA:public iJsonSerializable{
public:
testClassA(void){};
virtual ~testClassA(void){};
virtual void serialize(Json::Value& root);
virtual void deSerialize(Json::Value& root);
int _testInt;
double _testDouble;
std::string _testString;
bool _testBool;
std::vector<std::string> _testStringsV;
private:
};
class testSubClassA:public testClassA{
public:
testSubClassA(void){};
virtual ~testSubClassA(void){};
virtual void serialize(Json::Value& root);
virtual void deSerialize(Json::Value& root);
double _testSubADouble;
};
void testSubClassA::serialize(Json::Value& root){
testClassA::serialize(root);
root["testSubADouble"]=_testSubADouble;
}
void testSubClassA::deSerialize(Json::Value& root){
_testSubADouble=root.get("testSubADouble",0.0).asDouble();
this->testClassA::deSerialize(root);
}
void testClassA::serialize(Json::Value& root){
root["testInt"]=_testInt;
root["testDouble"]=_testDouble;
root["testString"]=_testString;
root["testBool"]=_testBool;
for(std::vector<std::string>::const_iterator it = _testStringsV.begin();it!=_testStringsV.end();it++){
root["testStringsV"].append((*it));
}
}
void testClassA::deSerialize(Json::Value& root){
_testInt=root.get("testInt",0).asInt();
_testDouble=root.get("testDouble",0.0).asDouble();
_testString=root.get("testString","").asString();
_testBool=root.get("testBool",false).asBool();
if(root.get("testStringsV","").isArray()){
int size=root.get("vector","").size();
for(int i=0;i<size;i++){
_testStringsV.push_back(root.get("testStringsV","")[i].asString());
}
}
}
bool cJsonSerializer::serialize(iJsonSerializable * obj,std::string & output){
if(obj==nullptr)
return false;
Json::Value jRoot;
obj->serialize(jRoot);
Json::StyledWriter writer;
output = writer.write(jRoot);
return true;
}
bool cJsonSerializer::deSerialize(iJsonSerializable * obj, std::string & input){
if(obj==nullptr)
return false;
Json::Value jRoot;
Json::Reader reader;
if (!reader.parse(input,jRoot))
return false;
obj->deSerialize(jRoot);
return true;
}
int main(){
testSubClassA mySubTest;
std::string inputDerived = "{ \"testInt\" : 42, \"testDouble\" : 3.14159, \"testString\" : \"foo\", \"testBool\" : true,\"testSubADouble\" : 55.55 }\n";
cJsonSerializer::deSerialize(&mySubTest,inputDerived);
std::cout << "raw input: " << inputDerived << std::endl;
std::cout << "Sub: loaded value for int: " << mySubTest._testInt << std::endl;
std::cout << "Sub: loaded value for double: " << mySubTest._testDouble << std::endl;
std::cout << "Sub: loaded value for String: " << mySubTest._testString << std::endl;
std::cout << "Sub: loaded value for bool: " << mySubTest._testBool<< std::endl;
std::cout << "Sub: loaded value for SubDouble: " << mySubTest._testSubADouble<< std::endl;
std::string outputDerived;
cJsonSerializer::serialize(&mySubTest,outputDerived);
std::cout << "Sub: serialized object: " << outputDerived << std::endl;
testClassA myTest;
std::string input = "{ \"testInt\" : 42, \"testDouble\" : 3.14159, \"testString\" : \"foo\", \"testBool\" : true }\n";
cJsonSerializer::deSerialize(&myTest,input);
std::cout << "raw input: " << input << std::endl;
std::cout << "loaded value for int: " << myTest._testInt << std::endl;
std::cout << "loaded value for double: " << myTest._testDouble << std::endl;
std::cout << "loaded value for String: " << myTest._testString << std::endl;
std::cout << "loaded value for bool: " << myTest._testBool<< std::endl;
std::string output;
cJsonSerializer::serialize(&myTest,output);
std::cout << "serialized object: " << output << std::endl;
return 0;
}