diff --git a/include/ircd/json/tuple.h b/include/ircd/json/tuple.h index 8fa301278..06a441fde 100644 --- a/include/ircd/json/tuple.h +++ b/include/ircd/json/tuple.h @@ -49,11 +49,11 @@ struct tuple_base /// /// Create and use a tuple to efficiently extract members from a json::object. /// The tuple will populate its own members during a single-pass iteration of -/// the JSON input. If the JSON does not contain a member specified in the -/// tuple, the value will be default initialized. If the JSON contains a member -/// not specified in the tuple, it is ignored. If you need to know all of the -/// members specified in the JSON dynamically, use a json::index or iterate -/// manually. +/// the JSON input. +/// +/// But remember, the tuple carries very little information for you at runtime +/// which may make it difficult to represent all JS phenomena like "undefined" +/// and "null". /// template struct tuple @@ -63,6 +63,8 @@ struct tuple using tuple_type = std::tuple; using super_type = tuple; + operator json::value() const; + static constexpr size_t size(); tuple(const json::object &); @@ -181,6 +183,13 @@ indexof(const string_view &name) return equal? i : indexof(name); } +template +constexpr bool +key_exists(const string_view &key) +{ + return indexof(key) < size(); +} + template enable_if_tuple &> @@ -801,5 +810,21 @@ operator<<(std::ostream &s, const tuple &t) return s; } +template +tuple::operator +json::value() +const +{ + json::value ret; + ret.type = OBJECT; + ret.create_string(serialized(*this), [this] + (mutable_buffer buffer) + { + stringify(buffer, *this); + }); + return ret; + +} + } // namespace json } // namespace ircd diff --git a/ircd/json.cc b/ircd/json.cc index 9bf6a2cdd..a0447d5f2 100644 --- a/ircd/json.cc +++ b/ircd/json.cc @@ -24,20 +24,6 @@ #include #include -BOOST_FUSION_ADAPT_STRUCT -( - ircd::json::object::member, - ( decltype(ircd::json::object::member::first), first ) - ( decltype(ircd::json::object::member::second), second ) -) - -BOOST_FUSION_ADAPT_STRUCT -( - ircd::json::member, - ( decltype(ircd::json::member::first), first ) - ( decltype(ircd::json::member::second), second ) -) - namespace ircd::json { namespace spirit = boost::spirit; @@ -78,6 +64,20 @@ namespace ircd::json struct ostreamer extern const ostreamer; } +BOOST_FUSION_ADAPT_STRUCT +( + ircd::json::member, + ( decltype(ircd::json::member::first), first ) + ( decltype(ircd::json::member::second), second ) +) + +BOOST_FUSION_ADAPT_STRUCT +( + ircd::json::object::member, + ( decltype(ircd::json::object::member::first), first ) + ( decltype(ircd::json::object::member::second), second ) +) + template struct ircd::json::input :qi::grammar