Mercurial > code
comparison C++/modules/Json/Json.cpp @ 475:b681299e6987
Json: removal of json::Object, json::Array, json::Document
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 09 Nov 2015 16:52:38 +0100 |
parents | 902b034df6e3 |
children | c7825b8c6145 |
comparison
equal
deleted
inserted
replaced
474:fb3158aca358 | 475:b681299e6987 |
---|---|
43 } | 43 } |
44 if (json_is_boolean(v)) { | 44 if (json_is_boolean(v)) { |
45 return Value{json_boolean_value(v)}; | 45 return Value{json_boolean_value(v)}; |
46 } | 46 } |
47 if (json_is_object(v)) { | 47 if (json_is_object(v)) { |
48 Object object; | 48 Value object{Type::Object}; |
49 | 49 |
50 readObject(object, v); | 50 readObject(object, v); |
51 | 51 |
52 return object; | 52 return object; |
53 } | 53 } |
54 if (json_is_array(v)) { | 54 if (json_is_array(v)) { |
55 Array array; | 55 Value array{Type::Array}; |
56 | 56 |
57 readArray(array, v); | 57 readArray(array, v); |
58 | 58 |
59 return array; | 59 return array; |
60 } | 60 } |
66 { | 66 { |
67 const char *key; | 67 const char *key; |
68 json_t *value; | 68 json_t *value; |
69 | 69 |
70 json_object_foreach(object, key, value) { | 70 json_object_foreach(object, key, value) { |
71 static_cast<Object &>(parent).insert(key, readValue(value)); | 71 parent.insert(key, readValue(value)); |
72 } | 72 } |
73 } | 73 } |
74 | 74 |
75 void readArray(Value &parent, json_t *array) | 75 void readArray(Value &parent, json_t *array) |
76 { | 76 { |
77 size_t index; | 77 size_t index; |
78 json_t *value; | 78 json_t *value; |
79 | 79 |
80 json_array_foreach(array, index, value) { | 80 json_array_foreach(array, index, value) { |
81 static_cast<Array &>(parent).append(readValue(value)); | 81 parent.append(readValue(value)); |
82 } | 82 } |
83 } | 83 } |
84 | 84 |
85 template <typename Func, typename... Args> | 85 template <typename Func, typename... Args> |
86 Value convert(Func fn, Args&&... args) | 86 Value convert(Func fn, Args&&... args) |
93 } | 93 } |
94 | 94 |
95 Value value; | 95 Value value; |
96 | 96 |
97 if (json_is_object(json)) { | 97 if (json_is_object(json)) { |
98 value = Object{}; | 98 value = Value{Type::Object}; |
99 readObject(value, json); | 99 readObject(value, json); |
100 } else { | 100 } else { |
101 value = Array{}; | 101 value = Value{Type::Array}; |
102 readArray(value, json); | 102 readArray(value, json); |
103 } | 103 } |
104 | 104 |
105 json_decref(json); | 105 json_decref(json); |
106 | 106 |
107 return value; | 107 return value; |
108 } | 108 } |
109 | 109 |
110 } // !namespace | 110 } // !namespace |
111 | |
112 void Value::copy(const Value &other) | |
113 { | |
114 switch (other.m_type) { | |
115 case Type::String: | |
116 new (&m_string) std::string(); | |
117 m_string = std::move(other.m_string); | |
118 break; | |
119 case Type::Real: | |
120 m_number = other.m_number; | |
121 break; | |
122 case Type::Int: | |
123 m_integer = other.m_integer; | |
124 break; | |
125 case Type::Boolean: | |
126 m_boolean = other.m_boolean; | |
127 break; | |
128 case Type::Object: | |
129 new (&m_object) std::map<std::string, Value>(); | |
130 m_object = std::move(other.m_object); | |
131 break; | |
132 case Type::Array: | |
133 new (&m_array) std::deque<Value>(); | |
134 m_array = std::move(other.m_array); | |
135 break; | |
136 default: | |
137 break; | |
138 } | |
139 | |
140 m_type = other.m_type; | |
141 } | |
142 | |
143 void Value::move(Value &&other) | |
144 { | |
145 switch (other.m_type) { | |
146 case Type::String: | |
147 new (&m_string) std::string(std::move(other.m_string)); | |
148 break; | |
149 case Type::Real: | |
150 m_number = other.m_number; | |
151 break; | |
152 case Type::Int: | |
153 m_integer = other.m_integer; | |
154 break; | |
155 case Type::Boolean: | |
156 m_boolean = other.m_boolean; | |
157 break; | |
158 case Type::Object: | |
159 new (&m_object) std::map<std::string, Value>(std::move(other.m_object)); | |
160 break; | |
161 case Type::Array: | |
162 new (&m_array) std::deque<Value>(std::move(other.m_array)); | |
163 break; | |
164 default: | |
165 break; | |
166 } | |
167 | |
168 m_type = other.m_type; | |
169 } | |
170 | |
171 Value::Value(Type type) | |
172 : m_type{type} | |
173 { | |
174 switch (type) { | |
175 case Type::String: | |
176 new (&m_string) std::string(); | |
177 break; | |
178 case Type::Array: | |
179 new (&m_array) std::deque<Value>(); | |
180 break; | |
181 case Type::Object: | |
182 new (&m_object) std::map<std::string, Value>(); | |
183 break; | |
184 case Type::Real: | |
185 m_number = 0; | |
186 break; | |
187 case Type::Int: | |
188 m_integer = 0; | |
189 break; | |
190 case Type::Boolean: | |
191 m_boolean = false; | |
192 break; | |
193 default: | |
194 break; | |
195 } | |
196 } | |
197 | |
198 Value::~Value() | |
199 { | |
200 switch (m_type) { | |
201 case Type::String: | |
202 m_string.~basic_string(); | |
203 break; | |
204 case Type::Object: | |
205 m_object.~map<std::string, Value>(); | |
206 break; | |
207 case Type::Array: | |
208 m_array.~deque<Value>(); | |
209 break; | |
210 default: | |
211 break; | |
212 } | |
213 } | |
111 | 214 |
112 bool Value::toBool() const noexcept | 215 bool Value::toBool() const noexcept |
113 { | 216 { |
114 if (m_type != Type::Boolean) { | 217 if (m_type != Type::Boolean) { |
115 return false; | 218 return false; |
143 } | 246 } |
144 | 247 |
145 return m_string; | 248 return m_string; |
146 } | 249 } |
147 | 250 |
148 Object Value::toObject() const noexcept | 251 Value::Value(const Buffer &buffer) |
149 { | 252 { |
150 if (m_type != Type::Object) { | 253 *this = convert(json_loads, buffer.text.c_str(), 0); |
151 return Object{}; | 254 } |
152 } | 255 |
153 | 256 Value::Value(const File &file) |
154 return Object(*this); | 257 { |
155 } | 258 *this = convert(json_load_file, file.path.c_str(), 0); |
156 | |
157 Array Value::toArray() const noexcept | |
158 { | |
159 if (m_type != Type::Array) { | |
160 return Array{}; | |
161 } | |
162 | |
163 return Array(*this); | |
164 } | |
165 | |
166 Document::Document(Buffer buffer) | |
167 { | |
168 m_value = convert(json_loads, buffer.text.c_str(), 0); | |
169 } | |
170 | |
171 Document::Document(File file) | |
172 { | |
173 m_value = convert(json_load_file, file.path.c_str(), 0); | |
174 } | 259 } |
175 | 260 |
176 std::string escape(const std::string &value) | 261 std::string escape(const std::string &value) |
177 { | 262 { |
178 std::string result; | 263 std::string result; |