2016-11-29 16:23:38 +01:00
|
|
|
/*
|
|
|
|
* charybdis: standing on the shoulders of giant build times
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Charybdis Development Team
|
|
|
|
* Copyright (C) 2017 Jason Volk <jason@zemos.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2017-03-16 21:35:37 +01:00
|
|
|
#include <ircd/spirit.h>
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT
|
|
|
|
(
|
2017-08-23 23:32:28 +02:00
|
|
|
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
|
|
|
)
|
2017-03-11 04:31:20 +01:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
namespace ircd {
|
|
|
|
namespace json {
|
|
|
|
|
|
|
|
namespace spirit = boost::spirit;
|
|
|
|
namespace ascii = spirit::ascii;
|
|
|
|
namespace karma = spirit::karma;
|
|
|
|
namespace qi = spirit::qi;
|
|
|
|
|
|
|
|
using spirit::unused_type;
|
|
|
|
|
|
|
|
using qi::lit;
|
|
|
|
using qi::char_;
|
2017-03-20 23:59:14 +01:00
|
|
|
using qi::long_;
|
2016-11-29 16:23:38 +01:00
|
|
|
using qi::double_;
|
|
|
|
using qi::raw;
|
|
|
|
using qi::omit;
|
|
|
|
using qi::matches;
|
|
|
|
using qi::hold;
|
|
|
|
using qi::eoi;
|
|
|
|
using qi::eps;
|
|
|
|
using qi::attr;
|
|
|
|
|
|
|
|
using karma::lit;
|
|
|
|
using karma::char_;
|
2017-03-20 23:59:14 +01:00
|
|
|
using karma::long_;
|
2017-03-16 21:36:18 +01:00
|
|
|
using karma::double_;
|
|
|
|
using karma::bool_;
|
2016-11-29 16:23:38 +01:00
|
|
|
using karma::maxwidth;
|
|
|
|
using karma::buffer;
|
|
|
|
using karma::eps;
|
2017-03-11 04:31:20 +01:00
|
|
|
using karma::attr_cast;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
template<class it>
|
|
|
|
struct 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" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
// literal
|
2017-03-20 23:59:14 +01:00
|
|
|
rule<string_view> lit_true { lit("true") ,"literal true" };
|
|
|
|
rule<string_view> lit_false { lit("false") ,"literal false" };
|
|
|
|
rule<string_view> lit_null { lit("null") ,"literal null" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-20 23:59:14 +01:00
|
|
|
rule<> quote { lit('"') ,"quote" };
|
|
|
|
rule<string_view> chars { raw[*(char_ - quote)] ,"characters" };
|
|
|
|
rule<string_view> string { quote >> chars >> quote ,"string" };
|
2017-04-03 05:55:23 +02:00
|
|
|
rule<string_view> name { quote >> raw[+(char_ - quote)] >> quote ,"name" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-20 23:59:14 +01:00
|
|
|
rule<string_view> boolean { lit_true | lit_false ,"boolean" };
|
|
|
|
rule<string_view> literal { lit_true | lit_false | lit_null ,"literal" };
|
|
|
|
rule<string_view> number { raw[double_] ,"number" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-18 04:32:32 +01:00
|
|
|
rule<string_view> array
|
|
|
|
{
|
|
|
|
array_begin >> -(omit[ws >> value >> ws] % value_sep) >> ws >> array_end
|
|
|
|
,"array"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-18 04:32:32 +01:00
|
|
|
rule<string_view> object
|
|
|
|
{
|
|
|
|
object_begin >> -(omit[ws >> member >> ws] % value_sep) >> ws >> object_end
|
|
|
|
,"object"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-18 04:32:32 +01:00
|
|
|
rule<string_view> value
|
|
|
|
{
|
|
|
|
lit_false | lit_true | lit_null | object | array | number | string
|
|
|
|
,"value"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
rule<object::member> member
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
|
|
|
name >> ws >> name_sep >> ws >> value
|
|
|
|
,"member"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<int> type
|
|
|
|
{
|
2017-03-25 00:46:15 +01:00
|
|
|
(omit[object_begin] >> attr(json::OBJECT)) |
|
|
|
|
(omit[array_begin] >> attr(json::ARRAY)) |
|
|
|
|
(omit[quote] >> attr(json::STRING)) |
|
|
|
|
(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<>{}}
|
|
|
|
{
|
|
|
|
array %= array_begin >> -(omit[ws >> value >> ws] % value_sep) >> ws >> array_end;
|
|
|
|
object %= object_begin >> -(omit[ws >> member >> ws] % value_sep) >> ws >> object_end;
|
|
|
|
}
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
template<class it>
|
|
|
|
struct output
|
|
|
|
: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" };
|
|
|
|
rule<> array_begin { lit('[') ,"array begin" };
|
|
|
|
rule<> array_end { lit(']') ,"array end" };
|
|
|
|
rule<> name_sep { lit(':') ,"name separator" };
|
|
|
|
rule<> value_sep { lit(',') ,"value separator" };
|
|
|
|
rule<> quote { lit('"') ,"quote" };
|
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" };
|
2017-03-20 23:59:14 +01:00
|
|
|
rule<string_view> chars { *(~char_('"')) ,"characters" };
|
2016-11-29 16:23:38 +01:00
|
|
|
rule<string_view> string { quote << chars << quote ,"string" };
|
2017-03-16 21:36:18 +01:00
|
|
|
rule<string_view> number { double_ ,"number" };
|
2017-04-03 05:55:23 +02:00
|
|
|
rule<string_view> name { quote << +(~char_('"')) << quote ,"name" };
|
2017-03-20 23:59:14 +01:00
|
|
|
rule<string_view> value { rule<string_view>{} /* subclass implemented */ ,"value" };
|
2017-09-08 11:17:06 +02:00
|
|
|
|
|
|
|
rule<const string_view &> elem
|
|
|
|
{
|
|
|
|
value
|
|
|
|
,"element"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<const json::array &> elems
|
|
|
|
{
|
|
|
|
-(value % value_sep)
|
|
|
|
,"elements"
|
|
|
|
};
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
rule<const json::array &> array
|
|
|
|
{
|
|
|
|
array_begin << elems << array_end
|
|
|
|
,"array"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
rule<const json::object::member &> member
|
|
|
|
{
|
|
|
|
name << name_sep << value
|
|
|
|
,"member"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<const json::object &> members
|
|
|
|
{
|
|
|
|
-(member % value_sep)
|
|
|
|
,"members"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<const json::object &> object
|
|
|
|
{
|
|
|
|
object_begin << members << object_end
|
|
|
|
,"object"
|
|
|
|
};
|
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
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace json {
|
|
|
|
|
|
|
|
struct parser
|
|
|
|
:input<const char *>
|
|
|
|
{
|
|
|
|
using input<const char *>::input;
|
|
|
|
}
|
|
|
|
const parser;
|
|
|
|
|
|
|
|
struct printer
|
|
|
|
:output<char *>
|
|
|
|
{
|
2017-03-31 00:55:17 +02:00
|
|
|
template<class generator,
|
|
|
|
class attribute>
|
|
|
|
bool operator()(char *&out, char *const &stop, generator&& gen, attribute&& a) const;
|
|
|
|
|
|
|
|
template<class generator>
|
|
|
|
bool operator()(char *&out, char *const &stop, generator&& gen) const;
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
template<class... args>
|
|
|
|
bool operator()(mutable_buffer &out, args&&... a) const
|
|
|
|
{
|
|
|
|
return operator()(begin(out), end(out), std::forward<args>(a)...);
|
|
|
|
}
|
|
|
|
|
2017-03-11 04:31:20 +01:00
|
|
|
printer();
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
const printer;
|
|
|
|
|
|
|
|
struct ostreamer
|
|
|
|
:output<karma::ostream_iterator<char>>
|
|
|
|
{
|
2017-03-11 04:31:20 +01:00
|
|
|
ostreamer();
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
const ostreamer;
|
|
|
|
|
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|
|
|
|
|
2017-03-23 22:58:24 +01:00
|
|
|
namespace ircd {
|
|
|
|
namespace json {
|
2017-03-31 00:55:17 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void
|
|
|
|
failed_to_serialize_object()
|
|
|
|
{
|
|
|
|
throw print_error("The JSON generator failed to serialize object");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
failed_to_stream_object()
|
|
|
|
{
|
|
|
|
throw print_error("The JSON generator failed to stream object");
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
} // namespace <anonymous>
|
2017-03-31 00:55:17 +02:00
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace db {
|
2017-03-23 22:58:24 +01:00
|
|
|
|
|
|
|
std::string
|
|
|
|
merge_operator(const string_view &key,
|
|
|
|
const std::pair<string_view, string_view> &delta)
|
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index index{delta.first};
|
|
|
|
index += delta.second;
|
|
|
|
return index;
|
2017-03-23 22:58:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace db
|
|
|
|
} // namespace ircd
|
|
|
|
|
2017-03-11 04:31:20 +01:00
|
|
|
ircd::json::printer::printer()
|
|
|
|
{
|
|
|
|
const auto recursor([this](auto &a, auto &b, auto &c)
|
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto recurse_array([&a]
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
mutable_buffer mb
|
|
|
|
{
|
|
|
|
const_cast<char *>(a.data()), a.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto r(stringify(mb, json::array(a)));
|
|
|
|
a.resize(r.size());
|
2017-03-18 04:32:32 +01:00
|
|
|
});
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto recurse_object([&a]
|
2017-03-11 04:31:20 +01:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
mutable_buffer mb
|
|
|
|
{
|
|
|
|
const_cast<char *>(a.data()), a.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto r(stringify(mb, json::object(a)));
|
|
|
|
a.resize(r.size());
|
2017-03-11 04:31:20 +01:00
|
|
|
});
|
|
|
|
|
2017-03-16 21:36:18 +01:00
|
|
|
const auto quote_string([&]
|
|
|
|
{
|
|
|
|
a.insert(a.end(), '"');
|
|
|
|
a.insert(a.begin(), '"');
|
|
|
|
});
|
|
|
|
|
2017-03-11 04:31:20 +01:00
|
|
|
if(likely(!a.empty())) switch(a.front())
|
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
case '{': recurse_object(); break;
|
2017-03-18 04:32:32 +01:00
|
|
|
case '[': recurse_array(); break;
|
2017-03-16 21:36:18 +01:00
|
|
|
case '"': break;
|
|
|
|
case '0': break;
|
|
|
|
case '1': break;
|
|
|
|
case '2': break;
|
|
|
|
case '3': break;
|
|
|
|
case '4': break;
|
|
|
|
case '5': break;
|
|
|
|
case '6': break;
|
|
|
|
case '7': break;
|
|
|
|
case '8': break;
|
|
|
|
case '9': break;
|
|
|
|
case 't':
|
|
|
|
case 'f':
|
|
|
|
case 'n':
|
|
|
|
if(a == "true" || a == "false" || a == "null")
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
quote_string();
|
|
|
|
break;
|
2017-03-11 04:31:20 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
value %= karma::string[recursor];
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
template<class gen,
|
|
|
|
class attr>
|
|
|
|
bool
|
|
|
|
ircd::json::printer::operator()(char *&out,
|
|
|
|
char *const &stop,
|
|
|
|
gen&& g,
|
|
|
|
attr&& a)
|
|
|
|
const
|
2017-03-11 04:31:20 +01:00
|
|
|
{
|
2017-03-31 00:55:17 +02:00
|
|
|
const auto gg
|
2017-03-11 04:31:20 +01:00
|
|
|
{
|
2017-08-23 23:02:24 +02:00
|
|
|
maxwidth(size_t(stop - out))[std::forward<gen>(g)] | eps[failed_to_serialize_object]
|
2017-03-31 00:55:17 +02:00
|
|
|
};
|
2017-03-16 21:36:18 +01:00
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
return karma::generate(out, gg, std::forward<attr>(a));
|
|
|
|
}
|
2017-03-16 21:36:18 +01:00
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
template<class gen>
|
|
|
|
bool
|
|
|
|
ircd::json::printer::operator()(char *&out,
|
|
|
|
char *const &stop,
|
|
|
|
gen&& g)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto gg
|
|
|
|
{
|
2017-08-23 23:02:24 +02:00
|
|
|
maxwidth(size_t(stop - out))[std::forward<gen>(g)] | eps[failed_to_serialize_object]
|
2017-03-31 00:55:17 +02:00
|
|
|
};
|
2017-03-11 04:31:20 +01:00
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
return karma::generate(out, gg);
|
2017-03-11 04:31:20 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
ircd::string_view
|
2017-09-08 16:47:07 +02:00
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const std::vector<json::object> &v)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto print_member([&buf](const string_view &elem)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
//printer(buf, printer.elem, elem);
|
|
|
|
const auto cpsz(std::min(size(buf), elem.size()));
|
|
|
|
memcpy(begin(buf), elem.data(), cpsz);
|
|
|
|
begin(buf) += cpsz;
|
2017-09-08 10:41:02 +02:00
|
|
|
});
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
char *const start(begin(buf));
|
|
|
|
printer(buf, printer.array_begin);
|
2017-09-08 10:41:02 +02:00
|
|
|
|
|
|
|
auto it(std::begin(v));
|
|
|
|
if(it != std::end(v))
|
|
|
|
{
|
|
|
|
print_member(*it);
|
|
|
|
for(++it; it != std::end(v); ++it)
|
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
printer(buf, printer.value_sep);
|
2017-09-08 10:41:02 +02:00
|
|
|
print_member(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
printer(buf, printer.array_end);
|
|
|
|
return string_view{start, begin(buf)};
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
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 index &obj)
|
|
|
|
{
|
|
|
|
const member *const &begin(&obj.idx.front());
|
|
|
|
const member *const &end(begin + obj.idx.size());
|
|
|
|
return stringify(buf, begin, end);
|
|
|
|
}
|
|
|
|
|
2017-09-08 21:29:21 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const member &m)
|
|
|
|
{
|
|
|
|
return stringify(buf, &m, &m + 1);
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const member *const &begin,
|
|
|
|
const member *const &end)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
|
|
|
const auto num(std::distance(begin, end));
|
2017-09-08 16:47:07 +02:00
|
|
|
const member *vec[num];
|
2017-09-08 10:41:02 +02:00
|
|
|
for(auto i(0); i < num; ++i)
|
|
|
|
vec[i] = begin + i;
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
return stringify(buf, vec, vec + num);
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const member *const *const &b,
|
|
|
|
const member *const *const &e)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto print_string([&buf](const value &value)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
printer(buf, printer.string, string_view{value});
|
2017-03-20 23:59:14 +01:00
|
|
|
});
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto print_literal([&buf](const value &value)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
printer(buf, printer.literal, string_view{value});
|
2017-03-16 21:36:18 +01:00
|
|
|
});
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto print_object([&buf](const value &value)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
if(value.serial)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
printer(buf, printer.object, string_view{value});
|
2017-03-16 21:36:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
assert(value.object);
|
2017-09-08 16:47:07 +02:00
|
|
|
stringify(buf, *value.object);
|
2017-03-16 21:36:18 +01:00
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto print_array([&buf](const value &value)
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
if(value.serial)
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2017-09-08 11:17:06 +02:00
|
|
|
//printer(out, stop, printer.array, value);
|
|
|
|
const string_view data(value);
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto cpsz(std::min(size(buf), data.size()));
|
|
|
|
memcpy(begin(buf), data.data(), cpsz);
|
|
|
|
begin(buf) += cpsz;
|
2017-03-18 04:32:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-20 23:59:14 +01:00
|
|
|
assert(0);
|
2017-08-23 23:32:28 +02:00
|
|
|
//assert(value.object);
|
|
|
|
//serialize(*value.object, out, stop);
|
2017-03-18 04:32:32 +01:00
|
|
|
});
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto print_number([&buf](const value &value)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
if(value.serial)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
if(value.floats)
|
2017-09-08 16:47:07 +02:00
|
|
|
printer(buf, double_, string_view{value});
|
2017-03-20 23:59:14 +01:00
|
|
|
else
|
2017-09-08 16:47:07 +02:00
|
|
|
printer(buf, long_, string_view{value});
|
2017-03-20 23:59:14 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
if(value.floats)
|
2017-09-08 16:47:07 +02:00
|
|
|
printer(buf, double_, value.floating);
|
2017-03-20 23:59:14 +01:00
|
|
|
else
|
2017-09-08 16:47:07 +02:00
|
|
|
printer(buf, long_, value.integer);
|
2017-03-20 23:59:14 +01:00
|
|
|
});
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto print_member([&](const member &member)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
printer(buf, printer.name << printer.name_sep, member.first);
|
2017-03-16 21:36:18 +01:00
|
|
|
|
|
|
|
switch(member.second.type)
|
|
|
|
{
|
|
|
|
case STRING: print_string(member.second); break;
|
2017-03-18 04:32:32 +01:00
|
|
|
case OBJECT: print_object(member.second); break;
|
|
|
|
case ARRAY: print_array(member.second); break;
|
2017-03-20 23:59:14 +01:00
|
|
|
case NUMBER: print_number(member.second); break;
|
|
|
|
case LITERAL: print_literal(member.second); break;
|
2017-03-16 21:36:18 +01:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
});
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
char *const start(begin(buf));
|
|
|
|
printer(buf, printer.object_begin);
|
2017-03-16 21:36:18 +01:00
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
auto it(b);
|
|
|
|
if(it != e)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-08 10:41:02 +02:00
|
|
|
print_member(**it);
|
2017-09-08 16:47:07 +02:00
|
|
|
for(++it; it != e; ++it)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
printer(buf, printer.value_sep);
|
2017-09-08 10:41:02 +02:00
|
|
|
print_member(**it);
|
2017-03-16 21:36:18 +01:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
printer(buf, printer.object_end);
|
|
|
|
return string_view{start, begin(buf)};
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
ircd::json::ostreamer::ostreamer()
|
|
|
|
{
|
|
|
|
const auto recursor([this](auto &a, auto &b, auto &c)
|
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto recurse_array([&a]
|
2017-03-31 00:55:17 +02:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
mutable_buffer mb
|
|
|
|
{
|
|
|
|
const_cast<char *>(a.data()), a.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto r(stringify(mb, json::array(a)));
|
|
|
|
a.resize(r.size());
|
2017-03-31 00:55:17 +02:00
|
|
|
});
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto recurse_object([&a]
|
2017-03-31 00:55:17 +02:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
mutable_buffer mb
|
|
|
|
{
|
|
|
|
const_cast<char *>(a.data()), a.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto r(stringify(mb, json::object(a)));
|
|
|
|
a.resize(r.size());
|
2017-03-31 00:55:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const auto quote_string([&]
|
|
|
|
{
|
|
|
|
a.insert(a.end(), '"');
|
|
|
|
a.insert(a.begin(), '"');
|
|
|
|
});
|
|
|
|
|
|
|
|
if(likely(!a.empty())) switch(a.front())
|
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
case '{': recurse_object(); break;
|
2017-03-31 00:55:17 +02:00
|
|
|
case '[': recurse_array(); break;
|
|
|
|
case '"': break;
|
|
|
|
case '0': break;
|
|
|
|
case '1': break;
|
|
|
|
case '2': break;
|
|
|
|
case '3': break;
|
|
|
|
case '4': break;
|
|
|
|
case '5': break;
|
|
|
|
case '6': break;
|
|
|
|
case '7': break;
|
|
|
|
case '8': break;
|
|
|
|
case '9': break;
|
|
|
|
case 't':
|
|
|
|
case 'f':
|
|
|
|
case 'n':
|
|
|
|
if(a == "true" || a == "false" || a == "null")
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
quote_string();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
value %= karma::string[recursor];
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
std::ostream &
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<<(std::ostream &s, const index &obj)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-16 21:36:18 +01:00
|
|
|
karma::ostream_iterator<char> osi(s);
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
const auto stream_string([&osi](const value &value)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
karma::generate(osi, ostreamer.string, string_view{value});
|
2017-03-20 23:59:14 +01:00
|
|
|
});
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
const auto stream_literal([&osi](const value &value)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-08-29 02:34:08 +02:00
|
|
|
karma::generate(osi, ostreamer.literal, string_view{value});
|
2017-03-16 21:36:18 +01:00
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
const auto stream_object([&osi, &s](const value &value)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
if(value.serial)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
karma::generate(osi, ostreamer.object, string_view{value});
|
2017-03-16 21:36:18 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-03-11 04:31:20 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
assert(value.object);
|
|
|
|
s << *value.object;
|
2017-03-16 21:36:18 +01:00
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
const auto stream_array([&osi](const value &value)
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
if(value.serial)
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
karma::generate(osi, ostreamer.array, string_view{value});
|
2017-03-18 04:32:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0);
|
2017-08-23 23:32:28 +02:00
|
|
|
//assert(value.object);
|
|
|
|
//s << *value.object;
|
2017-03-18 04:32:32 +01:00
|
|
|
});
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
const auto stream_number([&osi](const value &value)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
if(value.serial)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
if(value.floats)
|
|
|
|
karma::generate(osi, double_, string_view{value});
|
2017-03-20 23:59:14 +01:00
|
|
|
else
|
2017-08-23 23:32:28 +02:00
|
|
|
karma::generate(osi, long_, string_view{value});
|
2017-03-20 23:59:14 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
if(value.floats)
|
|
|
|
karma::generate(osi, double_, value.floating);
|
2017-03-20 23:59:14 +01:00
|
|
|
else
|
2017-08-23 23:32:28 +02:00
|
|
|
karma::generate(osi, long_, value.integer);
|
2017-03-20 23:59:14 +01:00
|
|
|
});
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
const auto stream_member([&](const member &member)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
|
|
|
karma::generate(osi, ostreamer.name << ostreamer.name_sep, string_view(member.first));
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-16 21:36:18 +01:00
|
|
|
switch(member.second.type)
|
|
|
|
{
|
2017-03-20 23:59:14 +01:00
|
|
|
case STRING: stream_string(member.second); break;
|
|
|
|
case OBJECT: stream_object(member.second); break;
|
|
|
|
case ARRAY: stream_array(member.second); break;
|
|
|
|
case NUMBER: stream_number(member.second); break;
|
|
|
|
case LITERAL: stream_literal(member.second); break;
|
2017-03-16 21:36:18 +01:00
|
|
|
}
|
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-16 21:36:18 +01:00
|
|
|
karma::generate(osi, ostreamer.object_begin);
|
|
|
|
|
|
|
|
auto it(begin(obj));
|
|
|
|
if(it != end(obj))
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-16 21:36:18 +01:00
|
|
|
stream_member(*it);
|
|
|
|
for(++it; it != end(obj); ++it)
|
|
|
|
{
|
|
|
|
karma::generate(osi, ostreamer.value_sep);
|
|
|
|
stream_member(*it);
|
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-16 21:36:18 +01:00
|
|
|
karma::generate(osi, ostreamer.object_end);
|
|
|
|
return s;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02: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)
|
|
|
|
{
|
|
|
|
const size_t ret(1 + !std::distance(begin, end));
|
|
|
|
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 + 1 + serialized(member.second);
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index
|
|
|
|
ircd::json::operator+(const object &left, const object &right)
|
2017-03-21 09:54:10 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
index ret(left);
|
2017-03-31 00:55:17 +02:00
|
|
|
ret += right;
|
|
|
|
return ret;
|
2017-03-21 09:54:10 +01:00
|
|
|
}
|
2017-03-31 00:55:17 +02:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index &
|
|
|
|
ircd::json::operator+=(index &left, const object &right)
|
2017-03-21 09:54:10 +01:00
|
|
|
{
|
2017-03-31 00:55:17 +02:00
|
|
|
for(const auto &b : right)
|
|
|
|
{
|
|
|
|
const auto deletion
|
|
|
|
{
|
|
|
|
// empty replacement value indicates deletion
|
|
|
|
b.second.empty() ||
|
|
|
|
b.second == "\"\"" ||
|
|
|
|
b.second == "{}" ||
|
|
|
|
b.second == "[]"
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto it(left.find(b.first));
|
|
|
|
if(it == left.end())
|
|
|
|
{
|
|
|
|
if(deletion) // prevents adding the empty indicator as the new key!
|
|
|
|
continue;
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
left.idx.emplace_back(member{b});
|
2017-03-31 00:55:17 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(deletion)
|
2017-03-21 09:54:10 +01:00
|
|
|
{
|
2017-03-31 00:55:17 +02:00
|
|
|
left.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
auto &a(const_cast<member &>(*it));
|
2017-03-31 00:55:17 +02:00
|
|
|
switch(type(a.second))
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
a = member{b};
|
2017-03-31 00:55:17 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge recursively
|
|
|
|
case OBJECT:
|
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
auto merged(std::make_unique<index>(json::object{a.second}));
|
|
|
|
*merged += json::object{b.second};
|
2017-03-31 00:55:17 +02:00
|
|
|
if(merged->empty())
|
|
|
|
{
|
|
|
|
// Child merge was empty value. Now we can also remove this empty parent.
|
|
|
|
left.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
a = member{string_view{a.first}, std::move(merged)};
|
2017-03-31 00:55:17 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-03-21 09:54:10 +01:00
|
|
|
}
|
2017-03-31 00:55:17 +02:00
|
|
|
}
|
2017-03-21 09:54:10 +01:00
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
return left;
|
2017-03-21 09:54:10 +01:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::index(recursive_t recursive,
|
|
|
|
const object &object)
|
|
|
|
:idx{object.count()}
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
std::transform(std::begin(object), std::end(object), std::begin(idx), [&]
|
2017-09-08 16:47:07 +02:00
|
|
|
(const object::member &m) -> member
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
|
|
|
switch(type(m.second))
|
|
|
|
{
|
|
|
|
case OBJECT:
|
2017-09-08 16:47:07 +02:00
|
|
|
return member{m.first, std::make_unique<index>(recursive, m.second)};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
|
|
|
default:
|
2017-09-08 16:47:07 +02:00
|
|
|
return member{m};
|
2017-03-20 23:59:14 +01:00
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
});
|
2017-03-31 00:55:17 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::index(const object &object)
|
|
|
|
:idx{object.count()}
|
2017-03-31 00:55:17 +02:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
std::transform(std::begin(object), std::end(object), std::begin(idx), []
|
2017-09-08 16:47:07 +02:00
|
|
|
(const object::member &m) -> member
|
2017-03-31 00:55:17 +02:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
return member{m};
|
2017-03-31 00:55:17 +02:00
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::index(std::initializer_list<member> builder)
|
2017-03-20 23:59:14 +01:00
|
|
|
:idx{builder.size()}
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-20 23:59:14 +01:00
|
|
|
std::transform(std::begin(builder), std::end(builder), std::begin(idx), []
|
|
|
|
(auto&& m)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-20 23:59:14 +01:00
|
|
|
return std::move(const_cast<member &>(m));
|
2017-03-16 21:36:18 +01:00
|
|
|
});
|
2017-04-03 05:58:30 +02:00
|
|
|
|
|
|
|
const auto empty([](const auto &member)
|
|
|
|
{
|
|
|
|
return member.first.empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
idx.erase(std::remove_if(std::begin(idx), std::end(idx), empty), idx.end());
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-16 21:36:18 +01:00
|
|
|
bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::erase(const string_view &name)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-16 21:36:18 +01:00
|
|
|
const auto it(find(name));
|
|
|
|
if(it == end())
|
|
|
|
return false;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-16 21:36:18 +01:00
|
|
|
erase(it);
|
|
|
|
return true;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-16 21:36:18 +01:00
|
|
|
void
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::erase(const const_iterator &it)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-16 21:36:18 +01:00
|
|
|
idx.erase(it);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::const_iterator
|
|
|
|
ircd::json::index::erase(const const_iterator &start,
|
|
|
|
const const_iterator &stop)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-16 21:36:18 +01:00
|
|
|
return { idx.erase(start, stop) };
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
const ircd::json::value &
|
|
|
|
ircd::json::index::at(const string_view &path)
|
2017-03-20 23:59:14 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto elem(split(path, '.'));
|
|
|
|
const auto ident(split(elem.first, '['));
|
|
|
|
const auto &name(ident.first);
|
|
|
|
const auto &indice(ident.second);
|
|
|
|
|
|
|
|
const auto it(find(name));
|
|
|
|
if(unlikely(it == end()))
|
|
|
|
throw not_found("'%s'", name);
|
|
|
|
|
|
|
|
const auto &val(it->second);
|
|
|
|
if(!indice.empty())
|
|
|
|
{
|
2017-08-23 23:02:24 +02:00
|
|
|
const auto idx(lex_cast<size_t>(rstrip(indice, ']')));
|
2017-03-20 23:59:14 +01:00
|
|
|
|
|
|
|
if(type(val) != ARRAY)
|
|
|
|
throw not_found("cannot recurse through non-array \"%s\" for indice [%zu]", name, idx);
|
|
|
|
|
|
|
|
if(val.serial)
|
|
|
|
throw not_found("cannot recurse through unparsed array \"%s\" for indice [%zu]", name, idx);
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
//return val.array->at(idx);
|
|
|
|
}
|
|
|
|
else if(!elem.second.empty())
|
|
|
|
{
|
|
|
|
if(type(val) != OBJECT)
|
|
|
|
throw not_found("cannot recurse through non-object \"%s\" for \"%s\"", name, elem.second);
|
|
|
|
|
|
|
|
if(val.serial)
|
|
|
|
throw not_found("cannot recurse through unparsed object \"%s\" for \"%s\"", name, elem.second);
|
|
|
|
|
|
|
|
return val.object->at(elem.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
size_t
|
2017-09-08 16:47:07 +02:00
|
|
|
ircd::json::serialized(const index &index)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
return index.serialized();
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
2017-03-16 21:36:18 +01:00
|
|
|
size_t
|
2017-09-08 16:47:07 +02:00
|
|
|
ircd::json::index::serialized()
|
2016-11-29 16:23:38 +01:00
|
|
|
const
|
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
const member *const &begin(&idx.front());
|
|
|
|
const member *const &end(begin + idx.size());
|
|
|
|
return json::serialized(begin, end);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::operator std::string()
|
2016-11-29 16:23:38 +01:00
|
|
|
const
|
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
const member *const &begin(&idx.front());
|
|
|
|
const member *const &end(begin + idx.size());
|
|
|
|
return json::string(begin, end);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/value.h
|
|
|
|
//
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::value::~value()
|
2017-03-16 21:36:18 +01:00
|
|
|
noexcept
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-18 04:32:32 +01:00
|
|
|
switch(type)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
|
|
|
case STRING: if(alloc) delete[] string; break;
|
2017-03-18 04:32:32 +01:00
|
|
|
case OBJECT: if(alloc) delete object; break;
|
|
|
|
//case ARRAY: if(alloc) delete array; break;
|
2017-03-16 21:36:18 +01:00
|
|
|
default: break;
|
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::value::operator std::string()
|
2017-03-16 21:36:18 +01:00
|
|
|
const
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-16 21:36:18 +01:00
|
|
|
switch(type)
|
|
|
|
{
|
2017-03-18 04:32:32 +01:00
|
|
|
case STRING:
|
2017-03-20 23:59:14 +01:00
|
|
|
case LITERAL:
|
2017-03-18 04:32:32 +01:00
|
|
|
return std::string(unquote(string_view(*this)));
|
|
|
|
|
|
|
|
case OBJECT:
|
|
|
|
if(serial)
|
|
|
|
return std::string(string_view(*this));
|
|
|
|
else
|
|
|
|
return std::string(*object);
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
if(serial)
|
|
|
|
return std::string(string_view(*this));
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
2017-03-20 23:59:14 +01:00
|
|
|
case NUMBER:
|
|
|
|
if(serial)
|
|
|
|
return std::string(string_view(*this));
|
|
|
|
else if(floats)
|
|
|
|
return std::string(lex_cast(floating));
|
|
|
|
else
|
|
|
|
return std::string(lex_cast(integer));
|
2017-03-16 21:36:18 +01:00
|
|
|
}
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
throw type_error("cannot index type[%d]", int(type));
|
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});
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
case OBJECT:
|
2017-03-20 23:59:14 +01:00
|
|
|
case NUMBER:
|
|
|
|
case LITERAL:
|
2017-03-18 04:32:32 +01:00
|
|
|
if(serial)
|
|
|
|
return string_view{string, len};
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
return lex_cast<int64_t>(string_view(*this));
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
case OBJECT:
|
|
|
|
case LITERAL:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw type_error("value type[%d] is not an int64_t", int(type));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::operator double()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case NUMBER:
|
|
|
|
return likely(floats)? floating : integer;
|
|
|
|
|
|
|
|
case STRING:
|
|
|
|
return lex_cast<double>(string_view(*this));
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
case OBJECT:
|
|
|
|
case LITERAL:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw type_error("value type[%d] is not a float", int(type));
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
case NUMBER: return lex_cast(integer).empty();
|
|
|
|
case STRING: return !len;
|
|
|
|
case OBJECT: return serial? !len : object? object->empty() : true;
|
|
|
|
case ARRAY: return serial? !len : array? false : true; //TODO: XXX arr
|
|
|
|
case LITERAL: return !len;
|
|
|
|
};
|
|
|
|
|
|
|
|
throw type_error("deciding if a type[%u] is empty is undefined", int(type));
|
|
|
|
}
|
|
|
|
|
2017-03-16 21:36:18 +01:00
|
|
|
size_t
|
2017-09-08 16:47:07 +02:00
|
|
|
ircd::json::serialized(const value &value)
|
|
|
|
{
|
|
|
|
return value.serialized();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::value::serialized()
|
2017-03-16 21:36:18 +01:00
|
|
|
const
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-16 21:36:18 +01:00
|
|
|
switch(type)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-20 23:59:14 +01:00
|
|
|
case NUMBER: return lex_cast(integer).size();
|
|
|
|
case STRING: return 1 + len + 1;
|
2017-09-08 16:47:07 +02:00
|
|
|
case OBJECT: return serial? len : object->serialized();
|
2017-03-20 23:59:14 +01:00
|
|
|
case ARRAY: return serial? len : 2;
|
|
|
|
case LITERAL: return len;
|
2017-03-16 21:36:18 +01:00
|
|
|
};
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2017-04-03 05:54:35 +02:00
|
|
|
throw type_error("deciding the size of a type[%u] is undefined", int(type));
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-20 23:59:14 +01:00
|
|
|
std::ostream &
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<<(std::ostream &s, const value &v)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
|
|
|
switch(v.type)
|
|
|
|
{
|
|
|
|
case STRING:
|
|
|
|
case LITERAL:
|
|
|
|
s << string_view(v);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OBJECT:
|
|
|
|
if(v.serial)
|
|
|
|
s << string_view(v);
|
|
|
|
else
|
|
|
|
s << *v.object;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
if(v.serial)
|
|
|
|
s << string_view(v);
|
|
|
|
else
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NUMBER:
|
|
|
|
if(v.serial)
|
|
|
|
s << string_view(v);
|
|
|
|
else if(v.floats)
|
|
|
|
s << v.floating;
|
|
|
|
else
|
|
|
|
s << v.integer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if(unlikely(type(a) != STRING || type(b) != STRING))
|
|
|
|
throw type_error("cannot compare values");
|
|
|
|
|
|
|
|
return static_cast<string_view>(a) > static_cast<string_view>(b);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if(unlikely(type(a) != STRING || type(b) != STRING))
|
|
|
|
throw type_error("cannot compare values");
|
|
|
|
|
|
|
|
return static_cast<string_view>(a) < static_cast<string_view>(b);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if(unlikely(type(a) != STRING || type(b) != STRING))
|
|
|
|
throw type_error("cannot compare values");
|
|
|
|
|
|
|
|
return static_cast<string_view>(a) >= static_cast<string_view>(b);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if(unlikely(type(a) != STRING || type(b) != STRING))
|
|
|
|
throw type_error("cannot compare values");
|
|
|
|
|
|
|
|
return static_cast<string_view>(a) <= static_cast<string_view>(b);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if(unlikely(type(a) != STRING || type(b) != STRING))
|
|
|
|
throw type_error("cannot compare values");
|
|
|
|
|
|
|
|
return static_cast<string_view>(a) != static_cast<string_view>(b);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if(unlikely(type(a) != STRING || type(b) != STRING))
|
|
|
|
throw type_error("cannot compare values");
|
|
|
|
|
|
|
|
return static_cast<string_view>(a) == static_cast<string_view>(b);
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const object &object)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
|
|
|
static const auto throws([]
|
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
throw print_error("The JSON generator failed to print object");
|
2017-03-16 21:36:18 +01:00
|
|
|
});
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
char *const start(begin(buf));
|
|
|
|
karma::generate(begin(buf), maxwidth(size(buf))[printer.object] | eps[throws], object);
|
|
|
|
|
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
2017-03-16 21:36:18 +01:00
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
std::ostream &
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<<(std::ostream &s, const object &object)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-11 04:31:20 +01:00
|
|
|
const auto &os(ostreamer);
|
2016-11-29 16:23:38 +01:00
|
|
|
static const auto throws([]
|
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
throw print_error("The JSON generator failed to output object to stream");
|
2016-11-29 16:23:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
karma::ostream_iterator<char> osi(s);
|
2017-08-23 23:32:28 +02:00
|
|
|
karma::generate(osi, os.object | eps[throws], object);
|
2016-11-29 16:23:38 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<<(std::ostream &s, const object::member &member)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
static const auto throws([]
|
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
throw print_error("The JSON generator failed to output object member to stream");
|
2016-11-29 16:23:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
karma::ostream_iterator<char> osi(s);
|
2017-03-16 21:36:18 +01:00
|
|
|
karma::generate(osi, ostreamer.member | eps[throws], member);
|
2016-11-29 16:23:38 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::object::const_iterator &
|
|
|
|
ircd::json::object::const_iterator::operator++()
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
static const qi::rule<const char *, json::object::member> member
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
parser.name >> parser.ws >> parser.name_sep >> parser.ws >> raw[parser.value]
|
|
|
|
};
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
static const qi::rule<const char *, json::object::member> parse_next
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
parser.object_end | (parser.value_sep >> parser.ws >> member)
|
|
|
|
};
|
|
|
|
|
|
|
|
state.first = string_view{};
|
|
|
|
state.second = string_view{};
|
|
|
|
if(!qi::phrase_parse(start, stop, parse_next, parser.WS, state))
|
|
|
|
start = stop;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::object::operator std::string()
|
2017-03-20 23:59:14 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
//TODO: tmp
|
|
|
|
std::stringstream ret;
|
|
|
|
ret << (*this);
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::object::const_iterator
|
|
|
|
ircd::json::object::begin()
|
2016-11-29 16:23:38 +01:00
|
|
|
const
|
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
static const qi::rule<const char *, json::object::member> member
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
parser.name >> parser.ws >> parser.name_sep >> parser.ws >> raw[parser.value]
|
|
|
|
};
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
static const qi::rule<const char *, json::object::member> parse_begin
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
parser.object_begin >> parser.ws >> (parser.object_end | member)
|
|
|
|
};
|
|
|
|
|
2017-03-18 04:32:32 +01:00
|
|
|
const_iterator ret(string_view::begin(), string_view::end());
|
2016-11-29 16:23:38 +01:00
|
|
|
if(!qi::phrase_parse(ret.start, ret.stop, parse_begin, parser.WS, ret.state))
|
|
|
|
ret.start = ret.stop;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::object::const_iterator
|
|
|
|
ircd::json::object::end()
|
2016-11-29 16:23:38 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return { string_view::end(), string_view::end() };
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &b,
|
|
|
|
const array &a)
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
|
|
|
static const auto throws([]
|
|
|
|
{
|
|
|
|
throw print_error("The JSON generator failed to print array");
|
|
|
|
});
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
char *const start(begin(b));
|
|
|
|
karma::generate(begin(b), maxwidth(size(b))[printer.array_begin] | eps[throws]);
|
2017-03-20 23:59:14 +01:00
|
|
|
|
|
|
|
auto it(begin(a));
|
|
|
|
if(it != end(a))
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
karma::generate(begin(b), maxwidth(size(b))[printer.elem] | eps[throws], *it);
|
2017-03-20 23:59:14 +01:00
|
|
|
for(++it; it != end(a); ++it)
|
2017-09-08 16:47:07 +02:00
|
|
|
karma::generate(begin(b), maxwidth(size(b))[printer.value_sep << printer.elem] | eps[throws], *it);
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
karma::generate(begin(b), maxwidth(size(b))[printer.array_end] | eps[throws]);
|
|
|
|
return string_view{start, begin(b)};
|
2017-03-18 04:32:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<<(std::ostream &s, const array &a)
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
|
|
|
const auto &os(ostreamer);
|
|
|
|
static const auto throws([]
|
|
|
|
{
|
|
|
|
throw print_error("The JSON generator failed to output array to stream");
|
|
|
|
});
|
|
|
|
|
|
|
|
karma::ostream_iterator<char> osi(s);
|
2017-03-20 23:59:14 +01:00
|
|
|
karma::generate(osi, ostreamer.array_begin | eps[throws]);
|
|
|
|
|
|
|
|
auto it(begin(a));
|
|
|
|
if(it != end(a))
|
|
|
|
{
|
|
|
|
karma::generate(osi, ostreamer.elem | eps[throws], *it);
|
|
|
|
for(++it; it != end(a); ++it)
|
|
|
|
karma::generate(osi, (ostreamer.value_sep << ostreamer.elem) | eps[throws], *it);
|
|
|
|
}
|
|
|
|
|
|
|
|
karma::generate(osi, ostreamer.array_end | eps[throws]);
|
2017-03-18 04:32:32 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::array::const_iterator &
|
|
|
|
ircd::json::array::const_iterator::operator++()
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
static const qi::rule<const char *, string_view> parse_next
|
|
|
|
{
|
2017-03-18 04:32:32 +01:00
|
|
|
parser.array_end | (parser.value_sep >> parser.ws >> raw[parser.value])
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
state = string_view{};
|
|
|
|
if(!qi::phrase_parse(start, stop, parse_next, parser.WS, state))
|
|
|
|
start = stop;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::array::operator std::string()
|
2017-03-20 23:59:14 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
//TODO: tmp
|
|
|
|
std::stringstream ret;
|
|
|
|
ret << (*this);
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::array::const_iterator
|
|
|
|
ircd::json::array::begin()
|
2016-11-29 16:23:38 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
static const qi::rule<const char *, string_view> parse_begin
|
|
|
|
{
|
|
|
|
parser.array_begin >> parser.ws >> (parser.array_end | raw[parser.value])
|
|
|
|
};
|
|
|
|
|
2017-03-18 04:32:32 +01:00
|
|
|
const_iterator ret(string_view::begin(), string_view::end());
|
2016-11-29 16:23:38 +01:00
|
|
|
if(!qi::phrase_parse(ret.start, ret.stop, parse_begin, parser.WS, ret.state))
|
|
|
|
ret.start = ret.stop;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::array::const_iterator
|
|
|
|
ircd::json::array::end()
|
2016-11-29 16:23:38 +01:00
|
|
|
const
|
|
|
|
{
|
2017-03-18 04:32:32 +01:00
|
|
|
return { string_view::end(), string_view::end() };
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const string_view &s)
|
|
|
|
{
|
|
|
|
switch(type(s, std::nothrow))
|
|
|
|
{
|
|
|
|
case NUMBER:
|
|
|
|
case OBJECT:
|
|
|
|
case ARRAY:
|
|
|
|
case LITERAL:
|
|
|
|
return s.size();
|
|
|
|
|
|
|
|
case STRING:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ret(s.size());
|
|
|
|
ret += !startswith(s, '"');
|
|
|
|
ret += !endswith(s, '"');
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
enum ircd::json::type
|
|
|
|
ircd::json::type(const string_view &buf)
|
|
|
|
{
|
|
|
|
static const auto flag(qi::skip_flag::dont_postskip);
|
|
|
|
|
|
|
|
enum type ret;
|
|
|
|
if(!qi::phrase_parse(begin(buf), end(buf), parser.type, parser.WS, flag, ret))
|
|
|
|
throw type_error("Failed to get type from buffer");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
|
|
|
enum ircd::json::type
|
|
|
|
ircd::json::type(const string_view &buf,
|
|
|
|
std::nothrow_t)
|
|
|
|
{
|
|
|
|
static const auto flag(qi::skip_flag::dont_postskip);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2017-09-09 21:20:00 +02:00
|
|
|
// iov.h
|
2017-09-08 21:29:21 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &head,
|
2017-09-09 21:20:00 +02:00
|
|
|
const iov &iov)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
2017-09-09 21:20:00 +02:00
|
|
|
const auto num{count(iov)};
|
2017-09-08 21:29:21 +02:00
|
|
|
const member *m[num];
|
|
|
|
|
|
|
|
size_t i(0);
|
2017-09-09 21:20:00 +02:00
|
|
|
for_each(iov, [&i, &m]
|
2017-09-08 21:29:21 +02:00
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
m[i++] = &member;
|
|
|
|
});
|
|
|
|
|
|
|
|
return stringify(head, m, m + num);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2017-09-09 21:20:00 +02:00
|
|
|
ircd::json::count(const iov &iov)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
2017-09-09 21:20:00 +02:00
|
|
|
return count(iov, []
|
2017-09-08 21:29:21 +02:00
|
|
|
(const auto &)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2017-09-09 21:20:00 +02:00
|
|
|
ircd::json::count(const iov &iov,
|
2017-09-08 21:29:21 +02:00
|
|
|
const member_closure_bool &closure)
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
2017-09-09 21:20:00 +02:00
|
|
|
for_each(iov, [&closure, &ret]
|
2017-09-08 21:29:21 +02:00
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
ret += closure(member);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-09-09 21:20:00 +02:00
|
|
|
ircd::json::for_each(const iov &b,
|
2017-09-08 21:29:21 +02:00
|
|
|
const member_closure &closure)
|
|
|
|
{
|
2017-09-09 21:20:00 +02:00
|
|
|
if(b.b)
|
|
|
|
std::for_each(b.b, b.e, closure);
|
2017-09-08 21:29:21 +02:00
|
|
|
else if(!b.m.first.empty())
|
|
|
|
closure(b.m);
|
|
|
|
|
|
|
|
if(b.child)
|
|
|
|
for_each(*b.child, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-09-09 21:20:00 +02:00
|
|
|
ircd::json::until(const iov &b,
|
2017-09-08 21:29:21 +02:00
|
|
|
const member_closure_bool &closure)
|
|
|
|
{
|
2017-09-09 21:20:00 +02:00
|
|
|
if(b.b && b.e)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
2017-09-09 21:20:00 +02:00
|
|
|
if(!ircd::until(b.b, b.e, closure))
|
2017-09-08 21:29:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(!b.m.first.empty() && !closure(b.m))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(b.child)
|
|
|
|
return until(*b.child, closure);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-09-09 21:20:00 +02:00
|
|
|
ircd::json::iov::has(const string_view &key)
|
2017-09-08 21:29:21 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto *const member(find(key));
|
|
|
|
return member != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::json::value &
|
2017-09-09 21:20:00 +02:00
|
|
|
ircd::json::iov::at(const string_view &key)
|
2017-09-08 21:29:21 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto *const member(find(key));
|
|
|
|
if(!member)
|
|
|
|
throw not_found("'%s'", key);
|
|
|
|
|
|
|
|
return member->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::json::member *
|
2017-09-09 21:20:00 +02:00
|
|
|
ircd::json::iov::find(const string_view &key)
|
2017-09-08 21:29:21 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const member *ret;
|
|
|
|
const auto test
|
|
|
|
{
|
|
|
|
[&key, &ret](const auto &member) -> bool
|
|
|
|
{
|
|
|
|
if(key == string_view{member.first})
|
|
|
|
{
|
|
|
|
ret = &member;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return !until(*this, test)? ret : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-09-09 21:20:00 +02:00
|
|
|
ircd::json::iov::test(const member_closure_bool &closure)
|
2017-09-08 21:29:21 +02:00
|
|
|
const
|
|
|
|
{
|
2017-09-09 21:20:00 +02:00
|
|
|
if(b && likely(e))
|
|
|
|
return ircd::until(b, e, closure);
|
2017-09-08 21:29:21 +02:00
|
|
|
|
|
|
|
if(!m.first.empty())
|
|
|
|
return closure(m);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-09 21:20:00 +02:00
|
|
|
ircd::json::iov::add::add(iov &head, const members &ms)
|
|
|
|
:iov{&head, nullptr, ms}
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
2017-09-09 21:20:00 +02:00
|
|
|
const auto existing(json::find(&head, [&ms](const iov &existing)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
|
|
|
return existing.test([&ms](const member &existing)
|
|
|
|
{
|
|
|
|
return std::all_of(begin(ms), end(ms), [&existing]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
return member.first == existing.first;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
if(existing)
|
|
|
|
throw exists("failed to add member '%s': already exists",
|
|
|
|
string_view{existing->m.first}); //TODO BUG
|
|
|
|
|
|
|
|
tail(&head)->child = this;
|
|
|
|
}
|
|
|
|
|
2017-09-09 21:20:00 +02:00
|
|
|
ircd::json::iov::add::add(iov &head, member member)
|
|
|
|
:iov{&head, nullptr, std::move(member)}
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
2017-09-09 21:20:00 +02:00
|
|
|
const auto existing(json::find(&head, [&member](const iov &existing)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
|
|
|
return existing.test([&member](const auto &existing)
|
|
|
|
{
|
|
|
|
return member.first == existing.first;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
if(existing)
|
|
|
|
throw exists("failed to add member '%s': already exists",
|
|
|
|
string_view{member.first});
|
|
|
|
|
|
|
|
tail(&head)->child = this;
|
|
|
|
}
|
|
|
|
|
2017-09-09 21:20:00 +02:00
|
|
|
ircd::json::iov::set::set(iov &head, const members &ms)
|
|
|
|
:iov{&head, nullptr, ms}
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
2017-09-09 21:20:00 +02:00
|
|
|
const auto existing(json::find(&head, [&ms](const iov &existing)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
|
|
|
return existing.test([&ms](const member &existing)
|
|
|
|
{
|
|
|
|
return std::all_of(begin(ms), end(ms), [&existing]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
return member.first == existing.first;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
if(existing)
|
|
|
|
{
|
|
|
|
const auto p(prev(existing));
|
|
|
|
if(p)
|
|
|
|
{
|
|
|
|
p->child = this;
|
|
|
|
this->child = existing->child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else tail(&head)->child = this;
|
|
|
|
}
|
|
|
|
|
2017-09-09 21:20:00 +02:00
|
|
|
ircd::json::iov::set::set(iov &head, member member)
|
|
|
|
:iov{&head, nullptr, std::move(member)}
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
2017-09-09 21:20:00 +02:00
|
|
|
const auto existing(json::find(&head, [&member](const iov &existing)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
|
|
|
return existing.test([&member](const auto &existing)
|
|
|
|
{
|
|
|
|
return member.first == existing.first;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
if(existing)
|
|
|
|
{
|
|
|
|
const auto p(prev(existing));
|
|
|
|
if(p)
|
|
|
|
{
|
|
|
|
p->child = this;
|
|
|
|
this->child = existing->child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else tail(&head)->child = this;
|
|
|
|
}
|