Bridges-C++  3.4.5-dev1-6-g935685a
Bridges(C++ API)
JSONutil.h
Go to the documentation of this file.
1 #ifndef JSON_UTIL_
2 #define JSON_UTIL_
3 
4 #include <sstream>
5 
7  std::string why;
8  std::string filename;
9  int linenumber;
10 
11  rapidjson_exception(const std::string & why, const std::string& filename, const int linenumber)
13 
14  operator std::string() const {
15  std::stringstream ss;
16  ss << why << " " << filename << " " << linenumber;
17  return ss.str();
18  }
19 };
20 
21 #ifndef RAPIDJSON_ASSERT
22 #define RAPIDJSON_ASSERT(x) if (!( x )) { throw rapidjson_exception( #x, __FILE__, __LINE__ ) ;}
23 #endif
24 #define RAPIDJSON_ASSERT_THROWS
25 
26 #include <rapidjson/document.h>
27 #include <rapidjson/stringbuffer.h>
28 #include <rapidjson/writer.h>
29 
30 namespace bridges {
31 
32  namespace JSONUtil {
36 
37  template <typename T>
38  inline std::string JSONencode(const T& d) {
39  rapidjson::Value s;
40  s.Set(d);
41 
42  rapidjson::StringBuffer buffer;
43  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
44  s.Accept(writer);
45 
46  const char* output = buffer.GetString();
47 
48  std::string ss = output;
49  return ss;
50  }
51 
52  template <>
53  inline std::string JSONencode<std::string> (const std::string& str) {
54 
55  rapidjson::Value s;
56  s.SetString(rapidjson::StringRef(str.c_str()));
57 
58  rapidjson::StringBuffer buffer;
59  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
60  s.Accept(writer);
61 
62  const char* output = buffer.GetString();
63 
64  std::string ss = output;
65  return ss;
66  }
67 
68  inline std::string JSONencode (const char* str) {
69  rapidjson::Value s;
70  s.SetString(rapidjson::StringRef(str));
71 
72  rapidjson::StringBuffer buffer;
73  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
74  s.Accept(writer);
75 
76  const char* output = buffer.GetString();
77 
78  std::string ss = output;
79  return ss;
80  }
81 
82  //precision=-1 means to use max precision.
83  //otherwise number of digits to use
84  inline std::string JSONencode(const double& d, int precision = -1) {
85  rapidjson::Value s;
86  s.Set(d);
87 
88  rapidjson::StringBuffer buffer;
89  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
90  if (precision > 0 )
91  writer.SetMaxDecimalPlaces(precision);
92  s.Accept(writer);
93 
94  const char* output = buffer.GetString();
95 
96  std::string ss = output;
97  return ss;
98  }
99 
100  //precision=-1 means to use max precision.
101  //otherwise number of digits to use
102  inline std::string JSONencode(const float& d, int precision = -1) {
103  rapidjson::Value s;
104  s.Set(d);
105 
106  rapidjson::StringBuffer buffer;
107  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
108  if (precision > 0 )
109  writer.SetMaxDecimalPlaces(precision);
110  s.Accept(writer);
111 
112  const char* output = buffer.GetString();
113 
114  std::string ss = output;
115  return ss;
116  }
117 
118  }
119 }
120 
121 #endif
std::string JSONencode(const T &d)
Definition: JSONutil.h:38
Support for drawing Bar charts.
Definition: alltypes.h:4
Definition: JSONutil.h:6
std::string filename
Definition: JSONutil.h:8
std::string why
Definition: JSONutil.h:7
int linenumber
Definition: JSONutil.h:9
rapidjson_exception(const std::string &why, const std::string &filename, const int linenumber)
Definition: JSONutil.h:11