Bridges-C++  3.4.4
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 #define RAPIDJSON_ASSERT(x) if (!( x )) { throw rapidjson_exception( #x, __FILE__, __LINE__ ) ;}
22 #define RAPIDJSON_ASSERT_THROWS
23 
24 #include <rapidjson/document.h>
25 #include <rapidjson/stringbuffer.h>
26 #include <rapidjson/writer.h>
27 
28 namespace bridges {
29 
30  namespace JSONUtil {
34 
35  template <typename T>
36  inline std::string JSONencode(const T& d) {
37  rapidjson::Value s;
38  s.Set(d);
39 
40  rapidjson::StringBuffer buffer;
41  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
42  s.Accept(writer);
43 
44  const char* output = buffer.GetString();
45 
46  std::string ss = output;
47  return ss;
48  }
49 
50  template <>
51  inline std::string JSONencode<std::string> (const std::string& str) {
52 
53  rapidjson::Value s;
54  s.SetString(rapidjson::StringRef(str.c_str()));
55 
56  rapidjson::StringBuffer buffer;
57  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
58  s.Accept(writer);
59 
60  const char* output = buffer.GetString();
61 
62  std::string ss = output;
63  return ss;
64  }
65 
66  inline std::string JSONencode (const char* str) {
67  rapidjson::Value s;
68  s.SetString(rapidjson::StringRef(str));
69 
70  rapidjson::StringBuffer buffer;
71  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
72  s.Accept(writer);
73 
74  const char* output = buffer.GetString();
75 
76  std::string ss = output;
77  return ss;
78  }
79 
80  //precision=-1 means to use max precision.
81  //otherwise number of digits to use
82  inline std::string JSONencode(const double& d, int precision = -1) {
83  rapidjson::Value s;
84  s.Set(d);
85 
86  rapidjson::StringBuffer buffer;
87  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
88  if (precision > 0 )
89  writer.SetMaxDecimalPlaces(precision);
90  s.Accept(writer);
91 
92  const char* output = buffer.GetString();
93 
94  std::string ss = output;
95  return ss;
96  }
97 
98  //precision=-1 means to use max precision.
99  //otherwise number of digits to use
100  inline std::string JSONencode(const float& d, int precision = -1) {
101  rapidjson::Value s;
102  s.Set(d);
103 
104  rapidjson::StringBuffer buffer;
105  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
106  if (precision > 0 )
107  writer.SetMaxDecimalPlaces(precision);
108  s.Accept(writer);
109 
110  const char* output = buffer.GetString();
111 
112  std::string ss = output;
113  return ss;
114  }
115 
116  }
117 }
118 
119 #endif
std::string JSONencode(const T &d)
Definition: JSONutil.h:36
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
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