MIRA
JSON.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_JSON_H_
48 #define _MIRA_JSON_H_
49 
50 #include <iostream>
51 
52 #ifndef Q_MOC_RUN
53 #include <json_spirit_value.h>
54 #endif
55 
56 #include <error/Exceptions.h>
57 
59 
60 #include <utils/Singleton.h>
61 
62 #include <stream/BinaryStream.h>
63 
64 // export the json templates to avoid recurring instantiations and therefore reduce compile time
65 extern template class json_spirit::Config_map<std::string>;
66 extern template class json_spirit::Value_impl<json_spirit::Config_map<std::string>>;
67 
68 namespace mira {
69 
71 
72 namespace json {
73 
75 
77 
82 template <typename T>
83 struct IsBaseType :
84  boost::mpl::bool_<boost::is_same<T, std::string>::value || boost::is_arithmetic<T>::value>
85 {};
86 
91 template <typename T>
92 struct TypeTrait
93 {
94  typedef T type;
95 };
96 
100 #define MIRA_JSON_TRAIT(B,T) \
101 template <> \
102 struct TypeTrait<T> \
103 { \
104  typedef B type; \
105 };
106 
107 MIRA_JSON_TRAIT(int, char)
108 MIRA_JSON_TRAIT(int, uint8)
109 MIRA_JSON_TRAIT(int, uint16)
110 MIRA_JSON_TRAIT(int64, uint32)
111 MIRA_JSON_TRAIT(int, int8)
112 MIRA_JSON_TRAIT(int, int16)
113 MIRA_JSON_TRAIT(int, int32)
114 MIRA_JSON_TRAIT(double, float)
117 MIRA_JSON_TRAIT(double, long double)
118 
119 
120 
123 template <typename T>
124 typename TypeTrait<T>::type cast(const T& value)
125 {
126  return static_cast<typename TypeTrait<T>::type>(value);
127 }
128 
129 template <typename T>
130 T reverse_cast(const typename TypeTrait<T>::type& value)
131 {
132  return static_cast<T>(value);
133 }
134 
136 
138 
143 class JSONDefaultPrecision : public Singleton<JSONDefaultPrecision>
144 {
146 
147 protected:
149 
151  static const unsigned int MIRA_JSON_DEFAULT_PRECISION = 3;
152 
153 public:
155  static void set(unsigned int precision) { instance().mPrecision = precision; }
156 
158  static void reset() { instance().mPrecision = MIRA_JSON_DEFAULT_PRECISION; }
159 
161  static unsigned int get() { return instance().mPrecision; }
162 
163 private:
164  unsigned int mPrecision;
165 };
166 
168 
174 typedef json_spirit::mValue Value;
175 
181 typedef json_spirit::mObject Object;
182 
188 typedef json_spirit::mArray Array;
189 
191 
201 void write(const Value& value, std::ostream& ioStream,
202  bool formatted=false, int precision = -1);
203 
214 std::string write(const Value& value, bool formatted=false,
215  int precision = -1);
216 
223 void read(const std::string& s, Value& oValue);
224 
231 void read(std::istream& ioStream, Value& oValue);
232 
240 inline void read(std::string::const_iterator& begin,
241  std::string::const_iterator& end,
242  Value& oValue);
243 
266 Value getElement(const Value& iValue,
267  const std::string& elementName);
268 
278 bool hasElement(const Value& iValue, const std::string& element);
279 
282  bool exists;
284  std::string error;
285 };
286 
298  const std::string& elementName);
299 
321 double getNumberElement(const Value& iValue,
322  const std::string& elementName);
323 
333 bool hasNumberElement(const Value& iValue, const std::string& element);
334 
337  bool exists;
338  double number;
339  std::string error;
340 };
341 
353  const std::string& elementName);
354 
356 
357 } // json
358 
359 
366 
367 template <>
368 class IsAtomicSerializable<JSONValue> : public std::true_type {};
369 
371 
372 } // namespace mira
373 
374 namespace json_spirit {
375 
377 
378 template <typename OStream>
379 inline OStream& operator<<(OStream& os, const mira::JSONValue& value)
380 {
381  os << mira::json::write(value, false);
382  return os;
383 }
384 
386 {
387  os << mira::json::write(value, false, 0);
388  return os;
389 }
390 
392 {
393  os << mira::json::write(value, false, 0);
394  return os;
395 }
396 
397 template <typename IStream>
398 inline IStream& operator>>(IStream& is, mira::JSONValue& value)
399 {
400  std::string tmp;
401  is >> tmp;
402  mira::json::read(tmp, value);
403  return is;
404 }
405 
406 template <typename CharT>
407 inline std::basic_stringstream<CharT>& operator>>(std::basic_stringstream<CharT>& is, mira::JSONValue& value)
408 {
409  mira::json::read(is, value);
410  is.seekg(-1, std::ios_base::cur);
411  return is;
412 }
413 
415 
416 } // namespace json_spirit
417 
418 
419 #endif
void write(const Value &value, std::ostream &ioStream, bool formatted=false, int precision=-1)
Writes a json::Value into a given stream using the JSON format.
OStream & operator<<(OStream &os, const mira::JSONValue &value)
Definition: JSON.h:379
bool hasNumberElement(const Value &iValue, const std::string &element)
Query existence of number element/member in a json::Value Use this variant to only query whether the ...
static void reset()
Reset default precision for json::write() to initial default value (MIRA_JSON_DEFAULT_PRECISION).
Definition: JSON.h:158
Implementation of the CreationPolicy that is used by the Singleton template.
Definition: Singleton.h:149
static Type & instance()
Returns a reference to the singleton instance.
Definition: Singleton.h:508
json_spirit::mArray Array
A representation of an array (vector) in JSON.
Definition: JSON.h:188
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
IStream & operator>>(IStream &is, mira::JSONValue &value)
Definition: JSON.h:398
Definition: JSON.h:374
A struct used for return value of getElementIfExists()
Definition: JSON.h:281
void read(const std::string &s, Value &oValue)
Read a json::Value from a string that contains JSON format.
QueryValueResult getElementIfExists(const Value &iValue, const std::string &elementName)
Get a json::Value element/member from a json::Value if it exists, otherwise the returned result will ...
QueryNumberResult getNumberElementIfExists(const Value &iValue, const std::string &elementName)
Get a number element/member from a json::Value if it exists, otherwise the returned result will conta...
uint32_t uint32
Definition: Types.h:64
Type trait that indicates whether a type can be serialized as an atomic value.
Definition: IsAtomicSerializable.h:83
std::string error
empty if exists is true
Definition: JSON.h:339
Output stream adapter that can be assigned to any output stream and allows binary output using the <<...
Definition: BinaryStream.h:293
A struct used for return value of getNumberElementIfExists()
Definition: JSON.h:336
Commonly used exception classes.
A singleton template class that can be freely configured using policies that control the instantiatio...
Definition: Singleton.h:495
A singleton class that can be freely configured using policies that control the creation, instantiation, lifetime and thread-safety.
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
double getNumberElement(const Value &iValue, const std::string &elementName)
Get a number element/member from a json::Value See getElement() for basic syntax. ...
Value getElement(const Value &iValue, const std::string &elementName)
Get a json::Value element/member from a json::Value Syntax:
uint8_t uint8
Definition: Types.h:62
json_spirit::mObject Object
A representation of an object (class, struct) in JSON.
Definition: JSON.h:181
static const unsigned int MIRA_JSON_DEFAULT_PRECISION
Default value if not changed using set().
Definition: JSON.h:151
uint16_t uint16
Definition: Types.h:63
JSONDefaultPrecision()
Definition: JSON.h:148
json_spirit::mValue Value
A value is an abstract description of data in JSON (underlying data can either be one of the JSON bas...
Definition: JSON.h:174
int8_t int8
Definition: Types.h:58
JSONDefaultPrecision is a singleton that provides a mechanism to control the default precision for ou...
Definition: JSON.h:143
json::Array JSONArray
Imports the json::Array type into mira namespace.
Definition: JSON.h:365
int16_t int16
Definition: Types.h:59
Contains the BinaryIStream and BinaryOStream classes for fast and efficient streaming of binary data...
int32_t int32
Definition: Types.h:60
Value value
undefined if exists is false
Definition: JSON.h:283
int64_t int64
Definition: Types.h:61
std::string error
empty if exists is true
Definition: JSON.h:284
Provides type trait that indicates whether a type can be serialized as atomic value.
PropertyHint precision(int p)
Sets the attribute "precision".
Definition: PropertyHint.h:285
json::Object JSONObject
Imports the json::Object type into mira namespace.
Definition: JSON.h:363
bool exists
Definition: JSON.h:337
bool hasElement(const Value &iValue, const std::string &element)
Query existence of element/member in a json::Value Use this variant to only query whether the element...
json::Value JSONValue
Imports the json::Value type into mira namespace.
Definition: JSON.h:361
bool exists
Definition: JSON.h:282
double number
undefined if exists is false
Definition: JSON.h:338