From d1c715e7a80111c85c24448b8a18bceeb0bb54ca Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Wed, 21 Aug 2024 16:41:27 +0200 Subject: [PATCH] chore(unit_tests,userspace): use nlhomann json instead of jsoncpp. Signed-off-by: Federico Di Pierro --- CMakeLists.txt | 1 + unit_tests/falco/test_configuration_schema.cpp | 2 +- userspace/falco/configuration.cpp | 5 +---- userspace/falco/configuration.h | 3 +-- userspace/falco/yaml_helper.h | 15 ++++++++------- 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 484d1522..bd1686cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,6 +151,7 @@ if (NOT EMSCRIPTEN) endif() include(zlib) +include(valijson) if (NOT MINIMAL_BUILD) if (NOT WIN32 AND NOT APPLE AND NOT EMSCRIPTEN) include(cares) diff --git a/unit_tests/falco/test_configuration_schema.cpp b/unit_tests/falco/test_configuration_schema.cpp index 79c5f22f..bf891b99 100644 --- a/unit_tests/falco/test_configuration_schema.cpp +++ b/unit_tests/falco/test_configuration_schema.cpp @@ -108,7 +108,7 @@ TEST(Configuration, schema_yaml_helper_validator) // We pass a string variable but not a schema std::string validation; - EXPECT_NO_THROW(conf.load_from_string(sample_yaml, Json::Value{}, &validation)); + EXPECT_NO_THROW(conf.load_from_string(sample_yaml, nlohmann::json{}, &validation)); EXPECT_EQ(validation, yaml_helper::validation_none); // We pass a schema but not a string storage for the validation; no validation takes place diff --git a/userspace/falco/configuration.cpp b/userspace/falco/configuration.cpp index 78ae6d07..47b480da 100644 --- a/userspace/falco/configuration.cpp +++ b/userspace/falco/configuration.cpp @@ -651,10 +651,7 @@ falco_configuration::falco_configuration(): m_metrics_convert_memory_to_mb(true), m_metrics_include_empty_values(false) { - if(!Json::Reader().parse(schema_json_string, m_config_schema) || m_config_schema.type() != Json::objectValue) - { - throw falco_exception("failed to parse config schema"); - } + m_config_schema = nlohmann::json::parse(schema_json_string); } config_loaded_res falco_configuration::init_from_content(const std::string& config_content, const std::vector& cmdline_options, const std::string& filename) diff --git a/userspace/falco/configuration.h b/userspace/falco/configuration.h index 40a5f10c..ef7fd5f9 100644 --- a/userspace/falco/configuration.h +++ b/userspace/falco/configuration.h @@ -31,7 +31,6 @@ limitations under the License. #include #include #include -#include #include "config_falco.h" #include "yaml_helper.h" @@ -197,7 +196,7 @@ public: // Needed by tests yaml_helper m_config; - Json::Value m_config_schema; + nlohmann::json m_config_schema; private: void merge_config_files(const std::string& config_name, config_loaded_res &res); diff --git a/userspace/falco/yaml_helper.h b/userspace/falco/yaml_helper.h index 90c8645b..990cf6bb 100644 --- a/userspace/falco/yaml_helper.h +++ b/userspace/falco/yaml_helper.h @@ -33,7 +33,8 @@ limitations under the License. #include #include -#include +#include +#include #include #include #include @@ -92,7 +93,7 @@ public: /** * Load the YAML document represented by the input string. */ - void load_from_string(const std::string& input, const Json::Value& schema={}, std::string *validation=nullptr) + void load_from_string(const std::string& input, const nlohmann::json& schema={}, std::string *validation=nullptr) { m_root = YAML::Load(input); pre_process_env_vars(m_root); @@ -113,12 +114,12 @@ public: /** * Load the YAML document from the given file path. */ - void load_from_file(const std::string& path, const Json::Value& schema={}, std::string *validation=nullptr) + void load_from_file(const std::string& path, const nlohmann::json& schema={}, std::string *validation=nullptr) { m_root = load_from_file_int(path, schema, validation); } - void include_config_file(const std::string& include_file_path, const Json::Value& schema={}, std::string *validation=nullptr) + void include_config_file(const std::string& include_file_path, const nlohmann::json& schema={}, std::string *validation=nullptr) { auto loaded_nodes = load_from_file_int(include_file_path, schema, validation); for(auto n : loaded_nodes) @@ -206,7 +207,7 @@ public: private: YAML::Node m_root; - YAML::Node load_from_file_int(const std::string& path, const Json::Value& schema={}, std::string *validation=nullptr) + YAML::Node load_from_file_int(const std::string& path, const nlohmann::json& schema={}, std::string *validation=nullptr) { auto root = YAML::LoadFile(path); pre_process_env_vars(root); @@ -225,7 +226,7 @@ private: return root; } - std::string validate_node(const YAML::Node &node, const Json::Value& schema={}) + std::string validate_node(const YAML::Node &node, const nlohmann::json& schema={}) { // Validate the yaml against our json schema valijson::Schema schemaDef; @@ -233,7 +234,7 @@ private: valijson::Validator validator(valijson::Validator::kWeakTypes); valijson::ValidationResults validationResults; valijson::adapters::YamlCppAdapter configAdapter(node); - valijson::adapters::JsonCppAdapter schemaAdapter(schema); + valijson::adapters::NlohmannJsonAdapter schemaAdapter(schema); schemaParser.populateSchema(schemaAdapter, schemaDef); if (!validator.validate(schemaDef, configAdapter, &validationResults))