JsonValue.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
17* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
18* GNU General Public License
19*/
20
21#include "JsonValue.h"
22
23#include <boost/regex.hpp>
24
27
28namespace armarx
29{
30 JsonValue::JsonValue(const std::string& value) : type(eString), value(value)
31 {
32 }
33
34 JsonValue::JsonValue(int value) : type(eNumber), value(ToString(value))
35 {
36 }
37
38 JsonValue::JsonValue(long value) : type(eNumber), value(ToString(value))
39 {
40 }
41
42 JsonValue::JsonValue(float value) : type(eNumber), value(ToString(value))
43 {
44 }
45
46 JsonValue::JsonValue(double value) : type(eNumber), value(ToString(value))
47 {
48 }
49
52 {
53 JsonValuePtr value(new JsonValue(eNull, "null"));
54 return value;
55 }
56
59 {
60 JsonValuePtr value(new JsonValue(eBool, "true"));
61 return value;
62 }
63
66 {
67 JsonValuePtr value(new JsonValue(eBool, "false"));
68 return value;
69 }
70
72 JsonValue::Create(const std::string& value)
73 {
74 JsonValuePtr jsonValue(new JsonValue(value));
75 return jsonValue;
76 }
77
80 {
81 JsonValuePtr jsonValue(new JsonValue(value));
82 return jsonValue;
83 }
84
87 {
88 JsonValuePtr jsonValue(new JsonValue(value));
89 return jsonValue;
90 }
91
93 JsonValue::Create(float value)
94 {
95 JsonValuePtr jsonValue(new JsonValue(value));
96 return jsonValue;
97 }
98
100 JsonValue::Create(double value)
101 {
102 JsonValuePtr jsonValue(new JsonValue(value));
103 return jsonValue;
104 }
105
107 JsonValue::CreateRaw(JsonValue::Type type, const std::string& value)
108 {
109 if (!CheckValue(type, value))
110 {
111 throw LocalException("Invalid value: ") << value;
112 }
113 JsonValuePtr result(new JsonValue(type, value));
114 return result;
115 }
116
117 bool
118 JsonValue::CheckValue(JsonValue::Type type, const std::string& value)
119 {
120 switch (type)
121 {
122 case eNull:
123 return CheckNull(value);
124
125 case eBool:
126 return CheckBool(value);
127
128 case eNumber:
129 return CheckNumber(value);
130
131 case eString:
132 return true;
133 }
134 throw LocalException("Invalid type.");
135 }
136
137 bool
138 JsonValue::CheckNumber(const std::string& value)
139 {
140 boost::regex re("^-?\\d+(\\.\\d+)?([eE][+-]?\\d+)?$");
141 boost::match_results<std::string::const_iterator> results;
142 return boost::regex_match(value, results, re, boost::regex_constants::match_default) != 0;
143 }
144
145 bool
146 JsonValue::CheckInt(const std::string& value)
147 {
148 boost::regex re("^-?\\d+$");
149 boost::match_results<std::string::const_iterator> results;
150 return boost::regex_match(value, results, re, boost::regex_constants::match_default) != 0;
151 }
152
153 bool
154 JsonValue::CheckBool(const std::string& value)
155 {
156 return value == "true" || value == "false";
157 }
158
159 bool
160 JsonValue::CheckNull(const std::string& value)
161 {
162 return value == "null";
163 }
164
165 void
167 {
168 switch (type)
169 {
170 case eNull:
171 case eBool:
172 case eNumber:
173 writer->writeRawValue(value);
174 break;
175
176 case eString:
177 writer->writeRawValue(JsonWriter::EscapeQuote(value));
178 break;
179 }
180 }
181
184 {
185 JsonValuePtr ptr(new JsonValue(*this));
186 return ptr;
187 }
188
189 std::string
191 {
192 std::ostringstream oss;
193 oss.imbue(std::locale::classic());
194 oss << value;
195 return oss.str();
196 }
197
198 std::string
200 {
201 std::ostringstream oss;
202 oss.imbue(std::locale::classic());
203 oss << value;
204 return oss.str();
205 }
206
207 std::string
209 {
210 std::ostringstream oss;
211 oss.imbue(std::locale::classic());
212 oss << value;
213 return oss.str();
214 }
215
216 std::string
218 {
219 std::ostringstream oss;
220 oss.imbue(std::locale::classic());
221 oss << value;
222 return oss.str();
223 }
224
225 std::string
227 {
228 if (type != eString)
229 {
230 throw LocalException("Invalid type");
231 }
232 return value;
233 }
234
235 float
237 {
238 if (type != eNumber)
239 {
240 throw LocalException("Invalid type");
241 }
242 return armarx::toFloat(value);
243 }
244
245 int
247 {
248 if (type != eNumber || !CheckInt(value))
249 {
250 throw LocalException("Invalid type");
251 }
252 return armarx::toFloat(value);
253 }
254
255 bool
257 {
258 if (type != eBool)
259 {
260 throw LocalException("Invalid type");
261 }
262 return value == "true";
263 }
264
265 std::string
267 {
268 return value;
269 }
270
273 {
274 JsonValuePtr ptr(new JsonValue(*this));
275 return ptr;
276 }
277
280 {
281 return type;
282 }
283
284 JsonValue::JsonValue(JsonValue::Type type, const std::string& value) : type(type), value(value)
285 {
286 }
287} // namespace armarx
static JsonValuePtr Null()
Definition JsonValue.cpp:51
static JsonValuePtr Create(const std::string &value)
Definition JsonValue.cpp:72
std::string asString() const
static JsonValuePtr CreateRaw(Type type, const std::string &value)
static JsonValuePtr True()
Definition JsonValue.cpp:58
static bool CheckInt(const std::string &value)
static JsonValuePtr False()
Definition JsonValue.cpp:65
static bool CheckNumber(const std::string &value)
void writeJson(const JsonWriterPtr &writer) override
static bool CheckNull(const std::string &value)
std::string rawValue()
float asFloat() const
static std::string ToString(int value)
JsonValuePtr toSharedPtr() const
bool asBool() const
int asInt() const
JsonDataPtr clone() override
static bool CheckValue(Type type, const std::string &value)
static bool CheckBool(const std::string &value)
JsonValue(const std::string &value)
Definition JsonValue.cpp:30
static std::string EscapeQuote(const std::string &str)
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< JsonValue > JsonValuePtr
Definition JsonValue.h:30
std::shared_ptr< JsonData > JsonDataPtr
Definition JsonData.h:31
std::shared_ptr< JsonWriter > JsonWriterPtr
Definition JsonWriter.h:30
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.