2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-09-08 12:08:29 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_JSON_MEMBER_H
|
|
|
|
|
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
struct member;
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
size_t serialized(const member *const &begin, const member *const &end);
|
|
|
|
string_view stringify(mutable_buffer &, const member *const *const &begin, const member *const *const &end);
|
|
|
|
string_view stringify(mutable_buffer &, const member *const &begin, const member *const &end);
|
2017-09-08 12:08:29 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
/// A pair of json::value representing state for a member of an object.
|
|
|
|
///
|
|
|
|
/// This is slightly heavier than object::member as that only deals with
|
|
|
|
/// a pair of strings while the value here holds more diverse native state.
|
2017-09-12 18:37:44 +02:00
|
|
|
///
|
|
|
|
/// The key value (member.first) should always be a STRING type. We don't use
|
|
|
|
/// string_view directly in member.first because json::value can take ownership
|
|
|
|
/// of a string or use a literal depending on the circumstance and it's more
|
|
|
|
/// consistent this way.
|
|
|
|
///
|
2017-09-08 12:08:29 +02:00
|
|
|
struct ircd::json::member
|
|
|
|
:std::pair<value, value>
|
|
|
|
{
|
2017-09-11 05:31:32 +02:00
|
|
|
using std::pair<value, value>::pair;
|
2017-09-08 12:08:29 +02:00
|
|
|
template<class K, class V> member(const K &k, V&& v);
|
|
|
|
explicit member(const string_view &k);
|
|
|
|
explicit member(const object::member &m);
|
|
|
|
member() = default;
|
|
|
|
|
|
|
|
friend bool operator==(const member &a, const member &b);
|
|
|
|
friend bool operator!=(const member &a, const member &b);
|
|
|
|
friend bool operator<(const member &a, const member &b);
|
|
|
|
|
|
|
|
friend bool operator==(const member &a, const string_view &b);
|
|
|
|
friend bool operator!=(const member &a, const string_view &b);
|
|
|
|
friend bool operator<(const member &a, const string_view &b);
|
|
|
|
|
2017-10-12 03:07:47 +02:00
|
|
|
friend bool defined(const member &);
|
2017-09-08 16:47:07 +02:00
|
|
|
friend size_t serialized(const member &);
|
|
|
|
friend string_view stringify(mutable_buffer &, const member &);
|
2017-09-08 12:08:29 +02:00
|
|
|
friend std::ostream &operator<<(std::ostream &, const member &);
|
|
|
|
};
|
|
|
|
|
2017-09-12 19:02:27 +02:00
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
using members = std::initializer_list<member>;
|
|
|
|
|
|
|
|
size_t serialized(const members &);
|
|
|
|
string_view stringify(mutable_buffer &, const members &);
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:08:29 +02:00
|
|
|
template<class K,
|
|
|
|
class V>
|
|
|
|
ircd::json::member::member(const K &k,
|
|
|
|
V&& v)
|
|
|
|
:std::pair<value, value>
|
|
|
|
{
|
|
|
|
value { k }, value { std::forward<V>(v) }
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::json::member::member(const object::member &m)
|
|
|
|
:std::pair<value, value>
|
|
|
|
{
|
|
|
|
m.first, value { m.second, type(m.second) }
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::json::member::member(const string_view &k)
|
|
|
|
:std::pair<value, value>
|
|
|
|
{
|
|
|
|
k, string_view{}
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::json::operator<(const member &a, const member &b)
|
|
|
|
{
|
|
|
|
return a.first < b.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::json::operator!=(const member &a, const member &b)
|
|
|
|
{
|
|
|
|
return a.first != b.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::json::operator==(const member &a, const member &b)
|
|
|
|
{
|
|
|
|
return a.first == b.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::json::operator<(const member &a, const string_view &b)
|
|
|
|
{
|
2017-09-08 12:43:11 +02:00
|
|
|
return string_view{a.first.string, a.first.len} < b;
|
2017-09-08 12:08:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::json::operator!=(const member &a, const string_view &b)
|
|
|
|
{
|
2017-09-08 12:43:11 +02:00
|
|
|
return string_view{a.first.string, a.first.len} != b;
|
2017-09-08 12:08:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::json::operator==(const member &a, const string_view &b)
|
|
|
|
{
|
2017-09-08 12:43:11 +02:00
|
|
|
return string_view{a.first.string, a.first.len} == b;
|
2017-09-08 12:08:29 +02:00
|
|
|
}
|
2017-10-12 03:07:47 +02:00
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::json::defined(const member &a)
|
|
|
|
{
|
|
|
|
return defined(a.second);
|
|
|
|
}
|