NRP Core  1.4.1
json_schema_utils.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef JSON_SCHEMA_UTILS_H
23 #define JSON_SCHEMA_UTILS_H
24 
25 #include "nrp_general_library/config/cmake_constants.h"
27 #include "nlohmann/json-schema.hpp"
28 #include <fstream>
29 #include "iostream"
30 
31 using nlohmann::json;
32 using nlohmann::json_schema::json_validator;
33 using nlohmann::json_uri;
34 
35 using jsonSharedPtr = std::shared_ptr<nlohmann::json>;
36 using jsonConstSharedPtr = std::shared_ptr<const nlohmann::json>;
37 
38 namespace json_utils {
39 
45  nlohmann::json parseJSONFile(const std::string &fileName);
46 
54  void jsonSchemaLoader(const json_uri &uri, json &schema);
55 
63  template <typename VALUE_T>
64  static void setDefault(nlohmann::json &instance, const std::string key, const VALUE_T new_value) {
65  // if key doesn't exist set it with new_value
66  if(!instance.contains(key))
67  instance[key] = new_value;
68  }
69 
70 
80  void validateJson(nlohmann::json &instance, std::string schema_path, bool addPatch = true);
81 
82 }
83 
84 #endif // JSON_SCHEMA_UTILS_H
85 
86 // EOF
jsonConstSharedPtr
std::shared_ptr< const nlohmann::json > jsonConstSharedPtr
Definition: json_schema_utils.h:36
json_utils::validateJson
void validateJson(nlohmann::json &instance, std::string schema_path, bool addPatch)
Validates a json object using a given json schema.
Definition: json_schema_utils.cpp:65
nrp_exceptions.h
json_utils
Definition: json_schema_utils.cpp:24
jsonSharedPtr
std::shared_ptr< nlohmann::json > jsonSharedPtr
Definition: json_schema_utils.h:35
json_utils::jsonSchemaLoader
void jsonSchemaLoader(const json_uri &uri, json &schema)
Loads schema from json file given its uri. To be passed to a nlohmann::json_schema::json_validator co...
Definition: json_schema_utils.cpp:49
json_utils::parseJSONFile
nlohmann::json parseJSONFile(const std::string &fileName)
Parse a JSON File and return it's values.
Definition: json_schema_utils.cpp:26
json
nlohmann::json json
Definition: engine_json_server.cpp:31