2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-03-18 04:32:32 +01:00
|
|
|
|
|
|
|
#pragma once
|
2017-08-23 23:32:28 +02:00
|
|
|
#define HAVE_IRCD_JSON_ARRAY_H
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
struct array;
|
|
|
|
|
2018-01-29 20:48:53 +01:00
|
|
|
bool empty(const array &);
|
2018-01-30 19:14:41 +01:00
|
|
|
bool operator!(const array &);
|
2018-02-17 23:46:10 +01:00
|
|
|
size_t size(const array &);
|
2018-01-29 20:48:53 +01:00
|
|
|
|
2023-02-12 05:39:48 +01:00
|
|
|
size_t serialized(const string_view *begin, const string_view *end);
|
|
|
|
size_t serialized(const std::string *begin, const std::string *end);
|
2018-03-20 22:51:58 +01:00
|
|
|
size_t serialized(const array &);
|
|
|
|
|
2023-02-12 05:39:48 +01:00
|
|
|
string_view stringify(mutable_buffer &buf, const string_view *begin, const string_view *end);
|
|
|
|
string_view stringify(mutable_buffer &buf, const std::string *begin, const std::string *end);
|
2018-03-20 22:51:58 +01:00
|
|
|
string_view stringify(mutable_buffer &, const array &);
|
2019-02-16 01:47:00 +01:00
|
|
|
std::ostream &operator<<(std::ostream &, const array &);
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Lightweight interface to a JSON array string.
|
|
|
|
///
|
2017-09-14 20:30:06 +02:00
|
|
|
/// This object accepts queries with numerical indexing. The same parsing
|
|
|
|
/// approach is used in ircd::json::object and that is important to note here:
|
|
|
|
/// iterating this array by incrementing your own numerical index and making
|
|
|
|
/// calls into this object is NOT efficient. Simply put, do not do something
|
|
|
|
/// like `for(int x=0; x<array.count(); x++) array.at(x)` as that will parse
|
2017-10-12 02:43:11 +02:00
|
|
|
/// the array from the beginning on every single increment. Instead, use the
|
2017-09-12 18:37:44 +02:00
|
|
|
/// provided iterator object.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::json::array
|
2017-03-18 04:32:32 +01:00
|
|
|
:string_view
|
|
|
|
{
|
|
|
|
struct const_iterator;
|
|
|
|
|
|
|
|
using value_type = const string_view;
|
|
|
|
using pointer = value_type *;
|
|
|
|
using reference = value_type &;
|
|
|
|
using iterator = const_iterator;
|
|
|
|
using size_type = size_t;
|
|
|
|
using difference_type = ptrdiff_t;
|
|
|
|
|
2022-06-11 21:41:30 +02:00
|
|
|
static constexpr const uint &max_recursion_depth {96};
|
2018-01-18 12:39:19 +01:00
|
|
|
|
2017-03-18 04:32:32 +01:00
|
|
|
const_iterator end() const;
|
|
|
|
const_iterator begin() const;
|
2017-03-20 12:25:55 +01:00
|
|
|
const_iterator find(size_t i) const;
|
2018-01-29 20:48:53 +01:00
|
|
|
|
|
|
|
bool empty() const;
|
2017-03-18 04:32:32 +01:00
|
|
|
size_t count() const;
|
2017-10-12 03:11:55 +02:00
|
|
|
size_t size() const;
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2017-03-20 12:25:55 +01:00
|
|
|
template<class T> T at(const size_t &i) const;
|
2023-02-12 05:39:48 +01:00
|
|
|
string_view at(const size_t i) const;
|
|
|
|
string_view operator[](const size_t i) const;
|
2017-03-20 12:25:55 +01:00
|
|
|
|
|
|
|
explicit operator std::string() const;
|
2017-03-18 04:32:32 +01:00
|
|
|
|
|
|
|
using string_view::string_view;
|
|
|
|
|
2018-03-20 22:51:58 +01:00
|
|
|
template<class it> static size_t serialized(const it &b, const it &e);
|
2017-09-14 20:30:06 +02:00
|
|
|
template<class it> static string_view stringify(mutable_buffer &, const it &b, const it &e);
|
2017-03-18 04:32:32 +01:00
|
|
|
};
|
|
|
|
|
2020-04-03 19:53:01 +02:00
|
|
|
#include "array_iterator.h"
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2017-03-20 12:25:55 +01:00
|
|
|
template<class T>
|
2020-08-02 18:13:08 +02:00
|
|
|
inline T
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::array::at(const size_t &i)
|
2017-03-20 12:25:55 +01:00
|
|
|
const try
|
|
|
|
{
|
|
|
|
return lex_cast<T>(at(i));
|
|
|
|
}
|
|
|
|
catch(const bad_lex_cast &e)
|
|
|
|
{
|
2018-11-29 19:20:19 +01:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"indice %zu must cast to type %s", i, typeid(T).name()
|
|
|
|
};
|
2017-03-20 12:25:55 +01:00
|
|
|
}
|
2020-04-03 19:53:01 +02:00
|
|
|
|
|
|
|
inline ircd::json::array::const_iterator
|
|
|
|
ircd::json::array::end()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return { string_view::end(), string_view::end() };
|
|
|
|
}
|
2020-12-10 14:40:48 +01:00
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::json::array::empty()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const string_view &sv{*this};
|
|
|
|
// Allow empty objects '{}' to pass this assertion; this function is not
|
|
|
|
// a type-check. Some serializers (like browser JS) might give an empty
|
|
|
|
// object before it has any context that the set is an array; it doesn't
|
|
|
|
// matter here for us.
|
|
|
|
assert(sv.size() > 2 || sv.empty() || sv == empty_array || sv == empty_object);
|
2022-06-30 22:12:25 +02:00
|
|
|
return sv.size() <= 2 || sv == literal_null;
|
2020-12-10 14:40:48 +01:00
|
|
|
}
|
2023-02-12 05:39:48 +01:00
|
|
|
|
|
|
|
inline size_t
|
|
|
|
ircd::json::size(const array &array)
|
|
|
|
{
|
|
|
|
return array.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::json::operator!(const array &array)
|
|
|
|
{
|
|
|
|
return empty(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::json::empty(const array &array)
|
|
|
|
{
|
|
|
|
return array.empty();
|
|
|
|
}
|