1#ifndef COOLPROP_DETAIL_JSON_H
2#define COOLPROP_DETAIL_JSON_H
29#include <nlohmann/json.hpp>
30#include <valijson/adapters/nlohmann_json_adapter.hpp>
31#include <valijson/schema.hpp>
32#include <valijson/schema_parser.hpp>
33#include <valijson/validator.hpp>
39inline nlohmann::json
parse(std::string_view text) {
42 }
catch (
const std::exception& e) {
49inline nlohmann::json
from_cbor(
const std::uint8_t* data, std::size_t size) {
52 }
catch (
const std::exception& e) {
62inline int get_integer(
const nlohmann::json& v,
const std::string& m) {
69 if (it->is_number_unsigned()) {
70 const std::uint64_t uval = it->get<std::uint64_t>();
71 if (uval >
static_cast<std::uint64_t
>(std::numeric_limits<int>::max()))
73 return static_cast<int>(uval);
75 const std::int64_t val = it->get<std::int64_t>();
76 if (val < std::numeric_limits<int>::min() || val > std::numeric_limits<int>::max())
78 return static_cast<int>(val);
81inline double get_double(
const nlohmann::json& v,
const std::string& m) {
85 return it->get<
double>();
88inline bool get_bool(
const nlohmann::json& v,
const std::string& m) {
92 return it->get<
bool>();
95inline std::string
get_string(
const nlohmann::json& v,
const std::string& m) {
99 return it->get<std::string>();
104 std::vector<double> out;
105 out.reserve(v.size());
106 for (
const auto& el : v) {
108 out.push_back(el.get<
double>());
113inline std::vector<double>
get_double_array(
const nlohmann::json& v,
const std::string& m) {
121 std::vector<CoolPropDbl> out;
122 out.reserve(v.size());
123 for (
const auto& el : v) {
125 out.push_back(
static_cast<CoolPropDbl>(el.get<
double>()));
131 auto it = v.find(name);
138 std::vector<std::vector<double>> out;
139 for (
const auto& row : v) {
148 std::vector<std::vector<CoolPropDbl>> out;
149 for (
const auto& row : v) {
158 std::vector<std::string> out;
159 out.reserve(v.size());
160 for (
const auto& el : v) {
162 out.push_back(el.get<std::string>());
167inline std::vector<std::string>
get_string_array(
const nlohmann::json& v,
const std::string& m) {
187 nlohmann::json schemaDoc;
190 }
catch (
const std::exception& e) {
191 errstr = std::string(
"Invalid schema: ") + e.what();
195 nlohmann::json inputDoc;
198 }
catch (
const std::exception& e) {
199 errstr = std::string(
"Invalid input json: ") + e.what();
203 valijson::Schema schema;
204 valijson::SchemaParser parser;
205 valijson::adapters::NlohmannJsonAdapter schemaAdapter(schemaDoc);
207 parser.populateSchema(schemaAdapter, schema);
208 }
catch (
const std::exception& e) {
209 errstr = std::string(
"Invalid schema: ") + e.what();
213 valijson::Validator validator;
214 valijson::ValidationResults results;
215 valijson::adapters::NlohmannJsonAdapter inputAdapter(inputDoc);
217 if (!validator.validate(schema, inputAdapter, &results)) {
219 valijson::ValidationResults::Error error;
220 while (results.popError(error)) {
221 for (
const std::string& ctx : error.context)
223 msg +=
": " + error.description +
"\n";
228 }
catch (
const std::exception& e) {
229 errstr = std::string(
"Schema validation internal error: ") + e.what();