Bridges-C++  3.2.0
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)
12  : why(why), filename(filename), linenumber(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 
31  namespace JSONUtil {
35 
36  template <typename T>
37  inline std::string JSONencode(const T& d) {
38  rapidjson::Value s;
39  s.Set(d);
40 
41  rapidjson::StringBuffer buffer;
42  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
43  s.Accept(writer);
44 
45  const char* output = buffer.GetString();
46 
47  std::string ss = output;
48  return ss;
49  }
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 
59  rapidjson::StringBuffer buffer;
60  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
61  s.Accept(writer);
62 
63  const char* output = buffer.GetString();
64 
65  std::string ss = output;
66  return ss;
67  }
68 
69  inline std::string JSONencode (const char* str) {
70  rapidjson::Value s;
71  s.SetString(rapidjson::StringRef(str));
72 
73 
74  rapidjson::StringBuffer buffer;
75  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
76  s.Accept(writer);
77 
78  const char* output = buffer.GetString();
79 
80  std::string ss = output;
81  return ss;
82  }
83 
84 
85  //precision=-1 means to use max precision.
86  //otherwise number of digits to use
87  inline std::string JSONencode(const double& d, int precision = -1) {
88  rapidjson::Value s;
89  s.Set(d);
90 
91  rapidjson::StringBuffer buffer;
92  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
93  if (precision > 0 )
94  writer.SetMaxDecimalPlaces(precision);
95  s.Accept(writer);
96 
97  const char* output = buffer.GetString();
98 
99  std::string ss = output;
100  return ss;
101  }
102 
103  //precision=-1 means to use max precision.
104  //otherwise number of digits to use
105  inline std::string JSONencode(const float& d, int precision = -1) {
106  rapidjson::Value s;
107  s.Set(d);
108 
109  rapidjson::StringBuffer buffer;
110  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
111  if (precision > 0 )
112  writer.SetMaxDecimalPlaces(precision);
113  s.Accept(writer);
114 
115  const char* output = buffer.GetString();
116 
117  std::string ss = output;
118  return ss;
119  }
120 
121 
122 
123  }
124 }
125 
126 #endif
rapidjson_exception(const std::string &why, const std::string &filename, const int linenumber)
Definition: JSONutil.h:11
std::string why
Definition: JSONutil.h:7
int linenumber
Definition: JSONutil.h:9
std::string JSONencode(const float &d, int precision=-1)
Definition: JSONutil.h:105
Definition: JSONutil.h:6
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
std::string filename
Definition: JSONutil.h:8