2#include "nlohmann/json.hpp"
3#include <nlohmann/json-schema.hpp>
13using nlohmann::json_schema::json_validator;
19 if (!std::filesystem::is_regular_file(path)) {
20 throw std::invalid_argument(
"Path to be loaded does not exist: " + path);
22 auto stream = std::ifstream(path);
24 throw std::invalid_argument(
"File stream cannot be opened from: " + path);
27 return nlohmann::json::parse(stream);
30 throw std::invalid_argument(
"File at " + path +
" is not valid JSON");
34 inline void JSON_to_file(
const nlohmann::json& jsondata,
const std::string& path){
35 std::ofstream file(path);
34 inline void JSON_to_file(
const nlohmann::json& jsondata,
const std::string& path) {
…}
39 inline auto all_same_length(
const nlohmann::json& j,
const std::vector<std::string>& ks) {
40 std::set<
decltype(j[0].size())> lengths;
41 for (
auto k : ks) { lengths.insert(j.at(k).size()); }
42 return lengths.size() == 1;
39 inline auto all_same_length(
const nlohmann::json& j,
const std::vector<std::string>& ks) {
…}
46 if (j.is_null() || (j.is_array() && j.size() == 0)){
47 return Eigen::ArrayXXd(0, 0);
50 const std::valarray<std::valarray<double>> m = j;
52 Eigen::ArrayXXd mat(m.size(), m.size());
57 for (
auto i = 0U; i < m.size(); ++i){
59 if (row.size() !=
static_cast<std::size_t
>(mat.rows())){
60 throw std::invalid_argument(
"provided matrix is not square");
62 for (
auto k = 0U; k < row.size(); ++k){
68 catch(
const nlohmann::json::exception&){
82 inline auto multilevel_JSON_load(
const nlohmann::json &j,
const std::optional<std::string>& default_path = std::nullopt){
84 auto is_valid_path = [](
const std::string & s){
86 return std::filesystem::is_regular_file(s) ||
true;
94 if (j.is_null() || (j.is_array() && j.empty()) || (j.is_string() && j.get<std::string>().empty())){
99 throw teqp::InvalidArgument(
"default path was not provided, and cannot load this thing: " + j.dump(1));
102 else if (j.is_object()){
106 else if (j.is_array() && j.size() > 0){
109 else if (j.is_string()){
111 std::string s = j.get<std::string>();
114 if (is_valid_path(s) && std::filesystem::is_regular_file(s)){
119 return nlohmann::json::parse(s);
82 inline auto multilevel_JSON_load(
const nlohmann::json &j,
const std::optional<std::string>& default_path = std::nullopt) {
…}
145 class custom_error_handler :
public nlohmann::json_schema::basic_error_handler
148 std::vector<std::string> errors;
149 void error(
const nlohmann::json::json_pointer &ptr,
const json &instance,
const std::string &message)
override
151 nlohmann::json_schema::basic_error_handler::error(ptr, instance, message);
152 std::stringstream ss;
153 ss << ptr <<
":" << instance <<
"': " << message <<
"\n";
154 errors.push_back(ss.str());
158 return handler.errors;
JSONValidator(const nlohmann::json &schema)
bool is_valid(const nlohmann::json &j) const
const nlohmann::json schema
std::vector< std::string > get_validation_errors(const nlohmann::json &j) const
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)
void JSON_to_file(const nlohmann::json &jsondata, const std::string &path)
auto multilevel_JSON_load(const nlohmann::json &j, const std::optional< std::string > &default_path=std::nullopt)