From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 3 Apr 2026 23:01:45 -0700 Subject: [PATCH 19/25] Add to_json, from_json, and json_serializer struct --- json.h | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/json.h b/json.h index 62af49cb18b713304ad95e89c52f5cc5bbc4b626..e52435a79ee7537928ba59fb146f635fc8511bfc 100644 --- a/json.h +++ b/json.h @@ -26,6 +26,35 @@ namespace wpi::util { +class json; + +template +struct json_serializer; + +namespace detail { +template +concept HasToJson = requires(json& j, const T& val) { + { to_json(j, val) }; +}; + +template +concept HasFromJson = requires(const json& j, T& val) { + { from_json(j, val) }; +}; + +template +concept HasJsonSerializer = requires (json& j, const json& cj, const T& val) { + typename json_serializer>; + { json_serializer>::to(j, val) }; +}; + +template +concept HasJsonDeserializer = requires (json& j, const json& cj, const T& val) { + typename json_serializer>; + { json_serializer>::from(cj) } -> std::convertible_to>; +}; +} // namespace detail + class json { friend bool operator==(const json& lhs, const json& rhs); @@ -152,6 +181,18 @@ class json { } + template + json(const T& value) : type_(Type::Null) + { + to_json(*this, value); + } + + template + json(const T& value) : type_(Type::Null) + { + json_serializer::to(*this, value); + } + constexpr Type type() const { return type_; @@ -214,6 +255,18 @@ class json object_t& get_object(); const object_t& get_object() const; + template + T get() const { + T value; + from_json(*this, value); + return value; + } + + template + T get() const { + return json_serializer::from(*this); + } + bool contains(std::string_view) const; json* lookup(std::string_view); @@ -228,6 +281,18 @@ class json json& operator=(const json&); json& operator=(json&&); + template + json& operator=(const T& value) { + to_json(*this, value); + return *this; + } + + template + json& operator=(const T& value) { + json_serializer::to(*this, value); + return *this; + } + json& operator[](size_t); json& operator[](std::string_view);