2017-03-16 21:32:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Charybdis Development Team
|
|
|
|
* Copyright (C) 2017 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2017-08-23 23:32:28 +02:00
|
|
|
#define HAVE_IRCD_JSON_VALUE_H
|
2017-03-16 21:32:42 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
struct value;
|
|
|
|
|
2018-01-24 06:18:47 +01:00
|
|
|
using values = std::initializer_list<value>;
|
|
|
|
|
2017-10-16 06:21:56 +02:00
|
|
|
size_t serialized(const bool &);
|
2017-09-14 20:30:06 +02:00
|
|
|
size_t serialized(const value *const &begin, const value *const &end);
|
|
|
|
size_t serialized(const values &);
|
|
|
|
|
|
|
|
string_view stringify(mutable_buffer &, const value *const &begin, const value *const &end);
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// A primitive of the ircd::json system representing a value at runtime.
|
|
|
|
///
|
2017-09-14 20:30:06 +02:00
|
|
|
/// This holds state for values apropos a JSON object or array. Value's
|
|
|
|
/// data can either be in the form of a JSON string or it can be some native
|
|
|
|
/// machine state. The serial flag indicates the former.
|
2017-09-12 18:37:44 +02:00
|
|
|
///
|
|
|
|
/// Value can can hold any of the JSON types in either of these states.
|
|
|
|
/// This is accomplished with runtime switching and branching but this is
|
2017-09-14 20:30:06 +02:00
|
|
|
/// still lightweight and without a vtable pointer. The structure is just
|
|
|
|
/// the size of two pointers like a string_view; we commandeer bits of the
|
|
|
|
/// second word to hold type, flags, and length information. Thus we can hold
|
|
|
|
/// large vectors of values at 16 byte alignment and not 24 byte.
|
|
|
|
///
|
|
|
|
/// Value is capable of allocation and ownership of its internal data and copy
|
|
|
|
/// semantics. This is primarily to support recursion and various developer
|
|
|
|
/// conveniences like nested initializer_list's etc. It is better to
|
|
|
|
/// std::move() a value than copy it, but the full copy semantic is supported;
|
|
|
|
/// however, if serial=false then a copy will stringify the data into JSON and
|
|
|
|
/// the destination will be serial=true,alloc=true; thus copying of complex
|
|
|
|
/// native values never occurs.
|
|
|
|
///
|
|
|
|
/// Take careful note of a quirk with `operator string_view()`: when the
|
|
|
|
/// value is a STRING type string_view()'ing the value will never show
|
|
|
|
/// the string with surrounding quotes in view. This is because the value
|
|
|
|
/// accepts both quoted and unquoted strings as input from the developer, then
|
|
|
|
/// always serializes correctly; unquoted strings are more natural to work
|
|
|
|
/// with. This does not apply to other types like OBJECT and array as
|
|
|
|
/// string_view()'ing those when in a serial state will show surrounding '{'
|
|
|
|
/// etc.
|
2017-09-12 18:37:44 +02:00
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::json::value
|
2017-03-16 21:32:42 +01:00
|
|
|
{
|
|
|
|
union // xxx std::variant
|
|
|
|
{
|
2017-03-20 12:25:55 +01:00
|
|
|
int64_t integer;
|
|
|
|
double floating;
|
2017-03-16 21:32:42 +01:00
|
|
|
const char *string;
|
2017-09-14 20:30:06 +02:00
|
|
|
const struct value *array;
|
|
|
|
const struct member *object;
|
2017-03-16 21:32:42 +01:00
|
|
|
};
|
|
|
|
|
2017-09-12 19:02:27 +02:00
|
|
|
uint64_t len : 57; ///< length indicator
|
|
|
|
enum type type : 3; ///< json::type indicator
|
2017-09-14 20:30:06 +02:00
|
|
|
uint64_t serial : 1; ///< only *string is used. type indicates JSON
|
2017-09-12 19:02:27 +02:00
|
|
|
uint64_t alloc : 1; ///< indicates the pointer for type is owned
|
|
|
|
uint64_t floats : 1; ///< for NUMBER type, integer or floating
|
2017-03-16 21:32:42 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
using create_string_closure = std::function<void (const mutable_buffer &)>;
|
|
|
|
void create_string(const size_t &len, const create_string_closure &);
|
|
|
|
|
2017-03-18 04:32:32 +01:00
|
|
|
public:
|
2017-09-14 20:30:06 +02:00
|
|
|
bool null() const; ///< literal null or assets are really null
|
|
|
|
bool empty() const; ///< null() or assets are empty
|
2017-09-12 19:02:27 +02:00
|
|
|
bool undefined() const;
|
2017-09-14 20:30:06 +02:00
|
|
|
bool operator!() const; ///< null() or undefined() or empty() or asset Falsy
|
2017-09-12 19:02:27 +02:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
operator string_view() const; ///< NOTE unquote()'s the string value
|
2017-09-08 21:29:21 +02:00
|
|
|
explicit operator double() const;
|
|
|
|
explicit operator int64_t() const;
|
2017-09-14 20:30:06 +02:00
|
|
|
explicit operator std::string() const; ///< NOTE full stringify() of value
|
2017-03-16 21:32:42 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
template<class T> explicit value(const T &specialized);
|
2017-09-14 20:30:06 +02:00
|
|
|
value(const string_view &sv, const enum type &);
|
2017-08-23 23:32:28 +02:00
|
|
|
template<size_t N> value(const char (&)[N]);
|
2017-09-12 19:02:27 +02:00
|
|
|
value(const char *const &s);
|
2017-08-23 23:32:28 +02:00
|
|
|
value(const string_view &sv);
|
2017-09-14 20:30:06 +02:00
|
|
|
value(const struct member *const &, const size_t &len);
|
|
|
|
value(std::unique_ptr<const struct member[]>, const size_t &len); // alloc = true
|
|
|
|
value(const struct value *const &, const size_t &len);
|
|
|
|
value(std::unique_ptr<const struct value[]>, const size_t &len); // alloc = true
|
|
|
|
value(const members &); // alloc = true
|
|
|
|
value(const nullptr_t &);
|
2017-08-23 23:32:28 +02:00
|
|
|
value();
|
|
|
|
value(value &&) noexcept;
|
2017-09-12 19:02:27 +02:00
|
|
|
value(const value &);
|
2017-08-23 23:32:28 +02:00
|
|
|
value &operator=(value &&) noexcept;
|
2017-09-14 20:30:06 +02:00
|
|
|
value &operator=(const value &);
|
2017-08-23 23:32:28 +02:00
|
|
|
~value() noexcept;
|
|
|
|
|
|
|
|
friend bool operator==(const value &a, const value &b);
|
|
|
|
friend bool operator!=(const value &a, const value &b);
|
|
|
|
friend bool operator<=(const value &a, const value &b);
|
|
|
|
friend bool operator>=(const value &a, const value &b);
|
|
|
|
friend bool operator<(const value &a, const value &b);
|
|
|
|
friend bool operator>(const value &a, const value &b);
|
2017-09-08 16:47:07 +02:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
friend enum type type(const value &a);
|
2017-10-12 03:07:47 +02:00
|
|
|
friend bool defined(const value &);
|
2017-09-08 16:47:07 +02:00
|
|
|
friend size_t serialized(const value &);
|
|
|
|
friend string_view stringify(mutable_buffer &, const value &);
|
2017-08-23 23:32:28 +02:00
|
|
|
friend std::ostream &operator<<(std::ostream &, const value &);
|
2017-03-16 21:32:42 +01:00
|
|
|
};
|
2017-03-20 12:25:55 +01:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
template<> value::value(const double &floating);
|
2017-11-25 22:07:25 +01:00
|
|
|
template<> value::value(const uint64_t &integer);
|
2017-08-28 23:51:22 +02:00
|
|
|
template<> value::value(const int64_t &integer);
|
|
|
|
template<> value::value(const float &floating);
|
2017-11-30 19:34:12 +01:00
|
|
|
template<> value::value(const uint32_t &integer);
|
2017-08-28 23:51:22 +02:00
|
|
|
template<> value::value(const int32_t &integer);
|
2017-11-30 19:34:12 +01:00
|
|
|
template<> value::value(const uint16_t &integer);
|
2017-08-28 23:51:22 +02:00
|
|
|
template<> value::value(const int16_t &integer);
|
2017-08-29 02:11:48 +02:00
|
|
|
template<> value::value(const bool &boolean);
|
2017-08-28 23:51:22 +02:00
|
|
|
template<> value::value(const std::string &str);
|
|
|
|
}
|
2017-03-16 21:32:42 +01:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
static_assert(sizeof(ircd::json::value) == 16, "");
|
2017-03-16 21:32:42 +01:00
|
|
|
|
2017-04-03 05:54:35 +02:00
|
|
|
inline
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::value::value()
|
2017-09-14 20:30:06 +02:00
|
|
|
:string{nullptr}
|
2017-04-03 05:54:35 +02:00
|
|
|
,len{0}
|
|
|
|
,type{STRING}
|
2017-09-14 20:30:06 +02:00
|
|
|
,serial{false}
|
2017-04-03 05:54:35 +02:00
|
|
|
,alloc{false}
|
|
|
|
,floats{false}
|
|
|
|
{}
|
|
|
|
|
2017-03-20 12:25:55 +01:00
|
|
|
inline
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::value::value(const string_view &sv,
|
|
|
|
const enum type &type)
|
2017-03-16 21:32:42 +01:00
|
|
|
:string{sv.data()}
|
|
|
|
,len{sv.size()}
|
|
|
|
,type{type}
|
2017-09-14 20:30:06 +02:00
|
|
|
,serial{type == STRING? surrounds(sv, '"') : true}
|
|
|
|
,alloc{false}
|
|
|
|
,floats{false}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::json::value::value(const char *const &s)
|
|
|
|
:string{s}
|
|
|
|
,len{strlen(s)}
|
|
|
|
,type{json::type(s, std::nothrow)}
|
|
|
|
,serial{type == STRING? surrounds(s, '"') : true}
|
2017-03-16 21:32:42 +01:00
|
|
|
,alloc{false}
|
2017-03-20 12:25:55 +01:00
|
|
|
,floats{false}
|
|
|
|
{}
|
2017-03-16 21:32:42 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
template<size_t N>
|
|
|
|
ircd::json::value::value(const char (&str)[N])
|
|
|
|
:string{str}
|
|
|
|
,len{strnlen(str, N)}
|
|
|
|
,type{json::type(str, std::nothrow)}
|
|
|
|
,serial{type == STRING? surrounds(str, '"') : true}
|
|
|
|
,alloc{false}
|
|
|
|
,floats{false}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<> inline
|
|
|
|
ircd::json::value::value(const std::string &str)
|
|
|
|
:value{string_view{str}}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::json::value::value(const string_view &sv)
|
|
|
|
:value{sv, json::type(sv, std::nothrow)}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
ircd::json::value::value(const T &t)
|
|
|
|
:value{static_cast<string_view>(t)}
|
|
|
|
{
|
|
|
|
static_assert(std::is_base_of<ircd::string_view, T>() ||
|
|
|
|
std::is_convertible<ircd::string_view, T>(), "");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::json::value::value(const nullptr_t &)
|
|
|
|
:value
|
|
|
|
{
|
|
|
|
literal_null, type::LITERAL
|
|
|
|
}{}
|
|
|
|
|
|
|
|
template<> inline
|
|
|
|
ircd::json::value::value(const bool &boolean)
|
|
|
|
:value
|
|
|
|
{
|
|
|
|
boolean? literal_true : literal_false, type::LITERAL
|
|
|
|
}{}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::json::value::value(const struct value *const &array,
|
|
|
|
const size_t &len)
|
|
|
|
:array{array}
|
|
|
|
,len{len}
|
|
|
|
,type{ARRAY}
|
|
|
|
,serial{false}
|
|
|
|
,alloc{false}
|
|
|
|
,floats{false}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
inline
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value::value(std::unique_ptr<const struct value[]> array,
|
|
|
|
const size_t &len)
|
|
|
|
:array{array.get()}
|
|
|
|
,len{len}
|
|
|
|
,type{ARRAY}
|
|
|
|
,serial{false}
|
|
|
|
,alloc{true}
|
|
|
|
,floats{false}
|
|
|
|
{
|
|
|
|
array.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::json::value::value(const struct member *const &object,
|
|
|
|
const size_t &len)
|
2017-08-23 23:32:28 +02:00
|
|
|
:object{object}
|
2017-09-14 20:30:06 +02:00
|
|
|
,len{len}
|
2017-08-23 23:32:28 +02:00
|
|
|
,type{OBJECT}
|
|
|
|
,serial{false}
|
|
|
|
,alloc{false}
|
|
|
|
,floats{false}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value::value(std::unique_ptr<const struct member[]> object,
|
|
|
|
const size_t &len)
|
2017-08-23 23:32:28 +02:00
|
|
|
:object{object.get()}
|
2017-09-14 20:30:06 +02:00
|
|
|
,len{len}
|
2017-08-23 23:32:28 +02:00
|
|
|
,type{OBJECT}
|
|
|
|
,serial{false}
|
|
|
|
,alloc{true}
|
|
|
|
,floats{false}
|
|
|
|
{
|
|
|
|
object.release();
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:25:55 +01:00
|
|
|
template<> inline
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::value::value(const int64_t &integer)
|
2017-03-16 21:32:42 +01:00
|
|
|
:integer{integer}
|
|
|
|
,len{0}
|
|
|
|
,type{NUMBER}
|
|
|
|
,serial{false}
|
|
|
|
,alloc{false}
|
2017-03-20 12:25:55 +01:00
|
|
|
,floats{false}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<> inline
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::value::value(const double &floating)
|
2017-03-20 12:25:55 +01:00
|
|
|
:floating{floating}
|
|
|
|
,len{0}
|
|
|
|
,type{NUMBER}
|
|
|
|
,serial{false}
|
|
|
|
,alloc{false}
|
|
|
|
,floats{true}
|
|
|
|
{}
|
|
|
|
|
2017-11-25 22:07:25 +01:00
|
|
|
template<>
|
|
|
|
__attribute__((warning("uint64_t narrows to int64_t when used in json::value")))
|
|
|
|
inline
|
|
|
|
ircd::json::value::value(const uint64_t &integer)
|
|
|
|
:value{int64_t(integer)}
|
|
|
|
{}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
template<> inline
|
|
|
|
ircd::json::value::value(const float &floating)
|
|
|
|
:value{double(floating)}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<> inline
|
|
|
|
ircd::json::value::value(const int32_t &integer)
|
|
|
|
:value{int64_t(integer)}
|
|
|
|
{}
|
|
|
|
|
2017-11-30 19:34:12 +01:00
|
|
|
template<> inline
|
|
|
|
ircd::json::value::value(const uint32_t &integer)
|
|
|
|
:value{int64_t(integer)}
|
|
|
|
{}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
template<> inline
|
|
|
|
ircd::json::value::value(const int16_t &integer)
|
|
|
|
:value{int64_t(integer)}
|
|
|
|
{}
|
|
|
|
|
2017-11-30 19:34:12 +01:00
|
|
|
template<> inline
|
|
|
|
ircd::json::value::value(const uint16_t &integer)
|
|
|
|
:value{int64_t(integer)}
|
|
|
|
{}
|
|
|
|
|
2017-03-16 21:32:42 +01:00
|
|
|
inline
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::value::value(value &&other)
|
2017-03-16 21:32:42 +01:00
|
|
|
noexcept
|
2017-03-18 04:32:32 +01:00
|
|
|
:integer{other.integer}
|
2017-03-16 21:32:42 +01:00
|
|
|
,len{other.len}
|
|
|
|
,type{other.type}
|
|
|
|
,serial{other.serial}
|
|
|
|
,alloc{other.alloc}
|
2017-03-20 12:25:55 +01:00
|
|
|
,floats{other.floats}
|
2017-03-16 21:32:42 +01:00
|
|
|
{
|
|
|
|
other.alloc = false;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
inline ircd::json::value &
|
|
|
|
ircd::json::value::operator=(value &&other)
|
2017-03-18 04:32:32 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
this->~value();
|
2017-03-18 04:32:32 +01:00
|
|
|
integer = other.integer;
|
|
|
|
len = other.len;
|
|
|
|
type = other.type;
|
|
|
|
serial = other.serial;
|
|
|
|
alloc = other.alloc;
|
|
|
|
other.alloc = false;
|
2017-03-20 12:25:55 +01:00
|
|
|
floats = other.floats;
|
2017-03-18 04:32:32 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-10-12 03:07:47 +02:00
|
|
|
inline bool
|
|
|
|
ircd::json::defined(const value &a)
|
|
|
|
{
|
|
|
|
return !a.undefined();
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:25:55 +01:00
|
|
|
inline enum ircd::json::type
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::type(const value &a)
|
2017-03-16 21:32:42 +01:00
|
|
|
{
|
2017-03-20 12:25:55 +01:00
|
|
|
return a.type;
|
2017-03-16 21:32:42 +01:00
|
|
|
}
|
2017-10-16 06:21:56 +02:00
|
|
|
|
|
|
|
inline size_t
|
|
|
|
ircd::json::serialized(const bool &b)
|
|
|
|
{
|
|
|
|
constexpr const size_t t
|
|
|
|
{
|
|
|
|
strlen("true")
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr const size_t f
|
|
|
|
{
|
|
|
|
strlen("false")
|
|
|
|
};
|
|
|
|
|
|
|
|
return b? t : f;
|
|
|
|
}
|