forked from SharifAIChallenge/AIC16-Client-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Message.cpp
85 lines (71 loc) · 1.65 KB
/
Message.cpp
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
/*
* Json.cpp
*
* Created on: Feb 4, 2015
* Author: rmin
*/
#include "Message.h"
#include <chrono>
Message::Message() {
// TODO Auto-generated constructor stub
}
Message::~Message() {
// TODO Auto-generated destructor stub
}
void Message::setJson(std::string &str)
{
reader.parse(str, root, false);
}
std::string Message::getJson() {
std::string output = writer.write(root);
output.push_back('\0');
return output;
}
void Message::setJson(Json::Value value) {
this->root = value;
}
std::string Message::getName() {
return root["name"].asString();
}
void Message::addNode(std::string name, std::string value) {
root[name] = value;
}
void Message::addNode(std::string name, int value) {
root[name] = value;
}
void Message::addArray(std::string name, std::vector<int> value) {
if (value.size() == 0) {
root[name] = Json::Value(Json::arrayValue);
} else {
for (int i = 0; i < (int) value.size(); i++) {
Json::Value tmp(value[i]);
root[name].append(tmp);
}
}
}
void Message::addArray(std::string name, std::vector<std::string> value) {
if (value.size() == 0) {
root[name] = Json::Value(Json::arrayValue);
} else {
for (int i = 0; i < (int) value.size(); i++) {
Json::Value tmp(value[i]);
root[name].append(tmp);
}
}
}
void Message::addArray(std::string name, std::vector<Message> value) {
if (value.size() == 0) {
root[name] = Json::Value(Json::arrayValue);
} else {
for (int i = 0; i < (int) value.size(); i++) {
Json::Value tmp(value[i].getRoot());
root[name].append(tmp);
}
}
}
Json::Value& Message::getArray(std::string str, bool isTurn) {
return root[str];
}
Json::Value& Message::getRoot() {
return root;
}