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.
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-16 21:35:37 +01:00
|
|
|
#include <ircd/spirit.h>
|
2017-09-14 20:30:06 +02:00
|
|
|
#include <boost/fusion/include/at.hpp>
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
namespace ircd::json
|
|
|
|
{
|
2018-03-17 18:44:40 +01:00
|
|
|
using namespace ircd::spirit;
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
template<class it> struct input;
|
|
|
|
template<class it> struct output;
|
|
|
|
|
|
|
|
// Instantiations of the grammars
|
|
|
|
struct parser extern const parser;
|
|
|
|
struct printer extern const printer;
|
|
|
|
struct ostreamer extern const ostreamer;
|
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-19 11:09:19 +02:00
|
|
|
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 )
|
|
|
|
)
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
template<class it>
|
2017-09-14 20:30:06 +02:00
|
|
|
struct ircd::json::input
|
2017-03-18 04:32:32 +01:00
|
|
|
:qi::grammar<it, unused_type>
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-18 04:32:32 +01:00
|
|
|
template<class T = unused_type> using rule = qi::rule<it, T>;
|
|
|
|
|
2017-03-20 23:59:14 +01:00
|
|
|
rule<> NUL { lit('\0') ,"nul" };
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
// insignificant whitespaces
|
2017-03-20 23:59:14 +01:00
|
|
|
rule<> SP { lit('\x20') ,"space" };
|
|
|
|
rule<> HT { lit('\x09') ,"horizontal tab" };
|
|
|
|
rule<> CR { lit('\x0D') ,"carriage return" };
|
|
|
|
rule<> LF { lit('\x0A') ,"line feed" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
// whitespace skipping
|
2017-03-20 23:59:14 +01:00
|
|
|
rule<> WS { SP | HT | CR | LF ,"whitespace" };
|
|
|
|
rule<> ws { *(WS) ,"whitespace monoid" };
|
|
|
|
rule<> wsp { +(WS) ,"whitespace semigroup" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
// structural
|
2017-03-20 23:59:14 +01:00
|
|
|
rule<> object_begin { lit('{') ,"object begin" };
|
|
|
|
rule<> object_end { lit('}') ,"object end" };
|
|
|
|
rule<> array_begin { lit('[') ,"array begin" };
|
|
|
|
rule<> array_end { lit(']') ,"array end" };
|
|
|
|
rule<> name_sep { lit(':') ,"name sep" };
|
|
|
|
rule<> value_sep { lit(',') ,"value sep" };
|
2017-11-30 19:49:16 +01:00
|
|
|
rule<> escape { lit('\\') ,"escape" };
|
|
|
|
rule<> quote { lit('"') ,"quote" };
|
2017-12-25 04:21:49 +01:00
|
|
|
|
2018-01-30 22:37:13 +01:00
|
|
|
// literal
|
|
|
|
rule<> lit_false { lit("false") ,"literal false" };
|
|
|
|
rule<> lit_true { lit("true") ,"literal true" };
|
|
|
|
rule<> lit_null { lit("null") ,"null" };
|
|
|
|
rule<> boolean { lit_true | lit_false ,"boolean" };
|
|
|
|
rule<> literal { lit_true | lit_false | lit_null ,"literal" };
|
|
|
|
|
|
|
|
// numerical (TODO: exponent)
|
2018-03-28 01:15:20 +02:00
|
|
|
rule<> number
|
|
|
|
{
|
2018-08-26 00:06:17 +02:00
|
|
|
double_ | long_
|
2018-03-28 01:15:20 +02:00
|
|
|
,"number"
|
|
|
|
};
|
2018-01-30 22:37:13 +01:00
|
|
|
|
|
|
|
// string
|
2017-12-25 04:21:49 +01:00
|
|
|
rule<> unicode
|
|
|
|
{
|
2018-03-22 00:04:45 +01:00
|
|
|
lit('u') >> qi::uint_parser<uint64_t, 16, 1, 12>{}
|
2017-12-25 04:21:49 +01:00
|
|
|
,"escaped unicode"
|
|
|
|
};
|
|
|
|
|
2017-11-30 19:49:16 +01:00
|
|
|
rule<> escaped
|
|
|
|
{
|
2018-03-22 02:47:56 +01:00
|
|
|
lit('"') | lit('\\') | lit('\b') | lit('\f') | lit('\n') | lit('\r') | lit('\t') | lit('\0')
|
2017-11-30 19:49:16 +01:00
|
|
|
,"escaped"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<> escaper
|
|
|
|
{
|
2018-03-22 02:47:56 +01:00
|
|
|
lit('"') | lit('\\') | lit('b') | lit('f') | lit('n') | lit('r') | lit('t') | lit('0') | unicode
|
2017-11-30 19:49:16 +01:00
|
|
|
,"escaped"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<> escaper_nc
|
|
|
|
{
|
|
|
|
escaper | lit('/')
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<string_view> chars
|
|
|
|
{
|
2018-03-22 02:47:56 +01:00
|
|
|
raw[*((char_ - escaped) | (escape >> escaper_nc))]
|
2017-11-30 19:49:16 +01:00
|
|
|
,"characters"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<string_view> string
|
|
|
|
{
|
|
|
|
quote >> chars >> (!escape >> quote)
|
|
|
|
,"string"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-11-30 19:49:16 +01:00
|
|
|
// container
|
|
|
|
rule<string_view> name
|
|
|
|
{
|
|
|
|
string
|
|
|
|
,"name"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-18 12:39:19 +01:00
|
|
|
// recursion depth
|
|
|
|
_r1_type depth;
|
|
|
|
[[noreturn]] static void throws_exceeded();
|
|
|
|
|
|
|
|
const rule<unused_type(uint)> member
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2018-03-18 20:21:43 +01:00
|
|
|
name >> -ws >> name_sep >> -ws >> value(depth)
|
2017-09-14 20:30:06 +02:00
|
|
|
,"member"
|
2017-03-18 04:32:32 +01:00
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-18 12:39:19 +01:00
|
|
|
const rule<unused_type(uint)> object
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2018-01-18 12:39:19 +01:00
|
|
|
(eps(depth < json::object::max_recursion_depth) | eps[throws_exceeded]) >>
|
|
|
|
|
2018-03-18 20:21:43 +01:00
|
|
|
object_begin >> -((-ws >> member(depth)) % (-ws >> value_sep)) >> -ws >> object_end
|
2017-03-18 04:32:32 +01:00
|
|
|
,"object"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-18 12:39:19 +01:00
|
|
|
const rule<unused_type(uint)> array
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2018-01-18 12:39:19 +01:00
|
|
|
(eps(depth < json::array::max_recursion_depth) | eps[throws_exceeded]) >>
|
|
|
|
|
2018-03-18 20:21:43 +01:00
|
|
|
array_begin >> -((-ws >> value(depth)) % (-ws >> value_sep)) >> -ws >> array_end
|
2017-09-14 20:30:06 +02:00
|
|
|
,"array"
|
2017-03-18 04:32:32 +01:00
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-18 12:39:19 +01:00
|
|
|
const rule<unused_type(uint)> value
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2018-09-05 09:55:55 +02:00
|
|
|
lit_false | lit_null | lit_true | string | number | object(depth + 1) | array(depth + 1)
|
2017-09-14 20:30:06 +02:00
|
|
|
,"value"
|
2017-03-18 04:32:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
rule<int> type
|
|
|
|
{
|
2018-09-05 09:55:55 +02:00
|
|
|
(omit[quote] >> attr(json::STRING)) |
|
2017-03-25 00:46:15 +01:00
|
|
|
(omit[object_begin] >> attr(json::OBJECT)) |
|
|
|
|
(omit[array_begin] >> attr(json::ARRAY)) |
|
|
|
|
(omit[number >> eoi] >> attr(json::NUMBER)) |
|
|
|
|
(omit[literal >> eoi] >> attr(json::LITERAL))
|
2017-03-18 04:32:32 +01:00
|
|
|
,"type"
|
|
|
|
};
|
|
|
|
|
|
|
|
input()
|
|
|
|
:input::base_type{rule<>{}}
|
2018-01-18 12:39:19 +01:00
|
|
|
{}
|
2017-03-18 04:32:32 +01:00
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
template<class it>
|
2017-09-14 20:30:06 +02:00
|
|
|
struct ircd::json::output
|
2016-11-29 16:23:38 +01:00
|
|
|
:karma::grammar<it, unused_type>
|
|
|
|
{
|
|
|
|
template<class T = unused_type> using rule = karma::rule<it, T>;
|
|
|
|
|
|
|
|
rule<> NUL { lit('\0') ,"nul" };
|
|
|
|
|
|
|
|
// insignificant whitespaces
|
|
|
|
rule<> SP { lit('\x20') ,"space" };
|
|
|
|
rule<> HT { lit('\x09') ,"horizontal tab" };
|
|
|
|
rule<> CR { lit('\x0D') ,"carriage return" };
|
|
|
|
rule<> LF { lit('\x0A') ,"line feed" };
|
|
|
|
|
|
|
|
// whitespace skipping
|
|
|
|
rule<> WS { SP | HT | CR | LF ,"whitespace" };
|
|
|
|
rule<> ws { *(WS) ,"whitespace monoid" };
|
|
|
|
rule<> wsp { +(WS) ,"whitespace semigroup" };
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
// structural
|
2017-03-18 04:32:32 +01:00
|
|
|
rule<> object_begin { lit('{') ,"object begin" };
|
|
|
|
rule<> object_end { lit('}') ,"object end" };
|
2017-09-14 20:30:06 +02:00
|
|
|
rule<> array_begin { lit('[') ,"array begin" };
|
|
|
|
rule<> array_end { lit(']') ,"array end" };
|
2017-03-18 04:32:32 +01:00
|
|
|
rule<> name_sep { lit(':') ,"name separator" };
|
|
|
|
rule<> value_sep { lit(',') ,"value separator" };
|
|
|
|
rule<> quote { lit('"') ,"quote" };
|
2018-02-14 06:14:52 +01:00
|
|
|
rule<> escape { lit('\\') ,"escape" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
rule<string_view> lit_true { karma::string("true") ,"literal true" };
|
|
|
|
rule<string_view> lit_false { karma::string("false") ,"literal false" };
|
|
|
|
rule<string_view> lit_null { karma::string("null") ,"literal null" };
|
|
|
|
rule<string_view> boolean { lit_true | lit_false ,"boolean" };
|
|
|
|
rule<string_view> literal { lit_true | lit_false | lit_null ,"literal" };
|
2018-03-21 00:07:22 +01:00
|
|
|
|
|
|
|
rule<string_view> number
|
|
|
|
{
|
|
|
|
double_
|
|
|
|
,"number"
|
|
|
|
};
|
2018-02-14 06:14:52 +01:00
|
|
|
|
2018-03-28 01:15:20 +02:00
|
|
|
rule<string_view> quoted
|
|
|
|
{
|
|
|
|
char_('"') << *char_ << char_('"')
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<string_view> unquoted
|
|
|
|
{
|
|
|
|
quote << *char_ << quote
|
|
|
|
};
|
|
|
|
|
2018-02-14 06:14:52 +01:00
|
|
|
rule<string_view> string
|
|
|
|
{
|
2018-03-28 01:15:20 +02:00
|
|
|
quoted | unquoted
|
2018-02-14 06:14:52 +01:00
|
|
|
,"string"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<string_view> name
|
|
|
|
{
|
|
|
|
string
|
|
|
|
,"name"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-03-21 22:13:29 +01:00
|
|
|
rule<json::object> object
|
2018-03-21 00:07:22 +01:00
|
|
|
{
|
2018-03-21 22:13:29 +01:00
|
|
|
object_begin << -(member % value_sep) << object_end
|
|
|
|
,"object"
|
2018-03-21 00:07:22 +01:00
|
|
|
};
|
|
|
|
|
2018-03-21 22:13:29 +01:00
|
|
|
rule<json::array> array
|
2018-03-21 00:07:22 +01:00
|
|
|
{
|
2018-03-21 22:13:29 +01:00
|
|
|
array_begin << -(value % value_sep) << array_end
|
|
|
|
,"array"
|
2018-03-21 00:07:22 +01:00
|
|
|
};
|
|
|
|
|
2018-03-21 22:13:29 +01:00
|
|
|
rule<json::object::member> member
|
2018-03-21 00:07:22 +01:00
|
|
|
{
|
2018-03-21 22:13:29 +01:00
|
|
|
name << name_sep << value
|
|
|
|
,"member"
|
2018-03-21 00:07:22 +01:00
|
|
|
};
|
|
|
|
|
2018-03-21 22:13:29 +01:00
|
|
|
rule<string_view> value
|
2018-03-21 00:07:22 +01:00
|
|
|
{
|
2018-03-21 22:13:29 +01:00
|
|
|
(&object << object)
|
|
|
|
| (&array << array)
|
|
|
|
| (&literal << literal)
|
|
|
|
| (&number << number)
|
2018-03-28 01:15:20 +02:00
|
|
|
| string
|
2018-03-21 22:13:29 +01:00
|
|
|
,"value"
|
2018-03-21 00:07:22 +01:00
|
|
|
};
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
output()
|
|
|
|
:output::base_type{rule<>{}}
|
2017-03-11 04:31:20 +01:00
|
|
|
{}
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-11-06 21:07:57 +01:00
|
|
|
struct ircd::json::expectation_failure
|
|
|
|
:parse_error
|
|
|
|
{
|
|
|
|
expectation_failure(const char *const &start,
|
|
|
|
const qi::expectation_failure<const char *> &e,
|
|
|
|
const ssize_t &show_max = 64)
|
|
|
|
:parse_error
|
|
|
|
{
|
|
|
|
"Expected %s. You input %zd invalid characters at position %zd: %s",
|
|
|
|
ircd::string(e.what_),
|
|
|
|
std::distance(e.first, e.last),
|
|
|
|
std::distance(start, e.first),
|
|
|
|
string_view{e.first, e.first + std::min(std::distance(e.first, e.last), show_max)}
|
|
|
|
}{}
|
|
|
|
};
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
struct ircd::json::parser
|
2016-11-29 16:23:38 +01:00
|
|
|
:input<const char *>
|
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
using input::input;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
const ircd::json::parser;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
struct ircd::json::printer
|
2016-11-29 16:23:38 +01:00
|
|
|
:output<char *>
|
|
|
|
{
|
2018-02-18 23:15:24 +01:00
|
|
|
template<class gen,
|
|
|
|
class... attr>
|
|
|
|
bool operator()(mutable_buffer &out, gen&&, attr&&...) const;
|
2017-03-31 00:55:17 +02:00
|
|
|
|
2018-02-18 23:15:24 +01:00
|
|
|
template<class gen>
|
|
|
|
bool operator()(mutable_buffer &out, gen&&) const;
|
2017-09-16 20:58:27 +02:00
|
|
|
|
|
|
|
template<class it_a,
|
|
|
|
class it_b,
|
|
|
|
class closure>
|
|
|
|
static void list_protocol(mutable_buffer &, it_a begin, const it_b &end, closure&&);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
const ircd::json::printer;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
struct ircd::json::ostreamer
|
2016-11-29 16:23:38 +01:00
|
|
|
:output<karma::ostream_iterator<char>>
|
|
|
|
{
|
2017-03-11 04:31:20 +01:00
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
const ircd::json::ostreamer;
|
2017-03-11 04:31:20 +01:00
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
template<class gen,
|
2018-02-18 23:15:24 +01:00
|
|
|
class... attr>
|
2017-03-31 00:55:17 +02:00
|
|
|
bool
|
2018-02-18 23:15:24 +01:00
|
|
|
ircd::json::printer::operator()(mutable_buffer &out,
|
2017-03-31 00:55:17 +02:00
|
|
|
gen&& g,
|
2018-02-18 23:15:24 +01:00
|
|
|
attr&&... a)
|
2017-03-31 00:55:17 +02:00
|
|
|
const
|
2017-03-11 04:31:20 +01:00
|
|
|
{
|
2018-02-18 23:15:24 +01:00
|
|
|
const auto maxwidth
|
|
|
|
{
|
|
|
|
karma::maxwidth(size(out))
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto gg
|
|
|
|
{
|
|
|
|
maxwidth[std::forward<gen>(g)]
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto throws{[&out]
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2017-12-25 04:21:49 +01:00
|
|
|
throw print_error
|
|
|
|
{
|
2018-02-18 23:15:24 +01:00
|
|
|
"Failed to print attributes '%s' generator '%s' (%zd bytes in buffer)",
|
|
|
|
demangle<decltype(a)...>(),
|
2017-12-25 04:21:49 +01:00
|
|
|
demangle<decltype(g)>(),
|
2018-02-18 23:15:24 +01:00
|
|
|
size(out)
|
2017-12-25 04:21:49 +01:00
|
|
|
};
|
|
|
|
}};
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2018-02-18 23:15:24 +01:00
|
|
|
return karma::generate(begin(out), gg | eps[throws], std::forward<attr>(a)...);
|
2017-03-31 00:55:17 +02:00
|
|
|
}
|
2017-03-16 21:36:18 +01:00
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
template<class gen>
|
|
|
|
bool
|
2018-02-18 23:15:24 +01:00
|
|
|
ircd::json::printer::operator()(mutable_buffer &out,
|
2017-03-31 00:55:17 +02:00
|
|
|
gen&& g)
|
|
|
|
const
|
|
|
|
{
|
2018-02-18 23:15:24 +01:00
|
|
|
const auto maxwidth
|
|
|
|
{
|
|
|
|
karma::maxwidth(size(out))
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto gg
|
|
|
|
{
|
|
|
|
maxwidth[std::forward<gen>(g)]
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto throws{[&out]
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2017-12-25 04:21:49 +01:00
|
|
|
throw print_error
|
|
|
|
{
|
|
|
|
"Failed to print generator '%s' (%zd bytes in buffer)",
|
|
|
|
demangle<decltype(g)>(),
|
2018-02-18 23:15:24 +01:00
|
|
|
size(out)
|
2017-12-25 04:21:49 +01:00
|
|
|
};
|
|
|
|
}};
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2018-02-18 23:15:24 +01:00
|
|
|
return karma::generate(begin(out), gg | eps[throws]);
|
2017-03-11 04:31:20 +01:00
|
|
|
}
|
|
|
|
|
2017-09-16 20:58:27 +02:00
|
|
|
template<class it_a,
|
|
|
|
class it_b,
|
|
|
|
class closure>
|
|
|
|
void
|
|
|
|
ircd::json::printer::list_protocol(mutable_buffer &buffer,
|
|
|
|
it_a it,
|
|
|
|
const it_b &end,
|
|
|
|
closure&& lambda)
|
|
|
|
{
|
|
|
|
if(it != end)
|
|
|
|
{
|
|
|
|
lambda(buffer, *it);
|
|
|
|
for(++it; it != end; ++it)
|
|
|
|
{
|
|
|
|
json::printer(buffer, json::printer.value_sep);
|
|
|
|
lambda(buffer, *it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 12:39:19 +01:00
|
|
|
template<class it>
|
|
|
|
void
|
|
|
|
ircd::json::input<it>::throws_exceeded()
|
|
|
|
{
|
|
|
|
throw recursion_limit
|
|
|
|
{
|
|
|
|
"Maximum recursion depth exceeded"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// stack.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::json::stack::stack(const mutable_buffer &buf,
|
2018-05-08 08:48:31 +02:00
|
|
|
flush_callback flusher,
|
|
|
|
const size_t &hiwat,
|
|
|
|
const size_t &lowat)
|
2018-03-18 00:55:32 +01:00
|
|
|
:buf{buf}
|
|
|
|
,flusher{std::move(flusher)}
|
2018-05-08 08:48:31 +02:00
|
|
|
,hiwat{hiwat}
|
|
|
|
,lowat{lowat}
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-04-10 22:33:26 +02:00
|
|
|
ircd::json::stack::stack(stack &&other)
|
|
|
|
noexcept
|
|
|
|
:buf{std::move(other.buf)}
|
|
|
|
,flusher{std::move(other.flusher)}
|
2018-04-14 00:15:45 +02:00
|
|
|
,eptr{std::move(other.eptr)}
|
2018-05-08 08:48:31 +02:00
|
|
|
,hiwat{std::move(other.hiwat)}
|
|
|
|
,lowat{std::move(other.lowat)}
|
2018-04-10 22:33:26 +02:00
|
|
|
,co{std::move(other.co)}
|
|
|
|
,ca{std::move(other.ca)}
|
|
|
|
{
|
|
|
|
other.co = nullptr;
|
|
|
|
other.ca = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::~stack()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
assert(clean() || done());
|
|
|
|
}
|
|
|
|
|
2018-04-11 02:13:40 +02:00
|
|
|
void
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::append(const string_view &s)
|
|
|
|
{
|
2018-04-11 02:13:40 +02:00
|
|
|
append(size(s), [&s](const mutable_buffer &buf)
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
return copy(buf, s);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-11 02:13:40 +02:00
|
|
|
void
|
2018-04-11 01:59:08 +02:00
|
|
|
ircd::json::stack::append(const size_t &expect,
|
|
|
|
const window_buffer::closure &closure)
|
2018-04-14 00:15:45 +02:00
|
|
|
try
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
2018-04-14 00:15:45 +02:00
|
|
|
if(unlikely(eptr))
|
|
|
|
return;
|
|
|
|
|
2018-04-11 02:13:40 +02:00
|
|
|
if(expect > buf.remaining())
|
|
|
|
{
|
|
|
|
if(unlikely(!flusher)) throw print_error
|
|
|
|
{
|
|
|
|
"Insufficient buffer. I need %zu more bytes; you only have %zu left (of %zu).",
|
|
|
|
expect,
|
|
|
|
buf.remaining(),
|
|
|
|
size(buf.base)
|
|
|
|
};
|
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
if(!flush(true))
|
2018-04-14 00:15:45 +02:00
|
|
|
return;
|
|
|
|
|
2018-04-11 02:13:40 +02:00
|
|
|
if(unlikely(expect > buf.remaining())) throw print_error
|
|
|
|
{
|
|
|
|
"Insufficient flush. I still need %zu more bytes to buffer.",
|
|
|
|
expect - buf.remaining()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
buf([&closure](const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return closure(buf);
|
|
|
|
});
|
|
|
|
|
2018-04-11 02:13:40 +02:00
|
|
|
if(!buf.remaining())
|
2018-05-08 08:48:31 +02:00
|
|
|
flush(true);
|
|
|
|
else if(buf.consumed() >= hiwat)
|
2018-04-11 02:13:40 +02:00
|
|
|
flush();
|
|
|
|
}
|
2018-04-14 00:15:45 +02:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
assert(!this->eptr);
|
|
|
|
this->eptr = std::current_exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::stack::rethrow_exception()
|
|
|
|
{
|
|
|
|
if(unlikely(eptr))
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
}
|
2018-04-11 02:13:40 +02:00
|
|
|
|
|
|
|
bool
|
2018-05-08 08:48:31 +02:00
|
|
|
ircd::json::stack::flush(const bool &force)
|
2018-04-14 00:15:45 +02:00
|
|
|
try
|
2018-04-11 02:13:40 +02:00
|
|
|
{
|
|
|
|
if(!flusher)
|
|
|
|
return false;
|
|
|
|
|
2018-04-14 00:15:45 +02:00
|
|
|
if(unlikely(eptr))
|
|
|
|
return false;
|
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
if(!force && buf.consumed() < lowat)
|
|
|
|
return false;
|
|
|
|
|
2018-04-11 02:13:40 +02:00
|
|
|
// The user returns the portion of the buffer they were able to flush
|
|
|
|
// rather than forcing them to wait on their sink to flush the whole
|
|
|
|
// thing, they can continue with us for a little while more.
|
|
|
|
const const_buffer flushed
|
|
|
|
{
|
|
|
|
flusher(buf.completed())
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(data(flushed) == data(buf.completed())); // Can only flush front sry
|
|
|
|
buf.shift(size(flushed));
|
|
|
|
return true;
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
2018-04-14 00:15:45 +02:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
assert(!this->eptr);
|
|
|
|
this->eptr = std::current_exception();
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-18 00:55:32 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::stack::clear()
|
|
|
|
{
|
|
|
|
buf.rewind(buf.consumed());
|
2018-04-14 00:15:45 +02:00
|
|
|
this->eptr = std::exception_ptr{};
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::json::stack::completed()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return buf.completed();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::stack::remaining()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return buf.remaining();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::stack::done()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return closed() && buf.consumed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::stack::clean()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return closed() && !buf.consumed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::stack::closed()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !opened();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::stack::opened()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return co || ca;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// object
|
|
|
|
//
|
|
|
|
|
2018-04-10 22:33:26 +02:00
|
|
|
ircd::json::stack::object::object(object &&other)
|
|
|
|
noexcept
|
|
|
|
:s{std::move(other.s)}
|
|
|
|
,pm{std::move(other.pm)}
|
|
|
|
,pa{std::move(other.pa)}
|
|
|
|
,cm{std::move(other.cm)}
|
|
|
|
,mc{std::move(other.mc)}
|
|
|
|
{
|
|
|
|
other.s = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::object::object(stack &s)
|
|
|
|
:s{&s}
|
|
|
|
{
|
|
|
|
assert(s.clean());
|
|
|
|
s.co = this;
|
2018-04-11 01:59:08 +02:00
|
|
|
s.append("{"_sv);
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::object::object(member &pm)
|
|
|
|
:s{pm.s}
|
|
|
|
,pm{&pm}
|
|
|
|
{
|
|
|
|
assert(s->opened());
|
2018-04-14 00:15:45 +02:00
|
|
|
s->rethrow_exception();
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(pm.co == nullptr);
|
|
|
|
assert(pm.ca == nullptr);
|
|
|
|
pm.co = this;
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append("{"_sv);
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::object::object(array &pa)
|
|
|
|
:s{pa.s}
|
|
|
|
,pa{&pa}
|
|
|
|
{
|
|
|
|
assert(s->opened());
|
2018-04-14 00:15:45 +02:00
|
|
|
s->rethrow_exception();
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(pa.co == nullptr);
|
|
|
|
assert(pa.ca == nullptr);
|
|
|
|
pa.co = this;
|
|
|
|
|
|
|
|
if(pa.vc)
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append(","_sv);
|
2018-03-18 00:55:32 +01:00
|
|
|
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append("{"_sv);
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::object::~object()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-04-10 22:33:26 +02:00
|
|
|
if(!s)
|
|
|
|
return; // std::move()'ed away
|
|
|
|
|
2018-04-10 23:17:56 +02:00
|
|
|
const unwind _{[this]
|
|
|
|
{
|
|
|
|
// Allows ~dtor to be called to close the JSON manually
|
|
|
|
s = nullptr;
|
|
|
|
}};
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(cm == nullptr);
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append("}"_sv);
|
2018-03-18 00:55:32 +01:00
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
if(pm) // branch taken if member of object
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
assert(pa == nullptr);
|
|
|
|
assert(pm->ca == nullptr);
|
|
|
|
assert(pm->co == this);
|
2018-04-11 00:11:44 +02:00
|
|
|
pm->co = nullptr;
|
2018-03-18 00:55:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
if(pa) // branch taken if value in array
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
assert(pm == nullptr);
|
2018-04-04 00:58:47 +02:00
|
|
|
assert(pa->ca == nullptr);
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(pa->co == this);
|
|
|
|
pa->vc++;
|
2018-04-04 00:58:47 +02:00
|
|
|
pa->co = nullptr;
|
2018-03-18 00:55:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
// branch taken if top of stack:: (w/ final flush)
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(s->co == this);
|
|
|
|
assert(s->ca == nullptr);
|
|
|
|
assert(pm == nullptr && pa == nullptr);
|
|
|
|
s->co = nullptr;
|
|
|
|
assert(s->done());
|
2018-05-08 08:48:31 +02:00
|
|
|
s->flush(true);
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// array
|
|
|
|
//
|
|
|
|
|
2018-04-10 22:33:26 +02:00
|
|
|
ircd::json::stack::array::array(array &&other)
|
|
|
|
noexcept
|
|
|
|
:s{std::move(other.s)}
|
|
|
|
,pm{std::move(other.pm)}
|
|
|
|
,pa{std::move(other.pa)}
|
|
|
|
,co{std::move(other.co)}
|
|
|
|
,ca{std::move(other.ca)}
|
|
|
|
,vc{std::move(other.vc)}
|
|
|
|
{
|
|
|
|
other.s = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::array::array(stack &s)
|
|
|
|
:s{&s}
|
|
|
|
{
|
|
|
|
assert(s.clean());
|
|
|
|
s.ca = this;
|
2018-04-11 01:59:08 +02:00
|
|
|
s.append("["_sv);
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::array::array(member &pm)
|
|
|
|
:s{pm.s}
|
|
|
|
,pm{&pm}
|
|
|
|
{
|
|
|
|
assert(s->opened());
|
2018-04-14 00:15:45 +02:00
|
|
|
s->rethrow_exception();
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(pm.co == nullptr);
|
|
|
|
assert(pm.ca == nullptr);
|
|
|
|
pm.ca = this;
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append("["_sv);
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::array::array(array &pa)
|
|
|
|
:s{pa.s}
|
|
|
|
,pa{&pa}
|
|
|
|
{
|
|
|
|
assert(s->opened());
|
2018-04-14 00:15:45 +02:00
|
|
|
s->rethrow_exception();
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(pa.co == nullptr);
|
|
|
|
assert(pa.ca == nullptr);
|
|
|
|
pa.ca = this;
|
|
|
|
|
|
|
|
if(pa.vc)
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append(","_sv);
|
2018-03-18 00:55:32 +01:00
|
|
|
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append("["_sv);
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::array::~array()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-04-10 22:33:26 +02:00
|
|
|
if(!s)
|
|
|
|
return; // std::move()'ed away
|
|
|
|
|
2018-04-10 23:17:56 +02:00
|
|
|
const unwind _{[this]
|
|
|
|
{
|
|
|
|
// Allows ~dtor to be called to close the JSON manually
|
|
|
|
s = nullptr;
|
|
|
|
}};
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(co == nullptr);
|
|
|
|
assert(ca == nullptr);
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append("]"_sv);
|
2018-03-18 00:55:32 +01:00
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
if(pm) // branch taken if member of object
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
assert(pa == nullptr);
|
|
|
|
assert(pm->ca == this);
|
|
|
|
assert(pm->co == nullptr);
|
|
|
|
pm->ca = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
if(pa) // branch taken if value in array
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
assert(pm == nullptr);
|
|
|
|
assert(pa->ca == this);
|
|
|
|
assert(pa->co == nullptr);
|
|
|
|
pa->vc++;
|
|
|
|
pa->ca = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
// branch taken if top of stack:: (w/ final flush)
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(s->ca == this);
|
|
|
|
assert(s->co == nullptr);
|
|
|
|
assert(pm == nullptr && pa == nullptr);
|
|
|
|
s->ca = nullptr;
|
|
|
|
assert(s->done());
|
2018-05-08 08:48:31 +02:00
|
|
|
s->flush(true);
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::stack::array::append(const json::value &value)
|
|
|
|
{
|
|
|
|
assert(s);
|
|
|
|
_pre_append();
|
|
|
|
const unwind post{[this]
|
|
|
|
{
|
|
|
|
_post_append();
|
|
|
|
}};
|
|
|
|
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append(serialized(value), [&value]
|
|
|
|
(mutable_buffer buf)
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
return size(stringify(buf, value));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::stack::array::_pre_append()
|
|
|
|
{
|
|
|
|
if(vc)
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append(","_sv);
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::stack::array::_post_append()
|
|
|
|
{
|
|
|
|
++vc;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// member
|
|
|
|
//
|
|
|
|
|
2018-04-10 22:33:26 +02:00
|
|
|
ircd::json::stack::member::member(member &&other)
|
|
|
|
noexcept
|
|
|
|
:s{std::move(other.s)}
|
|
|
|
,po{std::move(other.po)}
|
|
|
|
,name{std::move(other.name)}
|
|
|
|
,co{std::move(other.co)}
|
|
|
|
,ca{std::move(other.ca)}
|
|
|
|
{
|
|
|
|
other.s = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::member::member(object &po,
|
|
|
|
const string_view &name)
|
|
|
|
:s{po.s}
|
|
|
|
,po{&po}
|
|
|
|
,name{name}
|
|
|
|
{
|
2018-04-14 00:15:45 +02:00
|
|
|
assert(s->opened());
|
|
|
|
s->rethrow_exception();
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(po.cm == nullptr);
|
|
|
|
po.cm = this;
|
|
|
|
|
|
|
|
if(po.mc)
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append(","_sv);
|
|
|
|
|
|
|
|
thread_local char tmp[2048];
|
|
|
|
mutable_buffer buf{tmp};
|
|
|
|
if(!printer(buf, json::printer.name << json::printer.name_sep, name))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"member name overflow: max size is under %zu", sizeof(tmp)
|
|
|
|
};
|
2018-03-18 00:55:32 +01:00
|
|
|
|
2018-04-11 01:59:08 +02:00
|
|
|
assert(data(buf) >= tmp);
|
|
|
|
s->append(string_view{tmp, size_t(data(buf) - tmp)});
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::member::member(object &po,
|
|
|
|
const string_view &name,
|
|
|
|
const json::value &value)
|
|
|
|
:member{po, name}
|
|
|
|
{
|
|
|
|
append(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::member::~member()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-04-10 22:33:26 +02:00
|
|
|
if(!s)
|
|
|
|
return; // std::move()'ed away
|
|
|
|
|
2018-04-10 23:17:56 +02:00
|
|
|
const unwind _{[this]
|
|
|
|
{
|
|
|
|
// Allows ~dtor to be called to close the JSON manually
|
|
|
|
s = nullptr;
|
|
|
|
}};
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(co == nullptr);
|
|
|
|
assert(ca == nullptr);
|
|
|
|
assert(po);
|
|
|
|
assert(po->cm == this);
|
|
|
|
po->mc++;
|
|
|
|
po->cm = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::stack::member::append(const json::value &value)
|
|
|
|
{
|
|
|
|
assert(s);
|
|
|
|
_pre_append();
|
|
|
|
const unwind post{[this]
|
|
|
|
{
|
|
|
|
_post_append();
|
|
|
|
}};
|
|
|
|
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append(serialized(value), [&value]
|
|
|
|
(mutable_buffer buf)
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
return size(stringify(buf, value));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::stack::member::_pre_append()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::stack::member::_post_append()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// iov.h
|
|
|
|
//
|
|
|
|
|
2018-03-17 19:52:28 +01:00
|
|
|
decltype(ircd::json::iov::MAX_SIZE)
|
|
|
|
ircd::json::iov::MAX_SIZE
|
|
|
|
{
|
|
|
|
1024
|
|
|
|
};
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::json::operator<<(std::ostream &s, const iov &iov)
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
s << json::strung(iov);
|
2017-09-14 20:30:06 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
ircd::string_view
|
2018-02-18 23:47:19 +01:00
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
2017-09-14 20:30:06 +02:00
|
|
|
const iov &iov)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2018-03-17 19:52:28 +01:00
|
|
|
const ctx::critical_assertion ca;
|
|
|
|
thread_local const member *m[iov.MAX_SIZE];
|
|
|
|
if(unlikely(size_t(iov.size()) > iov.MAX_SIZE))
|
|
|
|
throw iov::oversize
|
|
|
|
{
|
|
|
|
"IOV has %zd members but maximum is %zu", iov.size(), iov.MAX_SIZE
|
|
|
|
};
|
|
|
|
|
2017-10-05 01:30:09 +02:00
|
|
|
std::transform(std::begin(iov), std::end(iov), m, []
|
|
|
|
(const member &m)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-10-05 01:30:09 +02:00
|
|
|
return &m;
|
2017-09-08 10:41:02 +02:00
|
|
|
});
|
|
|
|
|
2017-10-16 06:22:18 +02:00
|
|
|
std::sort(m, m + iov.size(), []
|
|
|
|
(const member *const &a, const member *const &b)
|
|
|
|
{
|
|
|
|
return *a < *b;
|
|
|
|
});
|
|
|
|
|
2018-02-19 00:20:43 +01:00
|
|
|
static const auto print_member
|
2018-02-18 23:47:19 +01:00
|
|
|
{
|
|
|
|
[](mutable_buffer &buf, const member *const &m)
|
|
|
|
{
|
|
|
|
printer(buf, printer.name << printer.name_sep, m->first);
|
|
|
|
stringify(buf, m->second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
char *const start{begin(buf)};
|
|
|
|
printer(buf, printer.object_begin);
|
|
|
|
printer::list_protocol(buf, m, m + iov.size(), print_member);
|
|
|
|
printer(buf, printer.object_end);
|
2018-03-28 01:45:56 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(serialized(iov) == size(ret));
|
|
|
|
return ret;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const iov &iov)
|
|
|
|
{
|
|
|
|
const size_t ret
|
|
|
|
{
|
|
|
|
1U + iov.empty()
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::accumulate(std::begin(iov), std::end(iov), ret, []
|
|
|
|
(auto ret, const auto &member)
|
|
|
|
{
|
|
|
|
return ret += serialized(member) + 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-26 02:45:49 +02:00
|
|
|
ircd::json::value &
|
|
|
|
ircd::json::iov::at(const string_view &key)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-05-26 02:45:49 +02:00
|
|
|
const auto it
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-05-26 02:45:49 +02:00
|
|
|
std::find_if(std::begin(*this), std::end(*this), [&key]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
return string_view{member.first} == key;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == std::end(*this))
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"key '%s' not found", key
|
|
|
|
};
|
|
|
|
|
|
|
|
return it->second;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::json::value &
|
|
|
|
ircd::json::iov::at(const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
2018-01-22 23:55:00 +01:00
|
|
|
const auto it
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-01-22 23:55:00 +01:00
|
|
|
std::find_if(std::begin(*this), std::end(*this), [&key]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
return string_view{member.first} == key;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == std::end(*this))
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"key '%s' not found", key
|
|
|
|
};
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2018-05-26 02:45:49 +02:00
|
|
|
bool
|
|
|
|
ircd::json::iov::has(const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::any_of(std::begin(*this), std::end(*this), [&key]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
return string_view{member.first} == key;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
ircd::json::iov::push::push(iov &iov,
|
|
|
|
member member)
|
|
|
|
:node
|
|
|
|
{
|
|
|
|
iov, std::move(member)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::iov::push::push(iov &iov,
|
|
|
|
const bool &b,
|
|
|
|
const conditional_member &cp)
|
|
|
|
:node
|
|
|
|
{
|
|
|
|
b?
|
|
|
|
&iov:
|
|
|
|
nullptr,
|
|
|
|
|
|
|
|
b?
|
|
|
|
member{cp.first, cp.second()}:
|
|
|
|
member{}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::iov::add::add(iov &iov,
|
|
|
|
member member)
|
|
|
|
:node
|
|
|
|
{
|
|
|
|
iov, [&iov, &member]
|
|
|
|
{
|
|
|
|
if(iov.has(member.first))
|
2018-06-05 20:16:52 +02:00
|
|
|
throw exists
|
|
|
|
{
|
|
|
|
"failed to add member '%s': already exists",
|
|
|
|
string_view{member.first}
|
|
|
|
};
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
return std::move(member);
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
ircd::json::iov::add::add(iov &iov,
|
|
|
|
const bool &b,
|
|
|
|
const conditional_member &cp)
|
2017-09-14 20:30:06 +02:00
|
|
|
:node
|
|
|
|
{
|
2018-06-05 20:16:52 +02:00
|
|
|
b?
|
|
|
|
&iov:
|
|
|
|
nullptr,
|
2017-09-08 10:41:02 +02:00
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
[&iov, &b, &cp]
|
|
|
|
{
|
|
|
|
if(!b)
|
|
|
|
return member{};
|
|
|
|
|
|
|
|
if(iov.has(cp.first))
|
|
|
|
throw exists
|
|
|
|
{
|
|
|
|
"failed to add member '%s': already exists",
|
|
|
|
string_view{cp.first}
|
|
|
|
};
|
|
|
|
|
|
|
|
return member
|
|
|
|
{
|
|
|
|
cp.first, cp.second()
|
|
|
|
};
|
|
|
|
}()
|
2018-06-05 08:17:29 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
ircd::json::iov::set::set(iov &iov,
|
|
|
|
member member)
|
2017-09-14 20:30:06 +02:00
|
|
|
:node
|
|
|
|
{
|
|
|
|
iov, [&iov, &member]
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
iov.remove_if([&member](const auto &existing)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
return string_view{existing.first} == string_view{member.first};
|
|
|
|
});
|
2017-09-08 10:41:02 +02:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
return std::move(member);
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
{
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
ircd::json::iov::set::set(iov &iov,
|
|
|
|
const bool &b,
|
|
|
|
const conditional_member &cp)
|
2017-09-14 20:30:06 +02:00
|
|
|
:node
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2018-06-05 20:16:52 +02:00
|
|
|
b?
|
|
|
|
&iov:
|
|
|
|
nullptr,
|
|
|
|
|
|
|
|
[&iov, &b, &cp]
|
|
|
|
{
|
|
|
|
if(!b)
|
|
|
|
return member{};
|
|
|
|
|
|
|
|
iov.remove_if([&cp](const auto &existing)
|
|
|
|
{
|
|
|
|
return string_view{existing.first} == cp.first;
|
|
|
|
});
|
|
|
|
|
|
|
|
return member
|
|
|
|
{
|
|
|
|
cp.first, cp.second()
|
|
|
|
};
|
|
|
|
}()
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 18:29:34 +02:00
|
|
|
ircd::json::iov::defaults::defaults(iov &iov,
|
|
|
|
member member)
|
|
|
|
:node
|
|
|
|
{
|
2018-06-05 20:16:52 +02:00
|
|
|
!iov.has(member.first)?
|
|
|
|
&iov:
|
|
|
|
nullptr,
|
|
|
|
|
|
|
|
std::move(member)
|
2017-10-25 18:29:34 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
ircd::json::iov::defaults::defaults(iov &iov,
|
|
|
|
bool b,
|
|
|
|
const conditional_member &cp)
|
2017-10-25 18:29:34 +02:00
|
|
|
:node
|
|
|
|
{
|
2018-06-05 20:16:52 +02:00
|
|
|
[&iov, &b, &cp]() -> json::iov *
|
|
|
|
{
|
|
|
|
if(!b)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if(!iov.has(cp.first))
|
|
|
|
return &iov;
|
|
|
|
|
|
|
|
b = false;
|
|
|
|
return nullptr;
|
|
|
|
}(),
|
|
|
|
|
|
|
|
[&iov, &b, &cp]
|
|
|
|
{
|
|
|
|
if(!b)
|
|
|
|
return member{};
|
|
|
|
|
|
|
|
return member
|
|
|
|
{
|
|
|
|
cp.first, cp.second()
|
|
|
|
};
|
|
|
|
}()
|
2017-10-25 18:29:34 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-09-06 03:33:46 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/strung.h
|
|
|
|
//
|
|
|
|
|
2018-09-06 04:21:59 +02:00
|
|
|
ircd::json::strung
|
|
|
|
ircd::json::insert(const strung &s,
|
|
|
|
const json::member &m)
|
|
|
|
{
|
|
|
|
if(!empty(s) && type(s) != type::OBJECT)
|
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Cannot insert member into JSON of type %s",
|
|
|
|
reflect(type(s))
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t mctr {0};
|
|
|
|
thread_local std::array<member, iov::MAX_SIZE> mb;
|
|
|
|
for(const object::member &m : object{s})
|
|
|
|
mb.at(mctr++) = member{m};
|
|
|
|
|
|
|
|
mb.at(mctr++) = m;
|
|
|
|
return strung
|
|
|
|
{
|
|
|
|
mb.data(), mb.data() + mctr
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::strung
|
|
|
|
ircd::json::remove(const strung &s,
|
|
|
|
const string_view &key)
|
|
|
|
{
|
|
|
|
if(empty(s))
|
|
|
|
return s;
|
|
|
|
|
|
|
|
if(type(s) != type::OBJECT)
|
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Cannot remove object member '%s' from JSON of type %s",
|
|
|
|
key,
|
|
|
|
reflect(type(s))
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t mctr {0};
|
|
|
|
thread_local std::array<object::member, iov::MAX_SIZE> mb;
|
|
|
|
for(const object::member &m : object{s})
|
|
|
|
if(m.first != key)
|
|
|
|
mb.at(mctr++) = m;
|
|
|
|
|
|
|
|
return strung
|
|
|
|
{
|
|
|
|
mb.data(), mb.data() + mctr
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::strung
|
|
|
|
ircd::json::remove(const strung &s,
|
|
|
|
const size_t &idx)
|
|
|
|
{
|
|
|
|
if(empty(s))
|
|
|
|
return s;
|
|
|
|
|
|
|
|
if(type(s) != type::ARRAY)
|
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Cannot remove array element [%zu] from JSON of type %s",
|
|
|
|
idx,
|
|
|
|
reflect(type(s))
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t mctr{0}, i{0};
|
|
|
|
thread_local std::array<string_view, iov::MAX_SIZE> mb;
|
|
|
|
for(const string_view &m : array{s})
|
|
|
|
if(i++ != idx)
|
|
|
|
mb.at(mctr++) = m;
|
|
|
|
|
|
|
|
return strung
|
|
|
|
{
|
|
|
|
mb.data(), mb.data() + mctr
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-06 03:33:46 +02:00
|
|
|
ircd::json::strung::operator
|
|
|
|
json::array()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return string_view{*this};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::strung::operator
|
|
|
|
json::object()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return string_view{*this};
|
|
|
|
}
|
|
|
|
|
2017-11-06 21:08:32 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/vector.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::json::vector::const_iterator &
|
|
|
|
ircd::json::vector::const_iterator::operator++()
|
|
|
|
try
|
|
|
|
{
|
2018-03-21 23:12:29 +01:00
|
|
|
static const parser::rule<string_view> parse_next
|
2017-11-06 21:08:32 +01:00
|
|
|
{
|
2018-01-18 12:39:19 +01:00
|
|
|
raw[parser.object(0)] | qi::eoi
|
2017-11-06 21:08:32 +01:00
|
|
|
,"next vector element or end"
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view state;
|
|
|
|
qi::parse(start, stop, eps > parse_next, state);
|
|
|
|
this->state = state;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
|
|
|
throw expectation_failure(start, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::vector::const_iterator
|
|
|
|
ircd::json::vector::begin()
|
|
|
|
const try
|
|
|
|
{
|
2018-03-21 23:12:29 +01:00
|
|
|
static const parser::rule<string_view> parse_begin
|
2017-11-06 21:08:32 +01:00
|
|
|
{
|
2018-01-18 12:39:19 +01:00
|
|
|
raw[parser.object(0)]
|
2017-11-06 21:08:32 +01:00
|
|
|
,"object vector element"
|
|
|
|
};
|
|
|
|
|
|
|
|
const_iterator ret
|
|
|
|
{
|
|
|
|
string_view::begin(), string_view::end()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!string_view{*this}.empty())
|
|
|
|
{
|
|
|
|
string_view state;
|
|
|
|
qi::parse(ret.start, ret.stop, eps > parse_begin, state);
|
|
|
|
ret.state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
|
|
|
throw expectation_failure(string_view::begin(), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::vector::const_iterator
|
|
|
|
ircd::json::vector::end()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return { string_view::end(), string_view::end() };
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/object.h
|
|
|
|
//
|
|
|
|
|
2018-01-18 12:39:19 +01:00
|
|
|
decltype(ircd::json::object::max_recursion_depth)
|
|
|
|
const ircd::json::object::max_recursion_depth
|
|
|
|
{
|
|
|
|
32
|
|
|
|
};
|
|
|
|
|
2017-09-19 11:06:52 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::json::operator<<(std::ostream &s, const object::member &member)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
s << json::strung(member);
|
2017-09-19 11:06:52 +02:00
|
|
|
return s;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2017-03-16 21:36:18 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const object::member &member)
|
|
|
|
{
|
2017-09-19 11:06:52 +02:00
|
|
|
char *const start(begin(buf));
|
2018-03-20 22:51:58 +01:00
|
|
|
printer(buf, printer.name << printer.name_sep, member.first);
|
|
|
|
stringify(buf, member.second);
|
2018-03-28 01:45:56 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(serialized(member) == size(ret));
|
|
|
|
return ret;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-19 11:06:52 +02:00
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const object::member &member)
|
|
|
|
{
|
2018-09-06 07:03:13 +02:00
|
|
|
const json::value key{member.first, json::STRING};
|
|
|
|
return serialized(key) + 1 + serialized(member.second);
|
2017-09-19 11:06:52 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::json::operator<<(std::ostream &s, const object &object)
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
s << json::strung(object);
|
2017-09-14 20:30:06 +02:00
|
|
|
return s;
|
|
|
|
}
|
2017-03-11 04:31:20 +01:00
|
|
|
|
2017-09-19 11:06:52 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const object &object)
|
2018-03-21 08:10:29 +01:00
|
|
|
try
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-03-21 08:10:29 +01:00
|
|
|
using member_array = std::array<object::member, iov::MAX_SIZE>;
|
|
|
|
using member_arrays = std::array<member_array, object::max_recursion_depth>;
|
|
|
|
static_assert(sizeof(member_arrays) == 1_MiB); // yay reentrance .. joy :/
|
|
|
|
|
|
|
|
thread_local member_arrays ma;
|
|
|
|
thread_local size_t mctr;
|
|
|
|
const size_t mc{mctr++};
|
|
|
|
assert(mc < ma.size());
|
|
|
|
const unwind uw{[&mc]
|
2017-09-19 11:06:52 +02:00
|
|
|
{
|
2018-03-21 08:10:29 +01:00
|
|
|
--mctr;
|
|
|
|
assert(mctr == mc);
|
|
|
|
}};
|
2017-09-19 11:06:52 +02:00
|
|
|
|
2018-03-21 08:10:29 +01:00
|
|
|
size_t i(0);
|
|
|
|
auto &m{ma.at(mc)};
|
|
|
|
for(auto it(begin(object)); it != end(object); ++it, ++i)
|
|
|
|
m.at(i) = *it;
|
|
|
|
|
|
|
|
std::sort(begin(m), begin(m) + i, []
|
|
|
|
(const object::member &a, const object::member &b)
|
|
|
|
{
|
|
|
|
return a.first < b.first;
|
|
|
|
});
|
|
|
|
|
|
|
|
return stringify(buf, m.data(), m.data() + i);
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw print_error
|
|
|
|
{
|
|
|
|
"Too many members (%zu) for stringifying JSON object",
|
|
|
|
size(object)
|
|
|
|
};
|
2017-09-19 11:06:52 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 07:39:30 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const object::member *const &b,
|
|
|
|
const object::member *const &e)
|
|
|
|
{
|
|
|
|
char *const start(begin(buf));
|
|
|
|
static const auto stringify_member
|
|
|
|
{
|
|
|
|
[](mutable_buffer &buf, const object::member &member)
|
|
|
|
{
|
|
|
|
stringify(buf, member);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
printer(buf, printer.object_begin);
|
|
|
|
printer::list_protocol(buf, b, e, stringify_member);
|
|
|
|
printer(buf, printer.object_end);
|
2018-03-28 01:45:56 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(serialized(b, e) == size(ret));
|
|
|
|
return ret;
|
2018-03-21 07:39:30 +01:00
|
|
|
}
|
|
|
|
|
2017-09-19 11:06:52 +02:00
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const object &object)
|
|
|
|
{
|
2018-03-28 01:34:33 +02:00
|
|
|
const auto b(begin(object));
|
|
|
|
const auto e(end(object));
|
|
|
|
assert(!empty(object) || b == e);
|
|
|
|
const size_t ret(1 + (b == e));
|
|
|
|
return std::accumulate(b, e, ret, []
|
2017-09-19 11:06:52 +02:00
|
|
|
(auto ret, const object::member &member)
|
|
|
|
{
|
|
|
|
return ret += serialized(member) + 1;
|
|
|
|
});
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-03-21 07:39:30 +01:00
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const object::member *const &begin,
|
|
|
|
const object::member *const &end)
|
|
|
|
{
|
|
|
|
const size_t ret(1 + (begin == end));
|
|
|
|
return std::accumulate(begin, end, ret, []
|
|
|
|
(auto ret, const object::member &member)
|
|
|
|
{
|
|
|
|
return ret += serialized(member) + 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:29:16 +01:00
|
|
|
bool
|
|
|
|
ircd::json::sorted(const object::member *const &begin,
|
|
|
|
const object::member *const &end)
|
|
|
|
{
|
|
|
|
return std::is_sorted(begin, end, []
|
|
|
|
(const object::member &a, const object::member &b)
|
|
|
|
{
|
|
|
|
return a.first < b.first;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::sorted(const object &object)
|
|
|
|
{
|
|
|
|
auto it(begin(object));
|
|
|
|
if(it == end(object))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
string_view last{it->first};
|
|
|
|
for(++it; it != end(object); last = it->first, ++it)
|
|
|
|
if(it->first < last)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::object::const_iterator &
|
|
|
|
ircd::json::object::const_iterator::operator++()
|
|
|
|
try
|
|
|
|
{
|
2018-03-18 20:21:43 +01:00
|
|
|
static const auto &ws
|
|
|
|
{
|
|
|
|
parser.ws
|
|
|
|
};
|
|
|
|
|
2018-03-21 23:12:29 +01:00
|
|
|
static const parser::rule<json::object::member> member
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2018-03-18 20:21:43 +01:00
|
|
|
parser.name >> -ws >> parser.name_sep >> -ws >> raw[parser.value(0)]
|
2017-09-14 20:30:06 +02:00
|
|
|
,"next object member"
|
|
|
|
};
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2018-03-21 23:12:29 +01:00
|
|
|
static const parser::rule<json::object::member> parse_next
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2018-03-18 20:21:43 +01:00
|
|
|
(parser.object_end | (parser.value_sep >> -ws >> member)) >> -ws
|
2017-09-14 20:30:06 +02:00
|
|
|
,"next object member or end"
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
state.first = string_view{};
|
|
|
|
state.second = string_view{};
|
|
|
|
qi::parse(start, stop, eps > parse_next, state);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
2017-11-06 21:07:57 +01:00
|
|
|
throw expectation_failure(start, e);
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::object::operator std::string()
|
|
|
|
const
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
return json::strung(*this);
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::object::const_iterator
|
|
|
|
ircd::json::object::begin()
|
|
|
|
const try
|
|
|
|
{
|
2018-03-18 20:21:43 +01:00
|
|
|
static const auto &ws
|
|
|
|
{
|
|
|
|
parser.ws
|
|
|
|
};
|
|
|
|
|
2018-03-21 23:12:29 +01:00
|
|
|
static const parser::rule<json::object::member> object_member
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
2018-03-18 20:21:43 +01:00
|
|
|
parser.name >> -ws >> parser.name_sep >> -ws >> raw[parser.value(0)]
|
2017-09-14 20:30:06 +02:00
|
|
|
,"object member"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-03-21 23:12:29 +01:00
|
|
|
static const parser::rule<json::object::member> parse_begin
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-03-18 20:21:43 +01:00
|
|
|
-ws >> parser.object_begin >> -ws >> (parser.object_end | object_member) >> -ws
|
2017-09-14 20:30:06 +02:00
|
|
|
,"object begin and member or end"
|
|
|
|
};
|
2017-03-16 21:36:18 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
const_iterator ret
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
string_view::begin(), string_view::end()
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-11-06 21:07:57 +01:00
|
|
|
if(!string_view{*this}.empty())
|
2017-09-14 20:30:06 +02:00
|
|
|
qi::parse(ret.start, ret.stop, eps > parse_begin, ret.state);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
2017-11-06 21:07:57 +01:00
|
|
|
throw expectation_failure(string_view::begin(), e);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::object::const_iterator
|
|
|
|
ircd::json::object::end()
|
|
|
|
const
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
return { string_view::end(), string_view::end() };
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/array.h
|
|
|
|
//
|
|
|
|
|
2018-01-18 12:39:19 +01:00
|
|
|
decltype(ircd::json::array::max_recursion_depth)
|
|
|
|
ircd::json::array::max_recursion_depth
|
|
|
|
{
|
|
|
|
32
|
|
|
|
};
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const array &v)
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2017-11-01 23:55:28 +01:00
|
|
|
if(string_view{v}.empty())
|
|
|
|
{
|
2018-03-28 01:47:48 +02:00
|
|
|
const char *const start{begin(buf)};
|
2018-01-24 06:18:47 +01:00
|
|
|
consume(buf, copy(buf, empty_array));
|
2018-03-28 01:47:48 +02:00
|
|
|
const string_view ret{start, begin(buf)};
|
|
|
|
assert(serialized(v) == size(ret));
|
|
|
|
return ret;
|
2017-11-01 23:55:28 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 22:51:58 +01:00
|
|
|
return array::stringify(buf, begin(v), end(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const array &v)
|
|
|
|
{
|
2018-03-28 01:34:33 +02:00
|
|
|
assert(!empty(v) || (begin(v) == end(v)));
|
2018-03-20 22:51:58 +01:00
|
|
|
return array::serialized(begin(v), end(v));
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const std::string *const &b,
|
|
|
|
const std::string *const &e)
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
return array::stringify(buf, b, e);
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-28 05:47:56 +02:00
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const std::string *const &b,
|
|
|
|
const std::string *const &e)
|
2017-03-21 09:54:10 +01:00
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
return array::serialized(b, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const string_view *const &b,
|
|
|
|
const string_view *const &e)
|
|
|
|
{
|
|
|
|
return array::stringify(buf, b, e);
|
2017-09-28 05:47:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const string_view *const &b,
|
|
|
|
const string_view *const &e)
|
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
return array::serialized(b, e);
|
2017-03-21 09:54:10 +01:00
|
|
|
}
|
2017-03-31 00:55:17 +02:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
template<class it>
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::array::stringify(mutable_buffer &buf,
|
|
|
|
const it &b,
|
|
|
|
const it &e)
|
2017-03-21 09:54:10 +01:00
|
|
|
{
|
2017-09-16 20:58:27 +02:00
|
|
|
static const auto print_element
|
2017-03-31 00:55:17 +02:00
|
|
|
{
|
2017-09-16 20:58:27 +02:00
|
|
|
[](mutable_buffer &buf, const string_view &element)
|
2017-03-31 00:55:17 +02:00
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
json::stringify(buf, element);
|
2017-03-31 00:55:17 +02:00
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
};
|
2017-03-31 00:55:17 +02:00
|
|
|
|
2018-03-28 01:45:56 +02:00
|
|
|
using ircd::buffer::begin;
|
|
|
|
char *const start(begin(buf));
|
2017-09-14 20:30:06 +02:00
|
|
|
printer(buf, printer.array_begin);
|
2017-09-16 20:58:27 +02:00
|
|
|
printer::list_protocol(buf, b, e, print_element);
|
2017-09-14 20:30:06 +02:00
|
|
|
printer(buf, printer.array_end);
|
2018-03-28 01:45:56 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
using ircd::buffer::size;
|
|
|
|
assert(serialized(b, e) == size(ret));
|
|
|
|
return ret;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
|
2018-03-20 22:51:58 +01:00
|
|
|
template<class it>
|
|
|
|
size_t
|
|
|
|
ircd::json::array::serialized(const it &b,
|
|
|
|
const it &e)
|
|
|
|
{
|
2018-03-24 07:18:11 +01:00
|
|
|
const size_t ret(1 + (b == e));
|
2018-03-20 22:51:58 +01:00
|
|
|
return std::accumulate(b, e, ret, []
|
|
|
|
(auto ret, const string_view &value)
|
|
|
|
{
|
|
|
|
return ret += json::serialized(value) + 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::json::operator<<(std::ostream &s, const array &a)
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
s << json::strung(a);
|
2017-09-14 20:30:06 +02:00
|
|
|
return s;
|
2017-03-21 09:54:10 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::array::const_iterator &
|
|
|
|
ircd::json::array::const_iterator::operator++()
|
|
|
|
try
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-03-18 20:21:43 +01:00
|
|
|
static const auto &ws
|
|
|
|
{
|
|
|
|
parser.ws
|
|
|
|
};
|
|
|
|
|
2018-03-21 23:12:29 +01:00
|
|
|
static const parser::rule<string_view> value
|
2017-11-30 19:49:16 +01:00
|
|
|
{
|
2018-01-18 12:39:19 +01:00
|
|
|
raw[parser.value(0)]
|
2017-11-30 19:49:16 +01:00
|
|
|
,"array element"
|
|
|
|
};
|
|
|
|
|
2018-03-21 23:12:29 +01:00
|
|
|
static const parser::rule<string_view> parse_next
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2018-03-18 20:21:43 +01:00
|
|
|
(parser.array_end | (parser.value_sep >> -ws >> value)) >> -ws
|
2017-09-14 20:30:06 +02:00
|
|
|
,"next array element or end"
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
state = string_view{};
|
|
|
|
qi::parse(start, stop, eps > parse_next, state);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
2017-11-06 21:07:57 +01:00
|
|
|
throw expectation_failure(start, e);
|
2017-03-31 00:55:17 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::array::operator std::string()
|
|
|
|
const
|
2017-03-31 00:55:17 +02:00
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
return json::strung(*this);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::array::const_iterator
|
|
|
|
ircd::json::array::begin()
|
|
|
|
const try
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-03-18 20:21:43 +01:00
|
|
|
static const auto &ws
|
|
|
|
{
|
|
|
|
parser.ws
|
|
|
|
};
|
|
|
|
|
2018-03-21 23:12:29 +01:00
|
|
|
static const parser::rule<string_view> value
|
2017-11-30 19:49:16 +01:00
|
|
|
{
|
2018-01-18 12:39:19 +01:00
|
|
|
raw[parser.value(0)]
|
2017-11-30 19:49:16 +01:00
|
|
|
,"array element"
|
|
|
|
};
|
|
|
|
|
2018-03-21 23:12:29 +01:00
|
|
|
static const parser::rule<string_view> parse_begin
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-03-18 20:21:43 +01:00
|
|
|
-ws >> parser.array_begin >> -ws >> (parser.array_end | value) >> -ws
|
2017-09-14 20:30:06 +02:00
|
|
|
,"array begin and element or end"
|
|
|
|
};
|
2017-04-03 05:58:30 +02:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
const_iterator ret
|
2017-04-03 05:58:30 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
string_view::begin(), string_view::end()
|
|
|
|
};
|
2017-04-03 05:58:30 +02:00
|
|
|
|
2017-11-06 21:07:57 +01:00
|
|
|
if(!string_view{*this}.empty())
|
2017-09-14 20:30:06 +02:00
|
|
|
qi::parse(ret.start, ret.stop, eps > parse_begin, ret.state);
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-11-06 21:07:57 +01:00
|
|
|
throw expectation_failure(string_view::begin(), e);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::array::const_iterator
|
|
|
|
ircd::json::array::end()
|
|
|
|
const
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
return { string_view::end(), string_view::end() };
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2018-03-21 21:49:00 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/member.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const members &list)
|
|
|
|
{
|
|
|
|
return stringify(buf, std::begin(list), std::end(list));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const member &m)
|
|
|
|
{
|
|
|
|
return stringify(buf, &m, &m + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const member *const &b,
|
|
|
|
const member *const &e)
|
2018-03-21 21:49:59 +01:00
|
|
|
try
|
2018-03-21 21:49:00 +01:00
|
|
|
{
|
2018-03-21 21:49:59 +01:00
|
|
|
using member_array = std::array<const member *, iov::MAX_SIZE>;
|
|
|
|
using member_arrays = std::array<member_array, object::max_recursion_depth>;
|
|
|
|
static_assert(sizeof(member_arrays) == 256_KiB);
|
|
|
|
|
|
|
|
thread_local member_arrays ma;
|
|
|
|
thread_local size_t mctr;
|
|
|
|
const size_t mc{mctr++};
|
|
|
|
assert(mc < ma.size());
|
|
|
|
const unwind uw{[&mc]
|
|
|
|
{
|
|
|
|
--mctr;
|
|
|
|
assert(mctr == mc);
|
|
|
|
}};
|
|
|
|
|
|
|
|
size_t i(0);
|
|
|
|
auto &m{ma.at(mc)};
|
|
|
|
for(auto it(b); it != e; ++it)
|
|
|
|
m.at(i++) = it;
|
|
|
|
|
|
|
|
std::sort(begin(m), begin(m) + i, []
|
|
|
|
(const member *const &a, const member *const &b)
|
|
|
|
{
|
|
|
|
return *a < *b;
|
|
|
|
});
|
|
|
|
|
2018-03-21 21:49:00 +01:00
|
|
|
static const auto print_member
|
|
|
|
{
|
2018-03-21 21:49:59 +01:00
|
|
|
[](mutable_buffer &buf, const member *const &m)
|
2018-03-21 21:49:00 +01:00
|
|
|
{
|
2018-03-21 21:49:59 +01:00
|
|
|
printer(buf, printer.name << printer.name_sep, m->first);
|
|
|
|
stringify(buf, m->second);
|
2018-03-21 21:49:00 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
char *const start{begin(buf)};
|
|
|
|
printer(buf, printer.object_begin);
|
2018-03-21 21:49:59 +01:00
|
|
|
printer::list_protocol(buf, m.data(), m.data() + i, print_member);
|
2018-03-21 21:49:00 +01:00
|
|
|
printer(buf, printer.object_end);
|
2018-03-28 01:45:56 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(serialized(b, e) == size(ret));
|
|
|
|
return ret;
|
2018-03-21 21:49:00 +01:00
|
|
|
}
|
2018-03-21 21:49:59 +01:00
|
|
|
catch(const std::out_of_range &)
|
|
|
|
{
|
|
|
|
throw print_error
|
|
|
|
{
|
|
|
|
"Too many members (%zd) for stringifying", std::distance(b, e)
|
|
|
|
};
|
|
|
|
}
|
2018-03-21 21:49:00 +01:00
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const members &m)
|
|
|
|
{
|
|
|
|
return serialized(std::begin(m), std::end(m));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const member *const &begin,
|
|
|
|
const member *const &end)
|
|
|
|
{
|
2018-03-24 07:18:11 +01:00
|
|
|
const size_t ret(1 + (begin == end));
|
2018-03-21 21:49:00 +01:00
|
|
|
return std::accumulate(begin, end, ret, []
|
|
|
|
(auto ret, const auto &member)
|
|
|
|
{
|
|
|
|
return ret += serialized(member) + 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const member &member)
|
|
|
|
{
|
|
|
|
return serialized(member.first) + 1 + serialized(member.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::sorted(const member *const &begin,
|
|
|
|
const member *const &end)
|
|
|
|
{
|
|
|
|
return std::is_sorted(begin, end, []
|
|
|
|
(const member &a, const member &b)
|
|
|
|
{
|
|
|
|
return a < b;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/value.h
|
|
|
|
//
|
|
|
|
|
2018-01-24 06:18:47 +01:00
|
|
|
const ircd::string_view ircd::json::literal_null { "null" };
|
|
|
|
const ircd::string_view ircd::json::literal_true { "true" };
|
|
|
|
const ircd::string_view ircd::json::literal_false { "false" };
|
|
|
|
const ircd::string_view ircd::json::empty_string { "\"\"" };
|
|
|
|
const ircd::string_view ircd::json::empty_object { "{}" };
|
|
|
|
const ircd::string_view ircd::json::empty_array { "[]" };
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2018-05-20 07:10:58 +02:00
|
|
|
decltype(ircd::json::undefined_number)
|
|
|
|
ircd::json::undefined_number
|
|
|
|
{
|
2018-05-23 09:29:26 +02:00
|
|
|
std::numeric_limits<decltype(ircd::json::undefined_number)>::min()
|
2018-05-20 07:10:58 +02:00
|
|
|
};
|
|
|
|
|
2018-05-23 09:29:26 +02:00
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
ircd::json::undefined_number != 0
|
|
|
|
);
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::json::operator<<(std::ostream &s, const value &v)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
s << json::strung(v);
|
2017-09-14 20:30:06 +02:00
|
|
|
return s;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const value *const &b,
|
|
|
|
const value *const &e)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-09-16 20:58:27 +02:00
|
|
|
static const auto print_value
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2017-09-16 20:58:27 +02:00
|
|
|
[](mutable_buffer &buf, const value &value)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2017-09-16 20:58:27 +02:00
|
|
|
stringify(buf, value);
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2017-09-16 20:58:27 +02:00
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-16 20:58:27 +02:00
|
|
|
char *const start(begin(buf));
|
|
|
|
printer(buf, printer.array_begin);
|
|
|
|
printer::list_protocol(buf, b, e, print_value);
|
2017-09-14 20:30:06 +02:00
|
|
|
printer(buf, printer.array_end);
|
2018-03-28 01:45:56 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(serialized(b, e) == size(ret));
|
|
|
|
return ret;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const value &v)
|
|
|
|
{
|
|
|
|
const auto start
|
|
|
|
{
|
|
|
|
begin(buf)
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
switch(v.type)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
case STRING:
|
|
|
|
{
|
2018-09-06 08:10:51 +02:00
|
|
|
if(!v.string)
|
|
|
|
{
|
|
|
|
consume(buf, copy(buf, empty_string));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const string_view sv
|
|
|
|
{
|
|
|
|
v.string, v.len
|
|
|
|
};
|
|
|
|
|
|
|
|
if(v.serial)
|
|
|
|
{
|
|
|
|
consume(buf, copy(buf, sv));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(surrounds(sv, '"'))
|
|
|
|
printer(buf, printer.quoted, sv);
|
|
|
|
else
|
|
|
|
printer(buf, printer.unquoted, sv);
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case LITERAL:
|
|
|
|
{
|
2018-03-21 23:43:35 +01:00
|
|
|
if(v.serial)
|
|
|
|
printer(buf, printer.literal, string_view{v});
|
|
|
|
else if(v.integer)
|
|
|
|
consume(buf, copy(buf, "true"_sv));
|
|
|
|
else
|
|
|
|
consume(buf, copy(buf, "false"_sv));
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OBJECT:
|
|
|
|
{
|
|
|
|
if(v.serial)
|
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
stringify(buf, json::object{string_view{v}});
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(v.object)
|
|
|
|
{
|
|
|
|
stringify(buf, v.object, v.object + v.len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-01-24 06:18:47 +01:00
|
|
|
consume(buf, copy(buf, empty_object));
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
{
|
|
|
|
if(v.serial)
|
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
stringify(buf, json::array{string_view{v}});
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(v.array)
|
|
|
|
{
|
|
|
|
stringify(buf, v.array, v.array + v.len);
|
|
|
|
break;
|
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2018-01-24 06:18:47 +01:00
|
|
|
consume(buf, copy(buf, empty_array));
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
case NUMBER:
|
|
|
|
{
|
|
|
|
if(v.serial)
|
2018-03-28 01:15:20 +02:00
|
|
|
//printer(buf, printer.number, string_view{v});
|
|
|
|
consume(buf, copy(buf, strip(string_view{v}, ' ')));
|
2018-03-20 22:51:58 +01:00
|
|
|
else if(v.floats)
|
|
|
|
consume(buf, copy(buf, lex_cast(v.floating)));
|
2017-09-14 20:30:06 +02:00
|
|
|
else
|
2018-03-20 22:51:58 +01:00
|
|
|
consume(buf, copy(buf, lex_cast(v.integer)));
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2018-03-28 01:45:56 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(serialized(v) == size(ret));
|
|
|
|
return ret;
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
size_t
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::serialized(const values &v)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
return serialized(std::begin(v), std::end(v));
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
2017-03-16 21:36:18 +01:00
|
|
|
size_t
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::serialized(const value *const &begin,
|
|
|
|
const value *const &end)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
// One opening '[' and either one ']' or comma count.
|
2018-03-24 07:18:11 +01:00
|
|
|
const size_t ret(1 + (begin == end));
|
2017-09-14 20:30:06 +02:00
|
|
|
return std::accumulate(begin, end, size_t(ret), []
|
|
|
|
(auto ret, const value &v)
|
|
|
|
{
|
|
|
|
return ret += serialized(v) + 1; // 1 comma
|
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const value &v)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
switch(v.type)
|
|
|
|
{
|
2018-04-04 22:36:30 +02:00
|
|
|
case OBJECT: return
|
|
|
|
v.serial?
|
|
|
|
serialized(json::object{v}):
|
|
|
|
serialized(v.object, v.object + v.len);
|
|
|
|
|
|
|
|
case ARRAY: return
|
|
|
|
v.serial?
|
|
|
|
serialized(json::array{v}):
|
|
|
|
serialized(v.array, v.array + v.len);
|
|
|
|
|
|
|
|
case LITERAL: return
|
|
|
|
v.serial?
|
|
|
|
v.len:
|
|
|
|
v.integer?
|
|
|
|
size(literal_true):
|
|
|
|
size(literal_false);
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
case NUMBER:
|
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
thread_local char test_buffer[256];
|
|
|
|
mutable_buffer buf{test_buffer};
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2018-03-20 22:51:58 +01:00
|
|
|
if(v.serial)
|
2018-03-28 01:15:20 +02:00
|
|
|
//printer(buf, printer.number, string_view{v});
|
|
|
|
return size(strip(string_view{v}, ' '));
|
2018-03-20 22:51:58 +01:00
|
|
|
else if(v.floats)
|
|
|
|
return size(lex_cast(v.floating));
|
|
|
|
else
|
|
|
|
return size(lex_cast(v.integer));
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2018-03-20 22:51:58 +01:00
|
|
|
return begin(buf) - test_buffer;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case STRING:
|
|
|
|
{
|
|
|
|
if(!v.string)
|
2018-03-29 07:05:54 +02:00
|
|
|
return size(empty_string);
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2018-09-06 08:10:51 +02:00
|
|
|
if(v.serial)
|
|
|
|
return v.len;
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
size_t ret(v.len);
|
|
|
|
const string_view sv{v.string, v.len};
|
|
|
|
ret += !startswith(sv, '"');
|
|
|
|
ret += !endswith(sv, '"');
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
throw type_error("deciding the size of a type[%u] is undefined", int(v.type));
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2018-04-05 00:20:24 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::defined(const value &a)
|
|
|
|
{
|
|
|
|
return !a.undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ircd::json::type
|
|
|
|
ircd::json::type(const value &a)
|
|
|
|
{
|
|
|
|
return a.type;
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
//
|
2018-04-05 00:20:24 +02:00
|
|
|
// value::value
|
2017-09-12 18:37:44 +02:00
|
|
|
//
|
|
|
|
|
2018-11-05 02:20:16 +01:00
|
|
|
ircd::json::value::value(const nullptr_t &)
|
|
|
|
:value
|
|
|
|
{
|
|
|
|
literal_null, type::LITERAL
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const json::object &sv)
|
|
|
|
:value{sv, OBJECT}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const json::array &sv)
|
|
|
|
:value{sv, ARRAY}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const bool &boolean)
|
|
|
|
:value
|
|
|
|
{
|
|
|
|
boolean? literal_true : literal_false, type::LITERAL
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const uint8_t &integer)
|
|
|
|
:value{int64_t{integer}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const int8_t &integer)
|
|
|
|
:value{int64_t{integer}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const uint16_t &integer)
|
|
|
|
:value{int64_t{integer}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const int16_t &integer)
|
|
|
|
:value{int64_t{integer}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const uint32_t &integer)
|
|
|
|
:value{int64_t{integer}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const int32_t &integer)
|
|
|
|
:value{int64_t{integer}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const int64_t &integer)
|
|
|
|
:integer{integer}
|
|
|
|
,len{0}
|
|
|
|
,type{NUMBER}
|
|
|
|
,serial{false}
|
|
|
|
,alloc{false}
|
|
|
|
,floats{false}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const double &floating)
|
|
|
|
:floating{floating}
|
|
|
|
,len{0}
|
|
|
|
,type{NUMBER}
|
|
|
|
,serial{false}
|
|
|
|
,alloc{false}
|
|
|
|
,floats{true}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const char *const &str)
|
|
|
|
:value{string_view{str}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const char *const &str,
|
|
|
|
const enum type &type)
|
|
|
|
:value{string_view{str}, type}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const string_view &sv)
|
|
|
|
:value{sv, json::type(sv, std::nothrow)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-04-05 00:20:24 +02:00
|
|
|
ircd::json::value::value(const string_view &sv,
|
|
|
|
const enum type &type)
|
|
|
|
:string{sv.data()}
|
|
|
|
,len{sv.size()}
|
|
|
|
,type{type}
|
|
|
|
,serial{type == STRING? surrounds(sv, '"') : true}
|
|
|
|
,alloc{false}
|
|
|
|
,floats{false}
|
2018-11-05 02:20:16 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const std::string &s)
|
|
|
|
:value{s, json::type(s, std::nothrow)}
|
|
|
|
{
|
|
|
|
}
|
2018-04-05 00:20:24 +02:00
|
|
|
|
|
|
|
ircd::json::value::value(const std::string &s,
|
|
|
|
const enum type &type)
|
|
|
|
:string{nullptr}
|
|
|
|
,len{0}
|
|
|
|
,type{type}
|
|
|
|
,serial{type == STRING? surrounds(s, '"') : true}
|
|
|
|
,alloc{true}
|
|
|
|
,floats{false}
|
|
|
|
{
|
|
|
|
const string_view sv{s};
|
|
|
|
create_string(serialized(sv), [&sv]
|
|
|
|
(mutable_buffer buf)
|
|
|
|
{
|
|
|
|
json::stringify(buf, sv);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::value(const struct member *const &object,
|
|
|
|
const size_t &len)
|
|
|
|
:object{object}
|
|
|
|
,len{len}
|
|
|
|
,type{OBJECT}
|
|
|
|
,serial{false}
|
|
|
|
,alloc{false}
|
|
|
|
,floats{false}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::json::value::value(std::unique_ptr<const struct member[]> &&object,
|
|
|
|
const size_t &len)
|
|
|
|
:object{object.get()}
|
|
|
|
,len{len}
|
|
|
|
,type{OBJECT}
|
|
|
|
,serial{false}
|
|
|
|
,alloc{true}
|
|
|
|
,floats{false}
|
|
|
|
{
|
|
|
|
object.release();
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value::value(const json::members &members)
|
|
|
|
:string{nullptr}
|
|
|
|
,len{serialized(members)}
|
|
|
|
,type{OBJECT}
|
|
|
|
,serial{true}
|
|
|
|
,alloc{true}
|
|
|
|
,floats{false}
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
create_string(len, [&members]
|
2018-02-18 23:04:22 +01:00
|
|
|
(mutable_buffer &buffer)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
json::stringify(buffer, members);
|
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value::value(const value &other)
|
|
|
|
:integer{other.integer}
|
|
|
|
,len{other.len}
|
|
|
|
,type{other.type}
|
|
|
|
,serial{other.serial}
|
|
|
|
,alloc{other.alloc}
|
|
|
|
,floats{other.floats}
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-03-12 18:55:18 +01:00
|
|
|
if(serial)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-03-12 18:55:18 +01:00
|
|
|
create_string(len, [&other]
|
2018-02-18 23:04:22 +01:00
|
|
|
(mutable_buffer &buffer)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
json::stringify(buffer, string_view{other});
|
2017-09-14 20:30:06 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else switch(type)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
case OBJECT:
|
2018-01-27 04:02:56 +01:00
|
|
|
{
|
2018-03-12 18:55:18 +01:00
|
|
|
if(!object)
|
2018-01-27 04:02:56 +01:00
|
|
|
break;
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2018-01-27 04:02:56 +01:00
|
|
|
const size_t count(this->len);
|
|
|
|
create_string(serialized(object, object + count), [this, &count]
|
2018-02-18 23:04:22 +01:00
|
|
|
(mutable_buffer &buffer)
|
2018-01-27 04:02:56 +01:00
|
|
|
{
|
|
|
|
json::stringify(buffer, object, object + count);
|
|
|
|
});
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
2018-01-27 04:02:56 +01:00
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
case ARRAY:
|
2018-01-27 04:02:56 +01:00
|
|
|
{
|
2018-03-12 18:55:18 +01:00
|
|
|
if(!array)
|
2018-01-27 04:02:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
const size_t count(this->len);
|
|
|
|
create_string(serialized(array, array + count), [this, &count]
|
2018-02-18 23:04:22 +01:00
|
|
|
(mutable_buffer &buffer)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-01-27 04:02:56 +01:00
|
|
|
json::stringify(buffer, array, array + count);
|
|
|
|
});
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
2018-01-27 04:02:56 +01:00
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2017-03-18 04:32:32 +01:00
|
|
|
case STRING:
|
2018-01-27 04:02:56 +01:00
|
|
|
{
|
2018-03-12 18:55:18 +01:00
|
|
|
if(!string)
|
2018-01-27 04:02:56 +01:00
|
|
|
break;
|
|
|
|
|
2018-03-20 22:51:58 +01:00
|
|
|
create_string(serialized(other), [&other]
|
2018-02-18 23:04:22 +01:00
|
|
|
(mutable_buffer &buffer)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
json::stringify(buffer, other);
|
2018-01-27 04:02:56 +01:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2017-03-20 23:59:14 +01:00
|
|
|
case LITERAL:
|
2017-09-14 20:30:06 +02:00
|
|
|
case NUMBER:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 00:20:24 +02:00
|
|
|
ircd::json::value::value(value &&other)
|
|
|
|
noexcept
|
|
|
|
:integer{other.integer}
|
|
|
|
,len{other.len}
|
|
|
|
,type{other.type}
|
|
|
|
,serial{other.serial}
|
|
|
|
,alloc{other.alloc}
|
|
|
|
,floats{other.floats}
|
|
|
|
{
|
|
|
|
other.alloc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value &
|
|
|
|
ircd::json::value::operator=(value &&other)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~value();
|
|
|
|
integer = other.integer;
|
|
|
|
len = other.len;
|
|
|
|
type = other.type;
|
|
|
|
serial = other.serial;
|
|
|
|
alloc = other.alloc;
|
|
|
|
other.alloc = false;
|
|
|
|
floats = other.floats;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value &
|
|
|
|
ircd::json::value::operator=(const value &other)
|
|
|
|
{
|
|
|
|
this->~value();
|
|
|
|
new (this) value(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::~value()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(!alloc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
else if(serial)
|
|
|
|
delete[] string;
|
|
|
|
|
|
|
|
else switch(type)
|
|
|
|
{
|
|
|
|
case STRING:
|
|
|
|
delete[] string;
|
|
|
|
break;
|
2017-03-18 04:32:32 +01:00
|
|
|
|
|
|
|
case OBJECT:
|
2017-09-14 20:30:06 +02:00
|
|
|
delete[] object;
|
|
|
|
break;
|
2017-03-18 04:32:32 +01:00
|
|
|
|
|
|
|
case ARRAY:
|
2017-09-14 20:30:06 +02:00
|
|
|
delete[] array;
|
|
|
|
break;
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
default:
|
|
|
|
break;
|
2017-03-16 21:36:18 +01:00
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value::operator std::string()
|
|
|
|
const
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
return json::strung(*this);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::value::operator string_view()
|
2017-03-16 21:36:18 +01:00
|
|
|
const
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-18 04:32:32 +01:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case STRING:
|
|
|
|
return unquote(string_view{string, len});
|
|
|
|
|
2017-09-12 19:02:27 +02:00
|
|
|
case NUMBER:
|
2017-09-14 20:30:06 +02:00
|
|
|
return serial? string_view{string, len}:
|
|
|
|
floats? byte_view<string_view>{floating}:
|
2017-12-25 04:21:49 +01:00
|
|
|
byte_view<string_view>{integer};
|
2017-03-18 04:32:32 +01:00
|
|
|
case ARRAY:
|
|
|
|
case OBJECT:
|
2017-03-20 23:59:14 +01:00
|
|
|
case LITERAL:
|
2017-09-14 20:30:06 +02:00
|
|
|
if(likely(serial))
|
2017-03-18 04:32:32 +01:00
|
|
|
return string_view{string, len};
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"value type[%d] is not a string", int(type)
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 21:29:21 +02:00
|
|
|
ircd::json::value::operator int64_t()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case NUMBER:
|
|
|
|
return likely(!floats)? integer : floating;
|
|
|
|
|
|
|
|
case STRING:
|
2017-09-14 20:30:06 +02:00
|
|
|
return lex_cast<int64_t>(string_view{*this});
|
2017-09-08 21:29:21 +02:00
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
case OBJECT:
|
|
|
|
case LITERAL:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"value type[%d] is not an int64_t", int(type)
|
|
|
|
};
|
2017-09-08 21:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::operator double()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case NUMBER:
|
|
|
|
return likely(floats)? floating : integer;
|
|
|
|
|
|
|
|
case STRING:
|
2017-09-14 20:30:06 +02:00
|
|
|
return lex_cast<double>(string_view{*this});
|
2017-09-08 21:29:21 +02:00
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
case OBJECT:
|
|
|
|
case LITERAL:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"value type[%d] is not a float", int(type)
|
|
|
|
};
|
2017-09-08 21:29:21 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 19:02:27 +02:00
|
|
|
bool
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value::operator!()
|
2017-09-12 19:02:27 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case NUMBER:
|
2017-09-14 20:30:06 +02:00
|
|
|
return floats? !(floating > 0.0 || floating < 0.0):
|
|
|
|
!bool(integer);
|
2017-09-12 19:02:27 +02:00
|
|
|
|
|
|
|
case STRING:
|
2017-09-14 20:30:06 +02:00
|
|
|
return string? !len || string_view{*this} == empty_string:
|
|
|
|
true;
|
2017-09-12 19:02:27 +02:00
|
|
|
|
|
|
|
case OBJECT:
|
2017-09-14 20:30:06 +02:00
|
|
|
return serial? !len || string_view{*this} == empty_object:
|
|
|
|
object? !len:
|
|
|
|
true;
|
2017-09-12 19:02:27 +02:00
|
|
|
|
|
|
|
case ARRAY:
|
2017-09-14 20:30:06 +02:00
|
|
|
return serial? !len || (string_view{*this} == empty_array):
|
|
|
|
array? !len:
|
|
|
|
true;
|
2017-09-12 19:02:27 +02:00
|
|
|
|
|
|
|
case LITERAL:
|
2017-09-14 20:30:06 +02:00
|
|
|
if(serial)
|
|
|
|
return string == nullptr ||
|
|
|
|
string_view{*this} == literal_false ||
|
|
|
|
string_view{*this} == literal_null;
|
2018-04-12 07:57:10 +02:00
|
|
|
else
|
|
|
|
return !bool(integer);
|
2017-09-12 19:02:27 +02:00
|
|
|
};
|
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"deciding if a type[%u] is falsy is undefined", int(type)
|
|
|
|
};
|
2017-09-12 19:02:27 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 05:54:35 +02:00
|
|
|
bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::value::empty()
|
2017-04-03 05:54:35 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
case NUMBER:
|
|
|
|
return serial? !len:
|
|
|
|
floats? !(floating > 0.0 || floating < 0.0):
|
|
|
|
!bool(integer);
|
|
|
|
|
|
|
|
case STRING:
|
2017-12-12 21:34:44 +01:00
|
|
|
return !string || !len || (serial && string_view{*this} == empty_string);
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
case OBJECT:
|
|
|
|
return serial? !len || string_view{*this} == empty_object:
|
|
|
|
object? !len:
|
|
|
|
true;
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
return serial? !len || string_view{*this} == empty_array:
|
2018-02-28 12:12:17 +01:00
|
|
|
array? !len:
|
|
|
|
true;
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
case LITERAL:
|
|
|
|
return serial? !len:
|
2018-04-12 07:57:10 +02:00
|
|
|
false;
|
2017-04-03 05:54:35 +02:00
|
|
|
};
|
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"deciding if a type[%u] is empty is undefined", int(type)
|
|
|
|
};
|
2017-04-03 05:54:35 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 19:02:27 +02:00
|
|
|
bool
|
|
|
|
ircd::json::value::null()
|
|
|
|
const
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2017-09-12 19:02:27 +02:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case NUMBER:
|
|
|
|
return floats? !(floating > 0.0 || floating < 0.0):
|
2017-09-14 20:30:06 +02:00
|
|
|
!bool(integer);
|
2017-09-12 19:02:27 +02:00
|
|
|
|
|
|
|
case STRING:
|
2018-04-12 07:57:10 +02:00
|
|
|
return string == nullptr ||
|
|
|
|
string_view{string, len}.null();
|
2017-09-12 19:02:27 +02:00
|
|
|
|
|
|
|
case OBJECT:
|
|
|
|
return serial? string == nullptr:
|
2017-09-14 20:30:06 +02:00
|
|
|
object? false:
|
2017-09-12 19:02:27 +02:00
|
|
|
true;
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
return serial? string == nullptr:
|
2018-02-28 12:12:17 +01:00
|
|
|
array? false:
|
2017-09-12 19:02:27 +02:00
|
|
|
true;
|
|
|
|
|
|
|
|
case LITERAL:
|
|
|
|
return serial? string == nullptr:
|
2018-04-12 07:57:10 +02:00
|
|
|
string? literal_null == string:
|
|
|
|
false;
|
2017-09-12 19:02:27 +02:00
|
|
|
};
|
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"deciding if a type[%u] is null is undefined", int(type)
|
|
|
|
};
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
bool
|
|
|
|
ircd::json::value::undefined()
|
|
|
|
const
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
switch(type)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
case NUMBER:
|
2018-05-20 07:10:58 +02:00
|
|
|
return integer == undefined_number;
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
case STRING:
|
2018-04-12 07:57:10 +02:00
|
|
|
return string_view{string, len}.undefined();
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
case OBJECT:
|
|
|
|
return serial? string == nullptr:
|
|
|
|
object? false:
|
|
|
|
true;
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
return serial? string == nullptr:
|
|
|
|
array? false:
|
|
|
|
true;
|
|
|
|
|
|
|
|
case LITERAL:
|
|
|
|
return serial? string == nullptr:
|
2018-04-12 06:59:47 +02:00
|
|
|
false;
|
2017-03-16 21:36:18 +01:00
|
|
|
};
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"deciding if a type[%u] is undefined is undefined", int(type)
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
void
|
|
|
|
ircd::json::value::create_string(const size_t &len,
|
|
|
|
const create_string_closure &closure)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
const size_t max
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
len + 1
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
std::unique_ptr<char[]> string
|
|
|
|
{
|
|
|
|
new char[max]
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2018-02-18 23:04:22 +01:00
|
|
|
mutable_buffer buffer
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
|
|
|
string.get(), len
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
closure(buffer);
|
|
|
|
(string.get())[len] = '\0';
|
|
|
|
this->alloc = true;
|
|
|
|
this->serial = true;
|
|
|
|
this->len = len;
|
|
|
|
this->string = string.release();
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator>(const value &a, const value &b)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2018-05-20 07:11:39 +02:00
|
|
|
if(unlikely(type(a) != STRING || type(b) != STRING))
|
|
|
|
throw type_error("cannot compare values");
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
return static_cast<string_view>(a) > static_cast<string_view>(b);
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<(const value &a, const value &b)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2018-05-20 07:11:39 +02:00
|
|
|
if(unlikely(type(a) != STRING || type(b) != STRING))
|
|
|
|
throw type_error("cannot compare values");
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
return static_cast<string_view>(a) < static_cast<string_view>(b);
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator>=(const value &a, const value &b)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2018-05-20 07:11:39 +02:00
|
|
|
if(unlikely(type(a) != STRING || type(b) != STRING))
|
|
|
|
throw type_error("cannot compare values");
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
return static_cast<string_view>(a) >= static_cast<string_view>(b);
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<=(const value &a, const value &b)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2018-05-20 07:11:39 +02:00
|
|
|
if(unlikely(type(a) != STRING || type(b) != STRING))
|
|
|
|
throw type_error("cannot compare values");
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
return static_cast<string_view>(a) <= static_cast<string_view>(b);
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator!=(const value &a, const value &b)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2018-05-20 07:11:39 +02:00
|
|
|
if(unlikely(type(a) != STRING || type(b) != STRING))
|
|
|
|
throw type_error("cannot compare values");
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
return static_cast<string_view>(a) != static_cast<string_view>(b);
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator==(const value &a, const value &b)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2018-05-20 07:11:39 +02:00
|
|
|
if(unlikely(type(a) != STRING || type(b) != STRING))
|
|
|
|
throw type_error("cannot compare values");
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
return static_cast<string_view>(a) == static_cast<string_view>(b);
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json.h
|
|
|
|
//
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-03-29 05:47:26 +02:00
|
|
|
std::string
|
|
|
|
ircd::json::why(const string_view &s)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
valid(s);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
catch(const expectation_failure &e)
|
|
|
|
{
|
|
|
|
return e.what();
|
|
|
|
}
|
|
|
|
|
2017-11-26 20:57:46 +01:00
|
|
|
bool
|
|
|
|
ircd::json::valid(const string_view &s,
|
|
|
|
std::nothrow_t)
|
|
|
|
noexcept try
|
|
|
|
{
|
2018-03-29 07:05:54 +02:00
|
|
|
static const parser::rule<> validator
|
|
|
|
{
|
|
|
|
parser.value(0) >> eoi
|
|
|
|
};
|
|
|
|
|
2017-11-26 20:57:46 +01:00
|
|
|
const char *start(begin(s)), *const stop(end(s));
|
2018-03-29 07:05:54 +02:00
|
|
|
return qi::parse(start, stop, validator);
|
2017-11-26 20:57:46 +01:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::valid(const string_view &s)
|
|
|
|
try
|
|
|
|
{
|
2018-03-29 07:05:54 +02:00
|
|
|
static const parser::rule<> validator
|
|
|
|
{
|
|
|
|
eps > parser.value(0) > eoi
|
|
|
|
};
|
|
|
|
|
2017-11-26 20:57:46 +01:00
|
|
|
const char *start(begin(s)), *const stop(end(s));
|
2018-03-29 07:05:54 +02:00
|
|
|
qi::parse(start, stop, validator);
|
2017-11-26 20:57:46 +01:00
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
|
|
|
throw expectation_failure(begin(s), e);
|
|
|
|
}
|
|
|
|
|
2018-03-29 06:11:26 +02:00
|
|
|
void
|
|
|
|
ircd::json::valid_output(const string_view &sv,
|
|
|
|
const size_t &expected)
|
|
|
|
{
|
|
|
|
if(unlikely(size(sv) != expected))
|
|
|
|
throw print_error
|
|
|
|
{
|
|
|
|
"stringified:%zu != serialized:%zu: %s",
|
|
|
|
size(sv),
|
|
|
|
expected,
|
|
|
|
sv
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(!valid(sv, std::nothrow))) //note: false alarm when T=json::member
|
|
|
|
throw print_error
|
|
|
|
{
|
|
|
|
"strung %zu bytes: %s: %s",
|
|
|
|
size(sv),
|
|
|
|
why(sv),
|
|
|
|
sv
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-16 02:25:15 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const string_view &v)
|
|
|
|
{
|
|
|
|
if(v.empty() && defined(v))
|
|
|
|
{
|
2018-03-28 01:47:48 +02:00
|
|
|
const char *const start{begin(buf)};
|
2018-01-24 06:18:47 +01:00
|
|
|
consume(buf, copy(buf, empty_string));
|
2018-03-28 01:47:48 +02:00
|
|
|
const string_view ret{start, begin(buf)};
|
|
|
|
assert(serialized(v) == size(ret));
|
|
|
|
return ret;
|
2017-11-16 02:25:15 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 22:51:58 +01:00
|
|
|
const json::value value{v};
|
|
|
|
return stringify(buf, value);
|
2017-11-16 02:25:15 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
size_t
|
2018-03-20 22:51:58 +01:00
|
|
|
ircd::json::serialized(const string_view &v)
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
if(v.empty() && defined(v))
|
2018-03-28 01:47:48 +02:00
|
|
|
return size(empty_string);
|
2017-09-08 16:47:07 +02:00
|
|
|
|
2018-03-20 22:51:58 +01:00
|
|
|
const json::value value{v};
|
|
|
|
return serialized(value);
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
enum ircd::json::type
|
|
|
|
ircd::json::type(const string_view &buf)
|
|
|
|
{
|
2018-05-20 07:11:39 +02:00
|
|
|
static const auto flag
|
|
|
|
{
|
|
|
|
qi::skip_flag::dont_postskip
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
enum type ret;
|
|
|
|
if(!qi::phrase_parse(begin(buf), end(buf), parser.type, parser.WS, flag, ret))
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Failed to get type from buffer"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
|
|
|
enum ircd::json::type
|
|
|
|
ircd::json::type(const string_view &buf,
|
|
|
|
std::nothrow_t)
|
|
|
|
{
|
2018-05-20 07:11:39 +02:00
|
|
|
static const auto flag
|
|
|
|
{
|
|
|
|
qi::skip_flag::dont_postskip
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
|
|
|
enum type ret;
|
|
|
|
if(!qi::phrase_parse(begin(buf), end(buf), parser.type, parser.WS, flag, ret))
|
|
|
|
return STRING;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-09-08 21:29:21 +02:00
|
|
|
|
|
|
|
ircd::string_view
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::reflect(const enum type &type)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
switch(type)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
case NUMBER: return "NUMBER";
|
|
|
|
case OBJECT: return "OBJECT";
|
|
|
|
case ARRAY: return "ARRAY";
|
|
|
|
case LITERAL: return "LITERAL";
|
|
|
|
case STRING: return "STRING";
|
2017-09-12 19:03:06 +02:00
|
|
|
}
|
2017-09-08 21:29:21 +02:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
return {};
|
2017-09-08 21:29:21 +02:00
|
|
|
}
|