mirror of
https://github.com/matrix-construct/construct
synced 2024-11-29 10:12:39 +01:00
ircd::json: Add splice operations over json::strung.
This commit is contained in:
parent
c189feb87b
commit
cffe958d2b
2 changed files with 81 additions and 0 deletions
|
@ -14,6 +14,10 @@
|
|||
namespace ircd::json
|
||||
{
|
||||
struct strung;
|
||||
|
||||
strung remove(const strung &, const string_view &key);
|
||||
strung remove(const strung &, const size_t &index);
|
||||
strung insert(const strung &, const json::member &);
|
||||
}
|
||||
|
||||
/// Interface around an allocated std::string of JSON. This is not a
|
||||
|
|
77
ircd/json.cc
77
ircd/json.cc
|
@ -1209,6 +1209,83 @@ ircd::json::iov::defaults::defaults(iov &iov,
|
|||
// json/strung.h
|
||||
//
|
||||
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
ircd::json::strung::operator
|
||||
json::array()
|
||||
const
|
||||
|
|
Loading…
Reference in a new issue