diff --git a/include/json2cpp/json2cpp.hpp b/include/json2cpp/json2cpp.hpp index 180a84d..14638d9 100644 --- a/include/json2cpp/json2cpp.hpp +++ b/include/json2cpp/json2cpp.hpp @@ -126,6 +126,8 @@ template struct data_variant // cppcheck-suppress noExplicitConstructor constexpr data_variant(std::basic_string_view s) : value{ s }, selected{ selected_type::string } {} + [[nodiscard]] constexpr bool is_boolean() const noexcept { return selected == selected_type::boolean; } + [[nodiscard]] constexpr const bool *get_if_boolean() const noexcept { if (selected == selected_type::boolean) { @@ -135,51 +137,68 @@ template struct data_variant } } + [[nodiscard]] constexpr bool is_array() const noexcept { return selected == selected_type::array; } + [[nodiscard]] constexpr const basic_array_t *get_if_array() const noexcept { - if (selected == selected_type::array) { + if (is_array()) { return &value.array_; } else { return nullptr; } } + + [[nodiscard]] constexpr bool is_object() const noexcept { return selected == selected_type::object; } + [[nodiscard]] constexpr const basic_object_t *get_if_object() const noexcept { - if (selected == selected_type::object) { + if (is_object()) { return &value.object_; } else { return nullptr; } } + + [[nodiscard]] constexpr bool is_integer() const noexcept { return selected == selected_type::integer; } + [[nodiscard]] constexpr const std::int64_t *get_if_integer() const noexcept { - if (selected == selected_type::integer) { + if (is_integer()) { return &value.int64_t_; } else { return nullptr; } } + + [[nodiscard]] constexpr bool is_uinteger() const noexcept { return selected == selected_type::uinteger; } + [[nodiscard]] constexpr const std::uint64_t *get_if_uinteger() const noexcept { - if (selected == selected_type::uinteger) { + if (is_uinteger()) { return &value.uint64_t_; } else { return nullptr; } } + + [[nodiscard]] constexpr bool is_floating_point() const noexcept { return selected == selected_type::floating_point; } + + [[nodiscard]] constexpr const double *get_if_floating_point() const noexcept { - if (selected == selected_type::floating_point) { + if (is_floating_point()) { return &value.double_; } else { return nullptr; } } + [[nodiscard]] constexpr bool is_string() const noexcept { return selected == selected_type::string; } + [[nodiscard]] constexpr const std::basic_string_view *get_if_string() const noexcept { - if (selected == selected_type::string) { + if (is_string()) { return &value.string_view_; } else { return nullptr; @@ -199,7 +218,7 @@ template struct basic_json : parent_value_(&value), index_{ index } {} - constexpr const basic_json &operator*() const noexcept + constexpr const basic_json &operator*() const { if (parent_value_->is_array()) { return (*parent_value_)[index_]; @@ -340,7 +359,7 @@ template struct basic_json } } - template[[nodiscard]] constexpr std::size_t count(const Key &key) const noexcept + template [[nodiscard]] constexpr std::size_t count(const Key &key) const { if (is_object()) { const auto found = find(key); @@ -353,7 +372,7 @@ template struct basic_json return 0; } - [[nodiscard]] constexpr iterator find(const std::basic_string_view key) const noexcept + [[nodiscard]] constexpr iterator find(const std::basic_string_view key) const { for (auto itr = begin(); itr != end(); ++itr) { if (itr.key() == key) { return itr; } @@ -369,8 +388,8 @@ template struct basic_json constexpr const auto &array_data() const { - if (const auto *result = data.get_if_array(); result != nullptr) { - return *result; + if (data.is_array()) { + return *data.get_if_array(); } else { throw std::runtime_error("value is not an array type"); } @@ -378,8 +397,8 @@ template struct basic_json constexpr const auto &object_data() const { - if (const auto *result = data.get_if_object(); result != nullptr) { - return *result; + if (data.is_object()) { + return *data.get_if_object(); } else { throw std::runtime_error("value is not an object type"); } @@ -388,35 +407,37 @@ template struct basic_json constexpr static basic_json object() { return basic_json{ data_t{ basic_object_t{} } }; } constexpr static basic_json array() { return basic_json{ data_t{ basic_array_t{} } }; } - template[[nodiscard]] constexpr auto get() const + template [[nodiscard]] constexpr auto get() const { // I don't like this level of implicit conversions in the `get()` function, // but it's necessary for API compatibility with nlohmann::json - if constexpr (std::is_same_v || std::is_same_v || std::is_same_v) { - if (const auto *uint_value = data.get_if_uinteger(); uint_value != nullptr) { - return Type(*uint_value); - } else if (const auto *value = data.get_if_integer(); value != nullptr) { - return Type(*value); - } else if (const auto *fpvalue = data.get_if_floating_point(); fpvalue != nullptr) { - return Type(*fpvalue); + if constexpr (std::is_same_v || std::is_same_v || std::is_same_v) { + if (data.is_uinteger()) { + return Type(*data.get_if_uinteger()); + } else if (data.is_integer()) { + return Type(*data.get_if_integer()); + } else if (data.is_floating_point()) { + return Type(*data.get_if_floating_point()); } else { throw std::runtime_error("Unexpected type: number requested");// + ss.str() ); } } else if constexpr (std::is_same_v> || std::is_same_v>) { - if (const auto *value = data.get_if_string(); value != nullptr) { return *value; } - else { + if (data.is_string()) { + return *data.get_if_string(); + } else { throw std::runtime_error("Unexpected type: string-like requested"); } } else if constexpr (std::is_same_v) { - if (const auto *value = data.get_if_boolean(); value != nullptr) { return *value; } - else { + if (data.is_boolean()) { + return *data.get_if_boolean(); + } else { throw std::runtime_error("Unexpected type: bool requested"); } } else { throw std::runtime_error("Unexpected type for get()"); } - } [[nodiscard]] constexpr bool is_object() const noexcept { return data.selected == data_t::selected_type::object; } diff --git a/include/json2cpp/json2cpp_adapter.hpp b/include/json2cpp/json2cpp_adapter.hpp index eb68a3c..b923a35 100644 --- a/include/json2cpp/json2cpp_adapter.hpp +++ b/include/json2cpp/json2cpp_adapter.hpp @@ -494,7 +494,7 @@ namespace adapters { bool operator!=(const json2cppJsonArrayValueIterator &other) const { return !(m_itr == other.m_itr); } - // cppcheck-suppress functionConst + // cppcheck-suppress functionConst const json2cppJsonArrayValueIterator &operator++() { ++m_itr; @@ -502,7 +502,7 @@ namespace adapters { return *this; } - // cppcheck-suppress functionConst + // cppcheck-suppress functionConst json2cppJsonArrayValueIterator operator++(int) { json2cppJsonArrayValueIterator iterator_pre(m_itr); @@ -510,7 +510,7 @@ namespace adapters { return iterator_pre; } - // cppcheck-suppress functionConst + // cppcheck-suppress functionConst const json2cppJsonArrayValueIterator &operator--() { --m_itr; @@ -575,7 +575,7 @@ namespace adapters { bool operator!=(const json2cppJsonObjectMemberIterator &other) const { return !(m_itr == other.m_itr); } - // cppcheck-suppress functionConst + // cppcheck-suppress functionConst const json2cppJsonObjectMemberIterator &operator++() { ++m_itr; @@ -583,7 +583,7 @@ namespace adapters { return *this; } - // cppcheck-suppress functionConst + // cppcheck-suppress functionConst json2cppJsonObjectMemberIterator operator++(int) { json2cppJsonObjectMemberIterator iterator_pre(m_itr); @@ -591,7 +591,7 @@ namespace adapters { return iterator_pre; } - // cppcheck-suppress functionConst + // cppcheck-suppress functionConst const json2cppJsonObjectMemberIterator &operator--() { --m_itr; diff --git a/src/json2cpp.cpp b/src/json2cpp.cpp index 0f0610b..d64ca97 100644 --- a/src/json2cpp.cpp +++ b/src/json2cpp.cpp @@ -23,7 +23,6 @@ SOFTWARE. */ - #include "json2cpp.hpp" #include @@ -36,13 +35,12 @@ std::string compile(const nlohmann::json &value, std::size_t &obj_count, std::ve if (value.is_object()) { std::vector pairs; for (auto itr = value.begin(); itr != value.end(); ++itr) { - pairs.push_back(fmt::format( - "value_pair_t{{{}, {{{}}}}},", json_string(itr.key()), compile(*itr, obj_count, lines))); + pairs.push_back( + fmt::format("value_pair_t{{{}, {{{}}}}},", json_string(itr.key()), compile(*itr, obj_count, lines))); } - lines.push_back(fmt::format("inline constexpr std::array object_data_{} = {{", - pairs.size(), - current_object_number)); + lines.push_back(fmt::format( + "inline constexpr std::array object_data_{} = {{", pairs.size(), current_object_number)); std::transform(pairs.begin(), pairs.end(), std::back_inserter(lines), [](const auto &pair) { return fmt::format(" {}", pair); @@ -58,9 +56,8 @@ std::string compile(const nlohmann::json &value, std::size_t &obj_count, std::ve }); - lines.push_back(fmt::format("inline constexpr std::array object_data_{} = {{{{", - entries.size(), - current_object_number)); + lines.push_back(fmt::format( + "inline constexpr std::array object_data_{} = {{{{", entries.size(), current_object_number)); std::transform(entries.begin(), entries.end(), std::back_inserter(lines), [](const auto &entry) { return fmt::format(" {}", entry); @@ -112,8 +109,7 @@ compile_results compile(const std::string_view document_name, const nlohmann::js results.impl.emplace_back("#include "); - results.impl.push_back( - fmt::format(R"( + results.impl.push_back(fmt::format(R"( namespace compiled_json::{}::impl {{ using json = json2cpp::basic_json; @@ -123,7 +119,8 @@ using array_t=json2cpp::basic_array_t; using object_t=json2cpp::basic_object_t; using value_pair_t=json2cpp::basic_value_pair_t; -)", document_name)); +)", + document_name)); const auto last_obj_name = compile(json, obj_count, results.impl); @@ -136,7 +133,8 @@ inline constexpr auto document = json{{{{{}}}}}; #endif -)", last_obj_name)); +)", + last_obj_name)); spdlog::info("{} JSON objects processed.", obj_count); @@ -178,7 +176,10 @@ void write_compilation([[maybe_unused]] std::string_view document_name, std::ofstream cpp(cpp_name); cpp << fmt::format("#include \"{}\"\n", impl_name.filename().string()); - cpp << fmt::format("namespace compiled_json::{} {{\nconst json2cpp::json &get() {{ return compiled_json::{}::impl::document; }}\n}}\n", document_name, document_name); + cpp << fmt::format( + "namespace compiled_json::{} {{\nconst json2cpp::json &get() {{ return compiled_json::{}::impl::document; }}\n}}\n", + document_name, + document_name); } void compile_to(const std::string_view document_name, diff --git a/src/schema_validator.cpp b/src/schema_validator.cpp index b0fd199..05b4b8d 100644 --- a/src/schema_validator.cpp +++ b/src/schema_validator.cpp @@ -163,8 +163,7 @@ void walk_internal(std::int64_t &int_sum, } } -template -void walk(const JSON &objects) +template void walk(const JSON &objects) { std::int64_t int_sum{}; double double_sum{}; @@ -174,13 +173,7 @@ void walk(const JSON &objects) spdlog::info("Starting tree walk"); - walk_internal(int_sum, - double_sum, - string_sizes, - array_count, - object_count, - objects - ); + walk_internal(int_sum, double_sum, string_sizes, array_count, object_count, objects); spdlog::info("{} {} {} {} {}", int_sum, double_sum, string_sizes, array_count, object_count); } @@ -193,9 +186,9 @@ int main(int argc, const char **argv) true,// show help if requested "schema_validator 0.0.1 Copyright 2022 Jason Turner");// version string - if (args.at("--walk").asBool()) { + if (args.at("--walk").asBool()) { if (args.at("--internal").asBool()) { - walk(compiled_json::energyplus_schema::get()); + walk(compiled_json::energyplus_schema::get()); } else { std::filesystem::path schema_file_name = args.at("").asString(); spdlog::info("Creating nlohmann::json object"); diff --git a/test/valijson_tests.cpp b/test/valijson_tests.cpp index a89107e..c2a835f 100644 --- a/test/valijson_tests.cpp +++ b/test/valijson_tests.cpp @@ -19,8 +19,7 @@ TEST_CASE("Can load a valijson schema") // Parse JSON schema content using valijson Schema mySchema; SchemaParser parser; - json2cppJsonAdapter mySchemaAdapter( - compiled_json::allof_integers_and_numbers_schema::get()); + json2cppJsonAdapter mySchemaAdapter(compiled_json::allof_integers_and_numbers_schema::get()); CHECK_NOTHROW(parser.populateSchema(mySchemaAdapter, mySchema)); } @@ -36,8 +35,7 @@ TEST_CASE("Validation fails where expected") // Parse JSON schema content using valijson Schema mySchema; SchemaParser parser; - json2cppJsonAdapter mySchemaAdapter( - compiled_json::allof_integers_and_numbers_schema::get()); + json2cppJsonAdapter mySchemaAdapter(compiled_json::allof_integers_and_numbers_schema::get()); CHECK_NOTHROW(parser.populateSchema(mySchemaAdapter, mySchema)); Validator validator; @@ -58,8 +56,7 @@ TEST_CASE("Can validate a document") // Parse JSON schema content using valijson Schema mySchema; SchemaParser parser; - json2cppJsonAdapter mySchemaAdapter( - compiled_json::allof_integers_and_numbers_schema::get()); + json2cppJsonAdapter mySchemaAdapter(compiled_json::allof_integers_and_numbers_schema::get()); CHECK_NOTHROW(parser.populateSchema(mySchemaAdapter, mySchema)); Validator validator;