diff --git a/include/Void/Data/Json.hpp b/include/Void/Data/Json.hpp index c02b3ab..3994c4a 100644 --- a/include/Void/Data/Json.hpp +++ b/include/Void/Data/Json.hpp @@ -227,6 +227,56 @@ namespace Void::Json return *p; } + // ── typed member accessors (safe navigation) ────────────────── + // Convenience getters for walking dynamic JSON without manual + // find()+type-check chains. A missing key, or a present key of the + // wrong type, yields the supplied default (scalars) or nullptr + // (object/array children) — never throws. + + [[nodiscard]] bool has(std::string_view key) const noexcept + { + return find(key) != nullptr; + } + + [[nodiscard]] std::string + get_str(std::string_view key, + std::string_view fallback = {}) const + { + const Value* c = find(key); + return (c && c->is_string()) ? c->as_string() + : std::string{fallback}; + } + + [[nodiscard]] double get_num(std::string_view key, + double fallback = 0.0) const noexcept + { + const Value* c = find(key); + return (c && c->is_number()) ? c->as_number() : fallback; + } + + [[nodiscard]] bool get_bool(std::string_view key, + bool fallback = false) const noexcept + { + const Value* c = find(key); + return (c && c->is_bool()) ? c->as_bool() : fallback; + } + + // Child only when it exists and is an object; else nullptr. + [[nodiscard]] const Value* + child_object(std::string_view key) const noexcept + { + const Value* c = find(key); + return (c && c->is_object()) ? c : nullptr; + } + + // Child only when it exists and is an array; else nullptr. + [[nodiscard]] const Value* + child_array(std::string_view key) const noexcept + { + const Value* c = find(key); + return (c && c->is_array()) ? c : nullptr; + } + // ── JSON Pointer (RFC 6901) query // ───────────────────────────────────────── at("/a/b/0") walks // object keys and array indices. Returns nullptr if any step is diff --git a/tests/standalone/Data/JsonAccessors.cpp b/tests/standalone/Data/JsonAccessors.cpp new file mode 100644 index 0000000..799ca5a --- /dev/null +++ b/tests/standalone/Data/JsonAccessors.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +#include + +using Void::Json::parse; + +// Typed safe-navigation member accessors on Json::Value. + +Void::Testing::Test json_accessors_present() +{ + const auto v = + parse("{\"s\":\"hi\",\"n\":42,\"b\":true,\"o\":{\"x\":1},\"a\":[1,2]}"); + Void::Testing::expect(v.has_value()); + Void::Testing::expect(v->has("s")); + Void::Testing::expect_eq(v->get_str("s"), std::string{"hi"}); + Void::Testing::expect_eq(v->get_num("n"), 42.0); + Void::Testing::expect(v->get_bool("b")); + Void::Testing::expect(v->child_object("o") != nullptr); + Void::Testing::expect(v->child_array("a") != nullptr); +} + +Void::Testing::Test json_accessors_missing_and_wrong_type() +{ + const auto v = parse("{\"s\":\"hi\",\"n\":42}"); + Void::Testing::expect(v.has_value()); + Void::Testing::expect(!v->has("nope")); + Void::Testing::expect_eq(v->get_str("nope", "def"), std::string{"def"}); + Void::Testing::expect_eq(v->get_num("nope", -1.0), -1.0); + Void::Testing::expect(!v->get_bool("nope")); + Void::Testing::expect(v->child_object("nope") == nullptr); + Void::Testing::expect(v->child_array("nope") == nullptr); + // Wrong type → fallback / nullptr. + Void::Testing::expect_eq(v->get_str("n", "x"), std::string{"x"}); + Void::Testing::expect(v->child_object("s") == nullptr); + Void::Testing::expect(v->child_array("n") == nullptr); +} + +Void::Testing::Test json_accessors_on_non_object() +{ + const auto v = parse("[1,2,3]"); + Void::Testing::expect(v.has_value()); + Void::Testing::expect(!v->has("k")); + Void::Testing::expect_eq(v->get_str("k", "d"), std::string{"d"}); +}