chore(unit_tests,userspace): use nlhomann json instead of jsoncpp.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
This commit is contained in:
Federico Di Pierro 2024-08-21 16:41:27 +02:00 committed by poiana
parent be927edfe8
commit d1c715e7a8
5 changed files with 12 additions and 14 deletions

View File

@ -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)

View File

@ -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

View File

@ -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<std::string>& cmdline_options, const std::string& filename)

View File

@ -31,7 +31,6 @@ limitations under the License.
#include <set>
#include <iostream>
#include <fstream>
#include <json/json.h>
#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);

View File

@ -33,7 +33,8 @@ limitations under the License.
#include <fstream>
#include <filesystem>
#include <valijson/adapters/jsoncpp_adapter.hpp>
#include <nlohmann/json.hpp>
#include <valijson/adapters/nlohmann_json_adapter.hpp>
#include <valijson/adapters/yaml_cpp_adapter.hpp>
#include <valijson/schema.hpp>
#include <valijson/schema_parser.hpp>
@ -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))