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 
25 #include <boost/regex.hpp>
26 
27 namespace armarx
28 {
29  JsonValue::JsonValue(const std::string& value)
30  : type(eString), value(value)
31  { }
32 
34  : type(eNumber), value(ToString(value))
35  { }
36 
38  : type(eNumber), value(ToString(value))
39  { }
40 
42  : type(eNumber), value(ToString(value))
43  { }
44 
46  : type(eNumber), value(ToString(value))
47  { }
48 
50  {
51  JsonValuePtr value(new JsonValue(eNull, "null"));
52  return value;
53  }
55  {
56  JsonValuePtr value(new JsonValue(eBool, "true"));
57  return value;
58  }
60  {
61  JsonValuePtr value(new JsonValue(eBool, "false"));
62  return value;
63  }
64 
65  JsonValuePtr JsonValue::Create(const std::string& value)
66  {
67  JsonValuePtr jsonValue(new JsonValue(value));
68  return jsonValue;
69  }
70 
72  {
73  JsonValuePtr jsonValue(new JsonValue(value));
74  return jsonValue;
75  }
76 
78  {
79  JsonValuePtr jsonValue(new JsonValue(value));
80  return jsonValue;
81  }
82 
84  {
85  JsonValuePtr jsonValue(new JsonValue(value));
86  return jsonValue;
87  }
88 
90  {
91  JsonValuePtr jsonValue(new JsonValue(value));
92  return jsonValue;
93  }
94 
96  {
97  if (!CheckValue(type, value))
98  {
99  throw LocalException("Invalid value: ") << value;
100  }
101  JsonValuePtr result(new JsonValue(type, value));
102  return result;
103  }
104 
105  bool JsonValue::CheckValue(JsonValue::Type type, const std::string& value)
106  {
107  switch (type)
108  {
109  case eNull:
110  return CheckNull(value);
111 
112  case eBool:
113  return CheckBool(value);
114 
115  case eNumber:
116  return CheckNumber(value);
117 
118  case eString:
119  return true;
120  }
121  throw LocalException("Invalid type.");
122  }
123 
124  bool JsonValue::CheckNumber(const std::string& value)
125  {
126  boost::regex re("^-?\\d+(\\.\\d+)?([eE][+-]?\\d+)?$");
127  boost::match_results<std::string::const_iterator> results;
128  return boost::regex_match(value, results, re, boost::regex_constants::match_default) != 0;
129  }
130 
131  bool JsonValue::CheckInt(const std::string& value)
132  {
133  boost::regex re("^-?\\d+$");
134  boost::match_results<std::string::const_iterator> results;
135  return boost::regex_match(value, results, re, boost::regex_constants::match_default) != 0;
136  }
137 
138  bool JsonValue::CheckBool(const std::string& value)
139  {
140  return value == "true" || value == "false";
141  }
142 
143  bool JsonValue::CheckNull(const std::string& value)
144  {
145  return value == "null";
146  }
147 
149  {
150  switch (type)
151  {
152  case eNull:
153  case eBool:
154  case eNumber:
155  writer->writeRawValue(value);
156  break;
157 
158  case eString:
159  writer->writeRawValue(JsonWriter::EscapeQuote(value));
160  break;
161  }
162  }
163 
165  {
166  JsonValuePtr ptr(new JsonValue(*this));
167  return ptr;
168  }
169 
170  std::string JsonValue::ToString(int value)
171  {
172  std::ostringstream oss;
173  oss.imbue(std::locale::classic());
174  oss << value;
175  return oss.str();
176  }
177 
178  std::string JsonValue::ToString(long value)
179  {
180  std::ostringstream oss;
181  oss.imbue(std::locale::classic());
182  oss << value;
183  return oss.str();
184  }
185 
186  std::string JsonValue::ToString(float value)
187  {
188  std::ostringstream oss;
189  oss.imbue(std::locale::classic());
190  oss << value;
191  return oss.str();
192  }
193 
194  std::string JsonValue::ToString(double value)
195  {
196  std::ostringstream oss;
197  oss.imbue(std::locale::classic());
198  oss << value;
199  return oss.str();
200  }
201 
202  std::string JsonValue::asString() const
203  {
204  if (type != eString)
205  {
206  throw LocalException("Invalid type");
207  }
208  return value;
209  }
210 
211  float JsonValue::asFloat() const
212  {
213  if (type != eNumber)
214  {
215  throw LocalException("Invalid type");
216  }
217  return armarx::toFloat(value);
218  }
219 
220  int JsonValue::asInt() const
221  {
222  if (type != eNumber || !CheckInt(value))
223  {
224  throw LocalException("Invalid type");
225  }
226  return armarx::toFloat(value);
227  }
228 
229  bool JsonValue::asBool() const
230  {
231  if (type != eBool)
232  {
233  throw LocalException("Invalid type");
234  }
235  return value == "true";
236  }
237 
238  std::string JsonValue::rawValue()
239  {
240  return value;
241  }
242 
244  {
245  JsonValuePtr ptr(new JsonValue(*this));
246  return ptr;
247  }
248 
250  {
251  return type;
252  }
253 
254  JsonValue::JsonValue(JsonValue::Type type, const std::string& value)
255  : type(type), value(value)
256  { }
257 }
armarx::JsonValue::eNumber
@ eNumber
Definition: JsonValue.h:38
armarx::JsonValuePtr
std::shared_ptr< JsonValue > JsonValuePtr
Definition: JsonValue.h:30
armarx::JsonValue::rawValue
std::string rawValue()
Definition: JsonValue.cpp:238
armarx::JsonValue::Type
Type
Definition: JsonValue.h:38
armarx::JsonValue::True
static JsonValuePtr True()
Definition: JsonValue.cpp:54
armarx::toFloat
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.
Definition: StringHelpers.cpp:97
armarx::JsonValue::False
static JsonValuePtr False()
Definition: JsonValue.cpp:59
armarx::JsonValue::CheckInt
static bool CheckInt(const std::string &value)
Definition: JsonValue.cpp:131
armarx::JsonValue::toSharedPtr
JsonValuePtr toSharedPtr() const
Definition: JsonValue.cpp:164
armarx::JsonValue::Create
static JsonValuePtr Create(const std::string &value)
Definition: JsonValue.cpp:65
armarx::JsonValue::clone
JsonDataPtr clone() override
Definition: JsonValue.cpp:243
armarx::JsonWriterPtr
std::shared_ptr< JsonWriter > JsonWriterPtr
Definition: JsonWriter.h:30
armarx::JsonValue::CheckNumber
static bool CheckNumber(const std::string &value)
Definition: JsonValue.cpp:124
StringHelpers.h
armarx::JsonValue::asFloat
float asFloat() const
Definition: JsonValue.cpp:211
armarx::JsonValue::eString
@ eString
Definition: JsonValue.h:38
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::JsonValue::eNull
@ eNull
Definition: JsonValue.h:38
JsonValue.h
armarx::JsonValue::CheckNull
static bool CheckNull(const std::string &value)
Definition: JsonValue.cpp:143
armarx::JsonValue::eBool
@ eBool
Definition: JsonValue.h:38
armarx::JsonValue::asString
std::string asString() const
Definition: JsonValue.cpp:202
armarx::JsonValue::writeJson
void writeJson(const JsonWriterPtr &writer) override
Definition: JsonValue.cpp:148
armarx::JsonWriter::EscapeQuote
static std::string EscapeQuote(const std::string &str)
Definition: JsonWriter.cpp:122
armarx::JsonValue::getType
Type getType()
Definition: JsonValue.cpp:249
armarx::JsonValue::ToString
static std::string ToString(int value)
Definition: JsonValue.cpp:170
armarx::JsonValue::CheckValue
static bool CheckValue(Type type, const std::string &value)
Definition: JsonValue.cpp:105
armarx::JsonValue::JsonValue
JsonValue(const std::string &value)
Definition: JsonValue.cpp:29
armarx::JsonDataPtr
std::shared_ptr< JsonData > JsonDataPtr
Definition: JsonData.h:31
armarx::JsonValue::CheckBool
static bool CheckBool(const std::string &value)
Definition: JsonValue.cpp:138
armarx::JsonValue::CreateRaw
static JsonValuePtr CreateRaw(Type type, const std::string &value)
Definition: JsonValue.cpp:95
armarx::JsonValue::asBool
bool asBool() const
Definition: JsonValue.cpp:229
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::JsonValue::Null
static JsonValuePtr Null()
Definition: JsonValue.cpp:49
Exception.h
armarx::JsonValue::asInt
int asInt() const
Definition: JsonValue.cpp:220