teqp 0.19.1
Loading...
Searching...
No Matches
json_tools.hpp
Go to the documentation of this file.
1#pragma once
2#include "nlohmann/json.hpp"
3#include <nlohmann/json-schema.hpp>
4
5#include <set>
6#include <filesystem>
7#include <fstream>
8#include "teqp/exceptions.hpp"
9
10#include <Eigen/Dense>
11
12using nlohmann::json;
13using nlohmann::json_schema::json_validator;
14
15namespace teqp{
16
18 inline nlohmann::json load_a_JSON_file(const std::string& path) {
19 if (!std::filesystem::is_regular_file(path)) {
20 throw std::invalid_argument("Path to be loaded does not exist: " + path);
21 }
22 auto stream = std::ifstream(path);
23 if (!stream) {
24 throw std::invalid_argument("File stream cannot be opened from: " + path);
25 }
26 try {
27 return nlohmann::json::parse(stream);
28 }
29 catch (...) {
30 throw std::invalid_argument("File at " + path + " is not valid JSON");
31 }
32 }
33
34 inline auto all_same_length(const nlohmann::json& j, const std::vector<std::string>& ks) {
35 std::set<decltype(j[0].size())> lengths;
36 for (auto k : ks) { lengths.insert(j.at(k).size()); }
37 return lengths.size() == 1;
38 }
39
40 inline auto build_square_matrix = [](const nlohmann::json& j){
41 if (j.is_null() || (j.is_array() && j.size() == 0)){
42 return Eigen::ArrayXXd(0, 0);
43 }
44 try{
45 const std::valarray<std::valarray<double>> m = j;
46 // First assume that the matrix is square, resize
47 Eigen::ArrayXXd mat(m.size(), m.size());
48 if (m.size() == 0){
49 return mat;
50 }
51 // Then copy elements over
52 for (auto i = 0U; i < m.size(); ++i){
53 auto row = m[i];
54 if (row.size() != static_cast<std::size_t>(mat.rows())){
55 throw std::invalid_argument("provided matrix is not square");
56 }
57 for (auto k = 0U; k < row.size(); ++k){
58 mat(i, k) = row[k];
59 }
60 }
61 return mat;
62 }
63 catch(const nlohmann::json::exception&){
64 throw teqp::InvalidArgument("Unable to convert this kmat to a NxN matrix of doubles:" + j.dump(2));
65 }
66 };
67
77 inline auto multilevel_JSON_load(const nlohmann::json &j, const std::string& default_path){
78
79 auto is_valid_path = [](const std::string & s){
80 try{
81 return std::filesystem::is_regular_file(s) || true; // this will return true if the function CAN BE CALLED without exception, indicating it could be a path
82 }
83 catch(...){
84 return false;
85 }
86 };
87
88 // If not provided (NULL, empty array or empty string), load from the default path provided
89 if (j.is_null() || (j.is_array() && j.empty()) || (j.is_string() && j.get<std::string>().empty())){
90 return load_a_JSON_file(default_path);
91 }
92 else if (j.is_object()){
93 // Assume we are already providing the thing
94 return j;
95 }
96 else if (j.is_array() && j.size() > 0){
97 return j;
98 }
99 else if (j.is_string()){
100 // If a string, either data in JSON format, or a path-like thing
101 std::string s = j.get<std::string>();
102
103 // If path to existing file, use it
104 if (is_valid_path(s) && std::filesystem::is_regular_file(s)){
105 return load_a_JSON_file(s);
106 }
107 // Or assume it is a string in JSON format
108 else{
109 return nlohmann::json::parse(s);
110 }
111 }
112 else{
113 throw teqp::InvalidArgument("Unable to load the argument to multilevel_JSON_load");
114 }
115 }
116
121 public:
122 const nlohmann::json schema;
123
124 json_validator validator; // create validator
125
126 // Instantiate the validator object, will throw if the schema is invalid
127 JSONValidator(const nlohmann::json& schema) : schema(schema) {
128 validator.set_root_schema(schema); // insert root-schema
129 }
130
131 // Return the validation errors when trying to validate the JSON
132 std::vector<std::string> get_validation_errors(const nlohmann::json& j) const{
133
134 /* Custom error handler */
135 class custom_error_handler : public nlohmann::json_schema::basic_error_handler
136 {
137 public:
138 std::vector<std::string> errors;
139 void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
140 {
141 nlohmann::json_schema::basic_error_handler::error(ptr, instance, message);
142 std::stringstream ss;
143 ss << ptr << ":" << instance << "': " << message << "\n";
144 errors.push_back(ss.str());
145 }
146 } handler;
147 validator.validate(j, handler); // validate the document
148 return handler.errors;
149 }
150
151 // A quick checker for validation of the JSON
152 bool is_valid(const nlohmann::json&j ) const { return get_validation_errors(j).empty(); }
153 };
154
155}
JSONValidator(const nlohmann::json &schema)
json_validator validator
bool is_valid(const nlohmann::json &j) const
const nlohmann::json schema
std::vector< std::string > get_validation_errors(const nlohmann::json &j) const
auto build_square_matrix
nlohmann::json load_a_JSON_file(const std::string &path)
Load a JSON file from a specified file.
auto all_same_length(const nlohmann::json &j, const std::vector< std::string > &ks)
auto multilevel_JSON_load(const nlohmann::json &j, const std::string &default_path)