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 #ifndef MIRA_WINDOWS
65 // export the json templates to avoid recurring instantiations and therefore reduce compile time
66 extern template class json_spirit::Config_map<std::string>;
67 extern template class json_spirit::Value_impl<json_spirit::Config_map<std::string>>;
68 #endif
69 
70 namespace mira {
71 
73 
74 namespace json {
75 
77 
79 
84 template <typename T>
85 struct IsBaseType :
86  boost::mpl::bool_<boost::is_same<T, std::string>::value || boost::is_arithmetic<T>::value>
87 {};
88 
93 template <typename T>
94 struct TypeTrait
95 {
96  typedef T type;
97 };
98 
102 #define MIRA_JSON_TRAIT(B,T) \
103 template <> \
104 struct TypeTrait<T> \
105 { \
106  typedef B type; \
107 };
108 
109 MIRA_JSON_TRAIT(int, char)
110 MIRA_JSON_TRAIT(int, uint8)
111 MIRA_JSON_TRAIT(int, uint16)
112 MIRA_JSON_TRAIT(int64, uint32)
113 MIRA_JSON_TRAIT(int, int8)
114 MIRA_JSON_TRAIT(int, int16)
115 MIRA_JSON_TRAIT(int, int32)
116 MIRA_JSON_TRAIT(double, float)
119 MIRA_JSON_TRAIT(double, long double)
120 
121 
122 
125 template <typename T>
126 typename TypeTrait<T>::type cast(const T& value)
127 {
128  return static_cast<typename TypeTrait<T>::type>(value);
129 }
130 
131 template <typename T>
132 T reverse_cast(const typename TypeTrait<T>::type& value)
133 {
134  return static_cast<T>(value);
135 }
136 
138 
140 
145 class JSONDefaultPrecision : public Singleton<JSONDefaultPrecision>
146 {
148 
149 protected:
151 
153  static const unsigned int MIRA_JSON_DEFAULT_PRECISION = 3;
154 
155 public:
157  static void set(unsigned int precision) { instance().mPrecision = precision; }
158 
160  static void reset() { instance().mPrecision = MIRA_JSON_DEFAULT_PRECISION; }
161 
163  static unsigned int get() { return instance().mPrecision; }
164 
165 private:
166  unsigned int mPrecision;
167 };
168 
170 
176 typedef json_spirit::mValue Value;
177 
183 typedef json_spirit::mObject Object;
184 
190 typedef json_spirit::mArray Array;
191 
193 
203 MIRA_BASE_EXPORT void write(const Value& value, std::ostream& ioStream,
204  bool formatted=false, int precision = -1);
205 
216 MIRA_BASE_EXPORT std::string write(const Value& value, bool formatted=false,
217  int precision = -1);
218 
225 MIRA_BASE_EXPORT void read(const std::string& s, Value& oValue);
226 
233 MIRA_BASE_EXPORT void read(std::istream& ioStream, Value& oValue);
234 
242 MIRA_BASE_EXPORT inline void read(std::string::const_iterator& begin,
243  std::string::const_iterator& end,
244  Value& oValue);
245 
268 MIRA_BASE_EXPORT Value getElement(const Value& iValue,
269  const std::string& elementName);
270 
280 MIRA_BASE_EXPORT bool hasElement(const Value& iValue, const std::string& element);
281 
284  bool exists;
286  std::string error;
287 };
288 
300  const std::string& elementName);
301 
323 MIRA_BASE_EXPORT double getNumberElement(const Value& iValue,
324  const std::string& elementName);
325 
335 MIRA_BASE_EXPORT bool hasNumberElement(const Value& iValue, const std::string& element);
336 
339  bool exists;
340  double number;
341  std::string error;
342 };
343 
355  const std::string& elementName);
356 
358 
359 } // json
360 
361 
368 
369 template <typename OStream>
370 inline OStream& operator<<(OStream& os, const JSONValue& value)
371 {
372  os << json::write(value, false);
373  return os;
374 }
375 
377 {
378  os << json::write(value, false, 0);
379  return os;
380 }
381 
383 {
384  os << json::write(value, false, 0);
385  return os;
386 }
387 
388 template <typename IStream>
389 inline IStream& operator>>(IStream& is, JSONValue& value)
390 {
391  std::string tmp;
392  is >> tmp;
393  json::read(tmp, value);
394  return is;
395 }
396 
397 template <>
398 class IsAtomicSerializable<JSONValue> : public std::true_type {};
399 
401 
402 } // mira
403 
404 #endif
MIRA_BASE_EXPORT 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:160
IStream & operator>>(IStream &is, JSONValue &value)
Definition: JSON.h:389
Implementation of the CreationPolicy that is used by the Singleton template.
Definition: Singleton.h:174
static Type & instance()
Returns a reference to the singleton instance.
Definition: Singleton.h:544
json_spirit::mArray Array
A representation of an array (vector) in JSON.
Definition: JSON.h:190
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
A struct used for return value of getElementIfExists()
Definition: JSON.h:283
MIRA_BASE_EXPORT 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...
Type trait that indicates whether a type can be serialized as an atomic value.
Definition: IsAtomicSerializable.h:71
std::string error
empty if exists is true
Definition: JSON.h:341
std::ostream & operator<<(std::ostream &s, const LibraryVersion &version)
MIRA_BASE_EXPORT 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...
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:338
MIRA_BASE_EXPORT 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.
Commonly used exception classes.
A singleton template class that can be freely configured using policies that control the instantiatio...
Definition: Singleton.h:531
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
MIRA_BASE_EXPORT void read(const std::string &s, Value &oValue)
Read a json::Value from a string that contains JSON format.
json_spirit::mObject Object
A representation of an object (class, struct) in JSON.
Definition: JSON.h:183
static const unsigned int MIRA_JSON_DEFAULT_PRECISION
Default value if not changed using set().
Definition: JSON.h:153
MIRA_BASE_EXPORT 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 ...
JSONDefaultPrecision()
Definition: JSON.h:150
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:176
JSONDefaultPrecision is a singleton that provides a mechanism to control the default precision for ou...
Definition: JSON.h:145
json::Array JSONArray
Imports the json::Array type into mira namespace.
Definition: JSON.h:367
Contains the BinaryIStream and BinaryOStream classes for fast and efficient streaming of binary data...
Value value
undefined if exists is false
Definition: JSON.h:285
std::string error
empty if exists is true
Definition: JSON.h:286
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:365
bool exists
Definition: JSON.h:339
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
MIRA_BASE_EXPORT double getNumberElement(const Value &iValue, const std::string &elementName)
Get a number element/member from a json::Value See getElement() for basic syntax. ...
json::Value JSONValue
Imports the json::Value type into mira namespace.
Definition: JSON.h:363
bool exists
Definition: JSON.h:284
double number
undefined if exists is false
Definition: JSON.h:340
MIRA_BASE_EXPORT Value getElement(const Value &iValue, const std::string &elementName)
Get a json::Value element/member from a json::Value Syntax: