0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 07:23:53 +01:00

ircd::json: Various fixes/cleanup/conversions.

This commit is contained in:
Jason Volk 2017-09-19 02:09:19 -07:00
parent 38c1d2e195
commit 4c50b2794f
2 changed files with 44 additions and 19 deletions

View file

@ -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<class... T>
struct tuple
@ -63,6 +63,8 @@ struct tuple
using tuple_type = std::tuple<T...>;
using super_type = tuple<T...>;
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<tuple, i + 1>(name);
}
template<class tuple>
constexpr bool
key_exists(const string_view &key)
{
return indexof<tuple>(key) < size<tuple>();
}
template<size_t i,
class tuple>
enable_if_tuple<tuple, tuple_value_type<tuple, i> &>
@ -801,5 +810,21 @@ operator<<(std::ostream &s, const tuple<T...> &t)
return s;
}
template<class... T>
tuple<T...>::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

View file

@ -24,20 +24,6 @@
#include <ircd/spirit.h>
#include <boost/fusion/include/at.hpp>
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<class it>
struct ircd::json::input
:qi::grammar<it, unused_type>