1 #ifndef RAPIDJSON_COOLPROP_H
2 #define RAPIDJSON_COOLPROP_H
5 #if defined(__powerpc__)
6 typedef unsigned int UINT32;
8 # define RAPIDJSON_NO_INT64DEFINE
14 #include "externals/rapidjson/include/rapidjson/rapidjson.h"
15 #include "externals/rapidjson/include/rapidjson/document.h"
16 #include "externals/rapidjson/include/rapidjson/filewritestream.h"
17 #include "externals/rapidjson/include/rapidjson/prettywriter.h"
18 #include "externals/rapidjson/include/rapidjson/stringbuffer.h"
19 #include "externals/rapidjson/include/rapidjson/schema.h"
27 doc.Parse<0>(JSON_string.c_str());
28 if (doc.HasParseError()) {
35 bool isnull,
isfalse,
istrue,
isbool,
isobject,
isarray,
isnumber,
isint,
isint64,
isuint,
isuint64,
isdouble,
isstring;
56 rapidjson::StringBuffer buffer;
57 rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
60 return buffer.GetString();
63 inline int get_integer(
const rapidjson::Value& v, std::string m) {
64 if (!v.HasMember(m.c_str())) {
67 const rapidjson::Value& el = v[m.c_str()];
75 inline double get_double(
const rapidjson::Value& v, std::string m) {
76 if (!v.HasMember(m.c_str())) {
79 const rapidjson::Value& el = v[m.c_str()];
83 return el.GetDouble();
87 inline bool get_bool(
const rapidjson::Value& v, std::string m) {
88 if (!v.HasMember(m.c_str())) {
91 const rapidjson::Value& el = v[m.c_str()];
99 inline std::string
get_string(
const rapidjson::Value& v, std::string m) {
100 if (!v.HasMember(m.c_str())) {
103 const rapidjson::Value& el = v[m.c_str()];
104 if (!el.IsString()) {
107 return el.GetString();
113 std::vector<double> out;
117 for (rapidjson::Value::ConstValueIterator itr = v.Begin(); itr != v.End(); ++itr) {
118 if (!itr->IsNumber()) {
121 out.push_back(itr->GetDouble());
128 if (!v.HasMember(m.c_str())) {
137 std::vector<CoolPropDbl> out;
141 for (rapidjson::Value::ConstValueIterator itr = v.Begin(); itr != v.End(); ++itr) {
142 if (!itr->IsNumber()) {
145 out.push_back(itr->GetDouble());
152 std::vector<std::vector<double>> out;
153 std::vector<double> tmp;
157 for (rapidjson::Value::ConstValueIterator itr = v.Begin(); itr != v.End(); ++itr) {
160 if (!(itr->IsArray())) {
164 for (rapidjson::Value::ConstValueIterator i = itr->Begin(); i != itr->End(); ++i) {
165 if (!i->IsNumber()) {
168 tmp.push_back(i->GetDouble());
177 std::vector<std::vector<CoolPropDbl>> out;
178 std::vector<CoolPropDbl> tmp;
182 for (rapidjson::Value::ConstValueIterator itr = v.Begin(); itr != v.End(); ++itr) {
183 if (!itr->IsArray()) {
187 for (rapidjson::Value::ConstValueIterator i = itr->Begin(); i != itr->End(); ++i) {
188 if (!i->IsNumber()) {
191 tmp.push_back(i->GetDouble());
200 std::vector<CoolPropDbl> out;
201 if (!v.HasMember(name.c_str())) {
204 if (!v[name.c_str()].IsArray()) {
207 for (rapidjson::Value::ConstValueIterator itr = v[name.c_str()].Begin(); itr != v[name.c_str()].End(); ++itr) {
208 if (!itr->IsNumber()) {
211 out.push_back(itr->GetDouble());
218 std::vector<std::string> out;
222 for (rapidjson::Value::ConstValueIterator itr = v.Begin(); itr != v.End(); ++itr) {
223 out.push_back(itr->GetString());
229 inline std::vector<std::string>
get_string_array(
const rapidjson::Value& v, std::string m) {
230 if (!v.HasMember(m.c_str())) {
238 template <
typename T>
240 rapidjson::StringBuffer buffer;
241 rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
243 return buffer.GetString();
247 inline void set_double_array2D(
const char* key,
const std::vector<std::vector<double>>& vec, rapidjson::Value& value, rapidjson::Document& doc) {
248 rapidjson::Value _i(rapidjson::kArrayType);
249 for (
unsigned int i = 0; i < vec.size(); ++i) {
250 rapidjson::Value _j(rapidjson::kArrayType);
251 for (
unsigned int j = 0; j < vec[i].size(); ++j) {
252 rapidjson::Value v(rapidjson::kNumberType);
253 v.SetDouble(vec[i][j]);
254 _j.PushBack(v, doc.GetAllocator());
256 _i.PushBack(_j, doc.GetAllocator());
258 value.AddMember(rapidjson::Value(key, doc.GetAllocator()).Move(), _i, doc.GetAllocator());
262 inline void set_string(
const std::string& key,
const std::string& s, rapidjson::Value& value, rapidjson::Document& doc) {
263 value.AddMember(rapidjson::Value(key.c_str(), doc.GetAllocator()).Move(), rapidjson::Value(s.c_str(), doc.GetAllocator()).Move(),
268 inline void set_string_array(
const char* key,
const std::vector<std::string>& vec, rapidjson::Value& value, rapidjson::Document& doc) {
269 rapidjson::Value _v(rapidjson::kArrayType);
270 for (
unsigned int i = 0; i < vec.size(); ++i) {
271 _v.PushBack(rapidjson::Value(vec[i].c_str(), doc.GetAllocator()).Move(), doc.GetAllocator());
273 value.AddMember(rapidjson::Value(key, doc.GetAllocator()).Move(), _v, doc.GetAllocator());
277 inline void set_int_array(
const char* key,
const std::vector<int>& vec, rapidjson::Value& value, rapidjson::Document& doc) {
278 rapidjson::Value _v(rapidjson::kArrayType);
279 for (
unsigned int i = 0; i < vec.size(); ++i) {
280 _v.PushBack(vec[i], doc.GetAllocator());
282 value.AddMember(rapidjson::Value(key, doc.GetAllocator()).Move(), _v, doc.GetAllocator());
286 inline void set_double_array(
const char* key,
const std::vector<double>& vec, rapidjson::Value& value, rapidjson::Document& doc) {
287 rapidjson::Value _v(rapidjson::kArrayType);
288 for (
unsigned int i = 0; i < vec.size(); ++i) {
289 _v.PushBack(vec[i], doc.GetAllocator());
291 value.AddMember(rapidjson::Value(key, doc.GetAllocator()).Move(), _v, doc.GetAllocator());
295 inline void set_long_double_array(
const char*
const key,
const std::vector<CoolPropDbl>& vec, rapidjson::Value& value, rapidjson::Document& doc) {
296 rapidjson::Value _v(rapidjson::kArrayType);
297 for (
unsigned int i = 0; i < vec.size(); ++i) {
298 _v.PushBack(
static_cast<double>(vec[i]), doc.GetAllocator());
300 value.AddMember(rapidjson::Value(key, doc.GetAllocator()).Move(), _v, doc.GetAllocator());
314 rapidjson::Document sd;
315 sd.Parse(schemaJson.c_str());
316 if (sd.HasParseError()) {
317 errstr =
format(
"Invalid schema: %s\n", schemaJson.c_str());
320 rapidjson::SchemaDocument schema(sd);
322 rapidjson::Document d;
323 d.Parse(inputJson.c_str());
324 if (d.HasParseError()) {
325 errstr =
format(
"Invalid input json: %s\n", inputJson.c_str());
329 rapidjson::SchemaValidator validator(schema);
330 if (!d.Accept(validator)) {
333 errstr =
to_string(validator.GetError());