forked from lwclwc/JsonProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonType.cpp
More file actions
130 lines (105 loc) · 2.31 KB
/
jsonType.cpp
File metadata and controls
130 lines (105 loc) · 2.31 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
#include "jsonType.hpp"
namespace jsonProject
{
JsonInt::JsonInt(int i) : m_data(i)
{}
Json::Type JsonInt::getType() const
{
return Type::INT;
}
string JsonInt::toJson() const
{
return to_string(static_cast<_LONGLONG>(m_data));
}
JsonDouble::JsonDouble(double i) : m_data(i)
{}
Json::Type JsonDouble::getType() const
{
return Type::DOUBLE;
}
string JsonDouble::toJson() const
{
return to_string(static_cast<_LONGLONG>(m_data));
}
JsonBool::JsonBool(bool i) : m_data(i)
{}
Json::Type JsonBool::getType() const
{
return Type::BOOL;
}
string JsonBool::toJson() const
{
return m_data ? "true" : "false";
}
JsonString::JsonString(const string &i) : m_data(i)
{}
Json::Type JsonString::getType() const
{
return Type::STRING;
}
string JsonString::toJson() const
{
return "\"" + m_data + "\"";
}
JsonArray::JsonArray() : m_data()
{}
Json::Type JsonArray::getType() const
{
return Type::ARRAY;
}
string JsonArray::toJson() const
{
if (m_data.empty())
{
return "[]";
}
string json = "[";
vector<shared_ptr<Json> >::const_iterator it = m_data.begin();
for (; it != m_data.end(); ++it)
{
json += (*it)->toJson();
if (*it != m_data.back())
{
json += ",";
}
}
return json + "]";
}
JsonArray& JsonArray::push(shared_ptr<Json> e)
{
m_data.push_back(e);
return *this;
}
JsonObject::JsonObject() : m_data()
{}
Json::Type JsonObject::getType() const
{
return OBJECT;
}
string JsonObject::toJson() const
{
string json = "{";
unordered_map<string, shared_ptr<Json> >::const_iterator it = m_data.begin();
for (; it != m_data.end();)
{
const pair<string, shared_ptr<Json> >& e = *it;
json += ("\"" + e.first + "\"");
json += ":";
json += e.second->toJson();
if (++it != m_data.end())
{
json += ",";
}
}
return json + "}";
}
JsonObject& JsonObject::push(const string& name, shared_ptr<Json> v)
{
m_data[name] = v;
return *this;
}
shared_ptr<Json> JsonObject::operator[](string key)
{
return m_data[key];
}
}