-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSONArray.hpp
96 lines (71 loc) · 1.43 KB
/
JSONArray.hpp
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
#ifndef JSONArray_h
#define JSONArray_h
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
class JSONArray
{
public:
JSONArray()
{
m_sstream << "{\n";
}
JSONArray(const JSONArray& obj)
{
std::string m_string = obj.m_sstream.str();
m_sstream << m_string;
};
void put(std::string key, std::string value)
{
JSONKVPair m_JSONKVPair(key, value);
m_sstream << m_JSONKVPair.getOutput() << ",\n";
}
void putJSONArray(std::string key, std::vector<JSONArray> objectVector)
{
m_sstream << "\"" + key + "\":\n[\n";
for (auto iter = objectVector.begin(); iter != objectVector.end(); iter++)
{
m_sstream << (*iter).getOutput() << ",\n";
}
if (objectVector.size() > 0)
{
m_sstream.seekp(-2, std::ios_base::cur);
m_sstream << "\n],\n";
}
else {
m_sstream << "],\n";
}
}
std::string getOutput()
{
m_sstream.seekp(-2, std::ios_base::cur);
m_sstream << "\n}";
return m_sstream.str();
}
private:
class JSONKVPair
{
public:
JSONKVPair()
:m_Key("NULL"), m_Value("NULL")
{
}
JSONKVPair(std::string key, std::string value)
:m_Key(key), m_Value(value)
{
}
std::string getOutput()
{
m_stringstream << "\"" + m_Key + "\":\"" + m_Value + "\"";
return m_stringstream.str();
}
private:
std::string m_Key;
std::string m_Value;
std::stringstream m_stringstream;
};
JSONKVPair m_JsonKVPair;
std::stringstream m_sstream;
};
#endif // JSONArray_h