mirror of
https://github.com/matrix-construct/construct
synced 2024-10-01 05:08:59 +02:00
ircd::json: Various fixes/cleanup/conversions.
This commit is contained in:
parent
38c1d2e195
commit
4c50b2794f
2 changed files with 44 additions and 19 deletions
|
@ -49,11 +49,11 @@ struct tuple_base
|
||||||
///
|
///
|
||||||
/// Create and use a tuple to efficiently extract members from a json::object.
|
/// 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 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
|
/// the JSON input.
|
||||||
/// 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
|
/// But remember, the tuple carries very little information for you at runtime
|
||||||
/// members specified in the JSON dynamically, use a json::index or iterate
|
/// which may make it difficult to represent all JS phenomena like "undefined"
|
||||||
/// manually.
|
/// and "null".
|
||||||
///
|
///
|
||||||
template<class... T>
|
template<class... T>
|
||||||
struct tuple
|
struct tuple
|
||||||
|
@ -63,6 +63,8 @@ struct tuple
|
||||||
using tuple_type = std::tuple<T...>;
|
using tuple_type = std::tuple<T...>;
|
||||||
using super_type = tuple<T...>;
|
using super_type = tuple<T...>;
|
||||||
|
|
||||||
|
operator json::value() const;
|
||||||
|
|
||||||
static constexpr size_t size();
|
static constexpr size_t size();
|
||||||
|
|
||||||
tuple(const json::object &);
|
tuple(const json::object &);
|
||||||
|
@ -181,6 +183,13 @@ indexof(const string_view &name)
|
||||||
return equal? i : indexof<tuple, i + 1>(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,
|
template<size_t i,
|
||||||
class tuple>
|
class tuple>
|
||||||
enable_if_tuple<tuple, tuple_value_type<tuple, i> &>
|
enable_if_tuple<tuple, tuple_value_type<tuple, i> &>
|
||||||
|
@ -801,5 +810,21 @@ operator<<(std::ostream &s, const tuple<T...> &t)
|
||||||
return s;
|
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 json
|
||||||
} // namespace ircd
|
} // namespace ircd
|
||||||
|
|
28
ircd/json.cc
28
ircd/json.cc
|
@ -24,20 +24,6 @@
|
||||||
#include <ircd/spirit.h>
|
#include <ircd/spirit.h>
|
||||||
#include <boost/fusion/include/at.hpp>
|
#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 ircd::json
|
||||||
{
|
{
|
||||||
namespace spirit = boost::spirit;
|
namespace spirit = boost::spirit;
|
||||||
|
@ -78,6 +64,20 @@ namespace ircd::json
|
||||||
struct ostreamer extern const ostreamer;
|
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>
|
template<class it>
|
||||||
struct ircd::json::input
|
struct ircd::json::input
|
||||||
:qi::grammar<it, unused_type>
|
:qi::grammar<it, unused_type>
|
||||||
|
|
Loading…
Reference in a new issue