2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Parser.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/StackAllocator.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
2023-07-31 18:38:53 +02:00
|
|
|
#include <functional>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <new>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class JsonTrue {
|
|
|
|
public:
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const JsonTrue &val) {
|
|
|
|
return sb << "true";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class JsonFalse {
|
|
|
|
public:
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const JsonFalse &val) {
|
|
|
|
return sb << "false";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class JsonNull {
|
|
|
|
public:
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, JsonNull val) {
|
|
|
|
return sb << "null";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class JsonBool {
|
|
|
|
public:
|
|
|
|
explicit JsonBool(bool value) : value_(value) {
|
|
|
|
}
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const JsonBool &val) {
|
|
|
|
if (val.value_) {
|
|
|
|
return sb << JsonTrue();
|
|
|
|
} else {
|
|
|
|
return sb << JsonFalse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class JsonInt {
|
|
|
|
public:
|
|
|
|
explicit JsonInt(int32 value) : value_(value) {
|
|
|
|
}
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const JsonInt &val) {
|
|
|
|
return sb << val.value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int32 value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class JsonLong {
|
|
|
|
public:
|
|
|
|
explicit JsonLong(int64 value) : value_(value) {
|
|
|
|
}
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const JsonLong &val) {
|
|
|
|
return sb << val.value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int64 value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class JsonFloat {
|
|
|
|
public:
|
|
|
|
explicit JsonFloat(double value) : value_(value) {
|
|
|
|
}
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const JsonFloat &val) {
|
|
|
|
return sb << val.value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
double value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class JsonOneChar {
|
|
|
|
public:
|
2021-10-17 21:56:40 +02:00
|
|
|
explicit JsonOneChar(uint32 c) : c_(c) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const JsonOneChar &val) {
|
|
|
|
auto c = val.c_;
|
|
|
|
return sb << '\\' << 'u' << "0123456789abcdef"[c >> 12] << "0123456789abcdef"[(c >> 8) & 15]
|
|
|
|
<< "0123456789abcdef"[(c >> 4) & 15] << "0123456789abcdef"[c & 15];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 c_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class JsonChar {
|
|
|
|
public:
|
2021-10-17 21:56:40 +02:00
|
|
|
explicit JsonChar(uint32 c) : c_(c) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const JsonChar &val) {
|
|
|
|
auto c = val.c_;
|
|
|
|
if (c < 0x10000) {
|
|
|
|
if (0xD7FF < c && c < 0xE000) {
|
2018-02-17 00:54:44 +01:00
|
|
|
// UTF-8 correctness has already been checked
|
2018-12-31 20:04:05 +01:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
return sb << JsonOneChar(c);
|
|
|
|
} else if (c <= 0x10ffff) {
|
|
|
|
return sb << JsonOneChar(0xD7C0 + (c >> 10)) << JsonOneChar(0xDC00 + (c & 0x3FF));
|
|
|
|
} else {
|
2018-02-17 00:54:44 +01:00
|
|
|
// UTF-8 correctness has already been checked
|
2018-12-31 20:04:05 +01:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 c_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class JsonRaw {
|
|
|
|
public:
|
|
|
|
explicit JsonRaw(Slice value) : value_(value) {
|
|
|
|
}
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const JsonRaw &val) {
|
|
|
|
return sb << val.value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Slice value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class JsonRawString {
|
|
|
|
public:
|
|
|
|
explicit JsonRawString(Slice value) : value_(value) {
|
|
|
|
}
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const JsonRawString &val);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Slice value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class JsonString {
|
|
|
|
public:
|
|
|
|
explicit JsonString(Slice str) : str_(str) {
|
|
|
|
}
|
|
|
|
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const JsonString &val);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Slice str_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class JsonScope;
|
|
|
|
class JsonValueScope;
|
|
|
|
class JsonArrayScope;
|
|
|
|
class JsonObjectScope;
|
|
|
|
|
|
|
|
class JsonBuilder {
|
|
|
|
public:
|
2020-06-24 13:47:36 +02:00
|
|
|
explicit JsonBuilder(StringBuilder &&sb = {}, int32 offset = -1) : sb_(std::move(sb)), offset_(offset) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
StringBuilder &string_builder() {
|
|
|
|
return sb_;
|
|
|
|
}
|
|
|
|
friend class JsonScope;
|
|
|
|
JsonValueScope enter_value() TD_WARN_UNUSED_RESULT;
|
|
|
|
JsonArrayScope enter_array() TD_WARN_UNUSED_RESULT;
|
|
|
|
JsonObjectScope enter_object() TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
int32 offset() const {
|
|
|
|
return offset_;
|
|
|
|
}
|
|
|
|
bool is_pretty() const {
|
|
|
|
return offset_ >= 0;
|
|
|
|
}
|
|
|
|
void print_offset() {
|
2019-08-13 17:33:32 +02:00
|
|
|
if (offset_ >= 0) {
|
|
|
|
sb_ << '\n';
|
|
|
|
for (int x = 0; x < offset_; x++) {
|
|
|
|
sb_ << " ";
|
|
|
|
}
|
2019-08-12 13:45:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void dec_offset() {
|
|
|
|
if (offset_ >= 0) {
|
|
|
|
CHECK(offset_ > 0);
|
|
|
|
offset_--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void inc_offset() {
|
|
|
|
if (offset_ >= 0) {
|
|
|
|
offset_++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
StringBuilder sb_;
|
|
|
|
JsonScope *scope_ = nullptr;
|
2019-08-12 13:45:57 +02:00
|
|
|
int32 offset_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class Jsonable {};
|
|
|
|
|
|
|
|
class JsonScope {
|
|
|
|
public:
|
2018-10-26 16:11:20 +02:00
|
|
|
explicit JsonScope(JsonBuilder *jb) : sb_(&jb->sb_), jb_(jb), save_scope_(jb->scope_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
jb_->scope_ = this;
|
|
|
|
CHECK(is_active());
|
|
|
|
}
|
2023-05-05 12:51:19 +02:00
|
|
|
JsonScope(const JsonScope &) = delete;
|
2023-05-05 13:31:55 +02:00
|
|
|
JsonScope &operator=(const JsonScope &) = delete;
|
2021-11-10 19:55:50 +01:00
|
|
|
JsonScope(JsonScope &&other) noexcept : sb_(other.sb_), jb_(other.jb_), save_scope_(other.save_scope_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
other.jb_ = nullptr;
|
|
|
|
}
|
|
|
|
JsonScope &operator=(JsonScope &&) = delete;
|
|
|
|
~JsonScope() {
|
|
|
|
if (jb_) {
|
|
|
|
leave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void leave() {
|
|
|
|
CHECK(is_active());
|
|
|
|
jb_->scope_ = save_scope_;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
StringBuilder *sb_;
|
|
|
|
|
|
|
|
// For CHECK
|
|
|
|
JsonBuilder *jb_;
|
|
|
|
JsonScope *save_scope_;
|
|
|
|
|
|
|
|
bool is_active() const {
|
|
|
|
return jb_ && jb_->scope_ == this;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonScope &operator<<(JsonTrue x) {
|
|
|
|
*sb_ << x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
JsonScope &operator<<(JsonFalse x) {
|
|
|
|
*sb_ << x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
JsonScope &operator<<(JsonNull x) {
|
|
|
|
*sb_ << x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
JsonScope &operator<<(const JsonBool &x) {
|
|
|
|
*sb_ << x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
JsonScope &operator<<(const JsonInt &x) {
|
|
|
|
*sb_ << x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
JsonScope &operator<<(const JsonLong &x) {
|
|
|
|
*sb_ << x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
JsonScope &operator<<(const JsonFloat &x) {
|
|
|
|
*sb_ << x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
JsonScope &operator<<(const JsonString &x) {
|
|
|
|
*sb_ << x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
JsonScope &operator<<(const JsonRawString &x) {
|
|
|
|
*sb_ << x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
JsonScope &operator<<(const JsonRaw &x) {
|
|
|
|
*sb_ << x;
|
|
|
|
return *this;
|
|
|
|
}
|
2019-12-23 01:21:51 +01:00
|
|
|
JsonScope &operator<<(bool x) = delete;
|
2018-12-31 20:04:05 +01:00
|
|
|
JsonScope &operator<<(int32 x) {
|
|
|
|
return *this << JsonInt(x);
|
|
|
|
}
|
|
|
|
JsonScope &operator<<(int64 x) {
|
|
|
|
return *this << JsonLong(x);
|
|
|
|
}
|
|
|
|
JsonScope &operator<<(double x) {
|
|
|
|
return *this << JsonFloat(x);
|
|
|
|
}
|
2019-12-23 01:09:21 +01:00
|
|
|
template <size_t N>
|
|
|
|
JsonScope &operator<<(const char (&x)[N]) {
|
|
|
|
return *this << JsonString(Slice(x));
|
|
|
|
}
|
|
|
|
JsonScope &operator<<(const char *x) {
|
|
|
|
return *this << JsonString(Slice(x));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
JsonScope &operator<<(Slice x) {
|
|
|
|
return *this << JsonString(x);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class JsonValueScope final : public JsonScope {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
using JsonScope::JsonScope;
|
|
|
|
template <class T>
|
|
|
|
std::enable_if_t<std::is_base_of<Jsonable, typename std::decay<T>::type>::value, JsonValueScope &> operator<<(
|
|
|
|
const T &x) {
|
|
|
|
x.store(this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
std::enable_if_t<!std::is_base_of<Jsonable, typename std::decay<T>::type>::value, JsonValueScope &> operator<<(
|
|
|
|
const T &x) {
|
|
|
|
CHECK(!was_);
|
|
|
|
was_ = true;
|
|
|
|
JsonScope::operator<<(x);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonArrayScope enter_array() TD_WARN_UNUSED_RESULT;
|
|
|
|
JsonObjectScope enter_object() TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool was_ = false;
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class JsonArrayScope final : public JsonScope {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit JsonArrayScope(JsonBuilder *jb) : JsonScope(jb) {
|
2019-08-12 13:45:57 +02:00
|
|
|
jb->inc_offset();
|
2018-12-31 20:04:05 +01:00
|
|
|
*sb_ << "[";
|
|
|
|
}
|
2023-05-05 13:31:55 +02:00
|
|
|
JsonArrayScope(const JsonArrayScope &) = delete;
|
|
|
|
JsonArrayScope &operator=(const JsonArrayScope &) = delete;
|
2023-05-05 12:51:19 +02:00
|
|
|
JsonArrayScope(JsonArrayScope &&) = default;
|
2023-05-05 13:31:55 +02:00
|
|
|
JsonArrayScope &operator=(JsonArrayScope &&) = delete;
|
2018-12-31 20:04:05 +01:00
|
|
|
~JsonArrayScope() {
|
|
|
|
if (jb_) {
|
|
|
|
leave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void leave() {
|
2019-08-12 13:45:57 +02:00
|
|
|
jb_->dec_offset();
|
2020-06-26 01:24:13 +02:00
|
|
|
jb_->print_offset();
|
2018-12-31 20:04:05 +01:00
|
|
|
*sb_ << "]";
|
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
JsonArrayScope &operator<<(const T &x) {
|
2018-04-11 13:55:07 +02:00
|
|
|
return (*this)(x);
|
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
JsonArrayScope &operator()(const T &x) {
|
2018-12-31 20:04:05 +01:00
|
|
|
enter_value() << x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
JsonValueScope enter_value() {
|
|
|
|
CHECK(is_active());
|
|
|
|
if (is_first_) {
|
|
|
|
*sb_ << ",";
|
|
|
|
} else {
|
|
|
|
is_first_ = true;
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
jb_->print_offset();
|
2018-12-31 20:04:05 +01:00
|
|
|
return jb_->enter_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_first_ = false;
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class JsonObjectScope final : public JsonScope {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit JsonObjectScope(JsonBuilder *jb) : JsonScope(jb) {
|
2019-08-12 13:45:57 +02:00
|
|
|
jb->inc_offset();
|
2018-12-31 20:04:05 +01:00
|
|
|
*sb_ << "{";
|
|
|
|
}
|
2023-05-05 13:31:55 +02:00
|
|
|
JsonObjectScope(const JsonObjectScope &) = delete;
|
|
|
|
JsonObjectScope &operator=(const JsonObjectScope &) = delete;
|
2023-05-05 12:51:19 +02:00
|
|
|
JsonObjectScope(JsonObjectScope &&) = default;
|
2023-05-05 13:31:55 +02:00
|
|
|
JsonObjectScope &operator=(JsonObjectScope &&) = delete;
|
2018-12-31 20:04:05 +01:00
|
|
|
~JsonObjectScope() {
|
|
|
|
if (jb_) {
|
|
|
|
leave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void leave() {
|
2019-08-12 13:45:57 +02:00
|
|
|
jb_->dec_offset();
|
2020-06-26 01:24:13 +02:00
|
|
|
jb_->print_offset();
|
2018-12-31 20:04:05 +01:00
|
|
|
*sb_ << "}";
|
|
|
|
}
|
2019-12-08 07:02:20 +01:00
|
|
|
template <class T>
|
2023-07-31 12:31:48 +02:00
|
|
|
JsonObjectScope &operator()(Slice field, T &&value) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(is_active());
|
|
|
|
if (is_first_) {
|
|
|
|
*sb_ << ",";
|
|
|
|
} else {
|
|
|
|
is_first_ = true;
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
jb_->print_offset();
|
2023-07-31 12:31:48 +02:00
|
|
|
jb_->enter_value() << field;
|
2019-08-12 13:45:57 +02:00
|
|
|
if (jb_->is_pretty()) {
|
|
|
|
*sb_ << " : ";
|
|
|
|
} else {
|
|
|
|
*sb_ << ":";
|
|
|
|
}
|
2018-04-11 13:55:07 +02:00
|
|
|
jb_->enter_value() << value;
|
2018-12-31 20:04:05 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2023-07-31 12:31:48 +02:00
|
|
|
JsonObjectScope &operator<<(const JsonRaw &field_value) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(is_active());
|
|
|
|
is_first_ = true;
|
2023-07-31 12:31:48 +02:00
|
|
|
jb_->enter_value() << field_value;
|
2018-12-31 20:04:05 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_first_ = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline JsonArrayScope JsonValueScope::enter_array() {
|
|
|
|
CHECK(!was_);
|
|
|
|
was_ = true;
|
|
|
|
return JsonArrayScope(jb_);
|
|
|
|
}
|
|
|
|
inline JsonObjectScope JsonValueScope::enter_object() {
|
|
|
|
CHECK(!was_);
|
|
|
|
was_ = true;
|
|
|
|
return JsonObjectScope(jb_);
|
|
|
|
}
|
|
|
|
inline JsonValueScope JsonBuilder::enter_value() {
|
|
|
|
return JsonValueScope(this);
|
|
|
|
}
|
|
|
|
inline JsonObjectScope JsonBuilder::enter_object() {
|
|
|
|
return JsonObjectScope(this);
|
|
|
|
}
|
|
|
|
inline JsonArrayScope JsonBuilder::enter_array() {
|
|
|
|
return JsonArrayScope(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
class JsonValue;
|
|
|
|
|
2023-07-31 15:30:39 +02:00
|
|
|
enum class JsonValueType { Null, Number, Boolean, String, Array, Object };
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
using JsonArray = vector<JsonValue>;
|
|
|
|
|
2023-07-31 12:31:48 +02:00
|
|
|
class JsonObject {
|
2023-07-31 15:30:39 +02:00
|
|
|
const JsonValue *get_field(Slice name) const;
|
|
|
|
|
2023-07-31 12:31:48 +02:00
|
|
|
public:
|
2023-07-31 18:47:55 +02:00
|
|
|
vector<std::pair<Slice, JsonValue>> field_values_;
|
|
|
|
|
|
|
|
JsonObject() = default;
|
|
|
|
|
2024-05-17 13:53:27 +02:00
|
|
|
explicit JsonObject(vector<std::pair<Slice, JsonValue>> &&field_values);
|
2023-07-31 12:33:35 +02:00
|
|
|
|
2023-07-31 19:01:02 +02:00
|
|
|
JsonObject(const JsonObject &) = delete;
|
|
|
|
JsonObject &operator=(const JsonObject &) = delete;
|
2024-02-22 11:08:31 +01:00
|
|
|
JsonObject(JsonObject &&) = default;
|
|
|
|
JsonObject &operator=(JsonObject &&) = default;
|
2023-07-31 19:01:02 +02:00
|
|
|
~JsonObject() = default;
|
|
|
|
|
2024-05-17 13:53:27 +02:00
|
|
|
size_t field_count() const;
|
2023-07-31 15:30:39 +02:00
|
|
|
|
|
|
|
JsonValue extract_field(Slice name);
|
|
|
|
|
|
|
|
Result<JsonValue> extract_optional_field(Slice name, JsonValueType type);
|
|
|
|
|
|
|
|
Result<JsonValue> extract_required_field(Slice name, JsonValueType type);
|
|
|
|
|
|
|
|
bool has_field(Slice name) const;
|
|
|
|
|
|
|
|
Result<bool> get_optional_bool_field(Slice name, bool default_value = false) const;
|
|
|
|
|
|
|
|
Result<bool> get_required_bool_field(Slice name) const;
|
|
|
|
|
|
|
|
Result<int32> get_optional_int_field(Slice name, int32 default_value = 0) const;
|
|
|
|
|
|
|
|
Result<int32> get_required_int_field(Slice name) const;
|
|
|
|
|
|
|
|
Result<int64> get_optional_long_field(Slice name, int64 default_value = 0) const;
|
|
|
|
|
|
|
|
Result<int64> get_required_long_field(Slice name) const;
|
|
|
|
|
|
|
|
Result<double> get_optional_double_field(Slice name, double default_value = 0.0) const;
|
|
|
|
|
|
|
|
Result<double> get_required_double_field(Slice name) const;
|
|
|
|
|
|
|
|
Result<string> get_optional_string_field(Slice name, string default_value = string()) const;
|
|
|
|
|
|
|
|
Result<string> get_required_string_field(Slice name) const;
|
2023-07-31 18:38:53 +02:00
|
|
|
|
|
|
|
void foreach(const std::function<void(Slice name, const JsonValue &value)> &callback) const;
|
2023-07-31 12:31:48 +02:00
|
|
|
};
|
|
|
|
|
2021-07-04 17:22:20 +02:00
|
|
|
class JsonValue final : private Jsonable {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2023-07-31 15:30:39 +02:00
|
|
|
using Type = JsonValueType;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
static Slice get_type_name(Type type);
|
|
|
|
|
|
|
|
JsonValue() {
|
|
|
|
}
|
|
|
|
~JsonValue() {
|
|
|
|
destroy();
|
|
|
|
}
|
2021-10-18 13:36:15 +02:00
|
|
|
JsonValue(JsonValue &&other) noexcept : JsonValue() {
|
2018-12-31 20:04:05 +01:00
|
|
|
init(std::move(other));
|
|
|
|
}
|
2021-10-18 13:36:15 +02:00
|
|
|
JsonValue &operator=(JsonValue &&other) noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (&other == this) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
destroy();
|
|
|
|
init(std::move(other));
|
|
|
|
return *this;
|
|
|
|
}
|
2023-05-05 12:51:19 +02:00
|
|
|
JsonValue(const JsonValue &) = delete;
|
|
|
|
JsonValue &operator=(const JsonValue &) = delete;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Type type() const {
|
|
|
|
return type_;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableSlice &get_string() {
|
|
|
|
CHECK(type_ == Type::String);
|
|
|
|
return string_;
|
|
|
|
}
|
|
|
|
const MutableSlice &get_string() const {
|
|
|
|
CHECK(type_ == Type::String);
|
|
|
|
return string_;
|
|
|
|
}
|
|
|
|
bool &get_boolean() {
|
|
|
|
CHECK(type_ == Type::Boolean);
|
|
|
|
return boolean_;
|
|
|
|
}
|
|
|
|
const bool &get_boolean() const {
|
|
|
|
CHECK(type_ == Type::Boolean);
|
|
|
|
return boolean_;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableSlice &get_number() {
|
|
|
|
CHECK(type_ == Type::Number);
|
|
|
|
return number_;
|
|
|
|
}
|
|
|
|
const MutableSlice &get_number() const {
|
|
|
|
CHECK(type_ == Type::Number);
|
|
|
|
return number_;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonArray &get_array() {
|
|
|
|
CHECK(type_ == Type::Array);
|
|
|
|
return array_;
|
|
|
|
}
|
|
|
|
const JsonArray &get_array() const {
|
|
|
|
CHECK(type_ == Type::Array);
|
|
|
|
return array_;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObject &get_object() {
|
|
|
|
CHECK(type_ == Type::Object);
|
|
|
|
return object_;
|
|
|
|
}
|
|
|
|
const JsonObject &get_object() const {
|
|
|
|
CHECK(type_ == Type::Object);
|
|
|
|
return object_;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonValue create_boolean(bool val) {
|
|
|
|
JsonValue res;
|
|
|
|
res.init_boolean(val);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonValue create_number(MutableSlice number) {
|
|
|
|
JsonValue res;
|
|
|
|
res.init_number(number);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonValue create_string(MutableSlice str) {
|
|
|
|
JsonValue res;
|
|
|
|
res.init_string(str);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonValue create_array(JsonArray v) {
|
|
|
|
JsonValue res;
|
|
|
|
res.init_array(std::move(v));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonValue make_object(JsonObject c) {
|
|
|
|
JsonValue res;
|
|
|
|
res.init_object(std::move(c));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void store(JsonValueScope *scope) const {
|
|
|
|
switch (type_) {
|
|
|
|
case Type::Null:
|
|
|
|
*scope << JsonRaw("null");
|
|
|
|
break;
|
|
|
|
case Type::Boolean:
|
|
|
|
if (get_boolean()) {
|
|
|
|
*scope << JsonRaw("true");
|
|
|
|
} else {
|
|
|
|
*scope << JsonRaw("false");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Type::Number:
|
|
|
|
*scope << JsonRaw(get_number());
|
|
|
|
break;
|
|
|
|
case Type::String:
|
|
|
|
*scope << JsonString(get_string());
|
|
|
|
break;
|
|
|
|
case Type::Array: {
|
|
|
|
auto arr = scope->enter_array();
|
|
|
|
for (auto &val : get_array()) {
|
|
|
|
arr << val;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::Object: {
|
|
|
|
auto object = scope->enter_object();
|
2023-07-31 12:31:48 +02:00
|
|
|
for (auto &field_value : get_object().field_values_) {
|
|
|
|
object(field_value.first, field_value.second);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
Type type_{Type::Null};
|
|
|
|
union {
|
|
|
|
MutableSlice number_;
|
|
|
|
bool boolean_;
|
|
|
|
MutableSlice string_;
|
|
|
|
JsonArray array_;
|
|
|
|
JsonObject object_;
|
|
|
|
};
|
|
|
|
|
|
|
|
void init_null() {
|
|
|
|
type_ = Type::Null;
|
|
|
|
}
|
|
|
|
void init_number(MutableSlice number) {
|
|
|
|
type_ = Type::Number;
|
|
|
|
new (&number_) MutableSlice(number);
|
|
|
|
}
|
|
|
|
void init_boolean(bool boolean) {
|
|
|
|
type_ = Type::Boolean;
|
|
|
|
boolean_ = boolean;
|
|
|
|
}
|
|
|
|
void init_string(MutableSlice slice) {
|
|
|
|
type_ = Type::String;
|
|
|
|
new (&string_) MutableSlice(slice);
|
|
|
|
}
|
|
|
|
void init_array(JsonArray array) {
|
|
|
|
type_ = Type::Array;
|
|
|
|
new (&array_) JsonArray(std::move(array));
|
|
|
|
}
|
|
|
|
void init_object(JsonObject object) {
|
|
|
|
type_ = Type::Object;
|
|
|
|
new (&object_) JsonObject(std::move(object));
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(JsonValue &&other) {
|
|
|
|
switch (other.type_) {
|
|
|
|
case Type::Null:
|
|
|
|
break;
|
|
|
|
case Type::Number:
|
|
|
|
init_number(other.number_);
|
|
|
|
break;
|
|
|
|
case Type::Boolean:
|
|
|
|
init_boolean(other.boolean_);
|
|
|
|
break;
|
|
|
|
case Type::String:
|
|
|
|
init_string(other.string_);
|
|
|
|
break;
|
|
|
|
case Type::Array:
|
|
|
|
init_array(std::move(other.array_));
|
|
|
|
break;
|
|
|
|
case Type::Object:
|
|
|
|
init_object(std::move(other.object_));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
other.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy() {
|
|
|
|
switch (type_) {
|
|
|
|
case Type::Null:
|
|
|
|
case Type::Boolean:
|
|
|
|
break;
|
|
|
|
case Type::Number:
|
|
|
|
number_.~MutableSlice();
|
|
|
|
break;
|
|
|
|
case Type::String:
|
|
|
|
string_.~MutableSlice();
|
|
|
|
break;
|
|
|
|
case Type::Array:
|
|
|
|
array_.~vector<JsonValue>();
|
|
|
|
break;
|
|
|
|
case Type::Object:
|
2023-07-31 12:31:48 +02:00
|
|
|
object_.~JsonObject();
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
type_ = Type::Null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &sb, JsonValue::Type type) {
|
|
|
|
switch (type) {
|
|
|
|
case JsonValue::Type::Null:
|
2018-05-20 14:30:36 +02:00
|
|
|
return sb << "Null";
|
2018-12-31 20:04:05 +01:00
|
|
|
case JsonValue::Type::Number:
|
2018-05-20 14:30:36 +02:00
|
|
|
return sb << "Number";
|
|
|
|
case JsonValue::Type::Boolean:
|
|
|
|
return sb << "Boolean";
|
2018-12-31 20:04:05 +01:00
|
|
|
case JsonValue::Type::String:
|
2018-05-20 14:30:36 +02:00
|
|
|
return sb << "String";
|
|
|
|
case JsonValue::Type::Array:
|
|
|
|
return sb << "Array";
|
|
|
|
case JsonValue::Type::Object:
|
|
|
|
return sb << "Object";
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-04 17:22:20 +02:00
|
|
|
class VirtuallyJsonable : private Jsonable {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
virtual void store(JsonValueScope *scope) const = 0;
|
|
|
|
VirtuallyJsonable() = default;
|
|
|
|
VirtuallyJsonable(const VirtuallyJsonable &) = delete;
|
|
|
|
VirtuallyJsonable &operator=(const VirtuallyJsonable &) = delete;
|
|
|
|
VirtuallyJsonable(VirtuallyJsonable &&) = default;
|
|
|
|
VirtuallyJsonable &operator=(VirtuallyJsonable &&) = default;
|
|
|
|
virtual ~VirtuallyJsonable() = default;
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class VirtuallyJsonableInt final : public VirtuallyJsonable {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit VirtuallyJsonableInt(int32 value) : value_(value) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void store(JsonValueScope *scope) const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
*scope << JsonInt(value_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int32 value_;
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class VirtuallyJsonableLong final : public VirtuallyJsonable {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit VirtuallyJsonableLong(int64 value) : value_(value) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void store(JsonValueScope *scope) const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
*scope << JsonLong(value_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int64 value_;
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class VirtuallyJsonableString final : public VirtuallyJsonable {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit VirtuallyJsonableString(Slice value) : value_(value) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void store(JsonValueScope *scope) const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
*scope << JsonString(value_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Slice value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
Result<MutableSlice> json_string_decode(Parser &parser) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status json_string_skip(Parser &parser) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
Result<JsonValue> do_json_decode(Parser &parser, int32 max_depth) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status do_json_skip(Parser &parser, int32 max_depth) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2018-12-10 02:01:02 +01:00
|
|
|
inline Result<JsonValue> json_decode(MutableSlice json) {
|
|
|
|
Parser parser(json);
|
2018-12-31 20:04:05 +01:00
|
|
|
const int32 DEFAULT_MAX_DEPTH = 100;
|
|
|
|
auto result = do_json_decode(parser, DEFAULT_MAX_DEPTH);
|
2018-03-09 18:43:24 +01:00
|
|
|
if (result.is_ok()) {
|
|
|
|
parser.skip_whitespaces();
|
|
|
|
if (!parser.empty()) {
|
|
|
|
return Status::Error("Expected string end");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StrT, class ValT>
|
2019-08-12 13:45:57 +02:00
|
|
|
StrT json_encode(const ValT &val, bool pretty = false) {
|
2018-10-30 15:26:31 +01:00
|
|
|
auto buf_len = 1 << 18;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto buf = StackAllocator::alloc(buf_len);
|
2019-08-12 13:45:57 +02:00
|
|
|
JsonBuilder jb(StringBuilder(buf.as_slice(), true), pretty ? 0 : -1);
|
2018-12-31 20:04:05 +01:00
|
|
|
jb.enter_value() << val;
|
2019-08-12 13:45:57 +02:00
|
|
|
if (pretty) {
|
|
|
|
jb.string_builder() << "\n";
|
|
|
|
}
|
2018-09-21 10:26:35 +02:00
|
|
|
LOG_IF(ERROR, jb.string_builder().is_error()) << "JSON buffer overflow";
|
2018-12-31 20:04:05 +01:00
|
|
|
auto slice = jb.string_builder().as_cslice();
|
|
|
|
return StrT(slice.begin(), slice.size());
|
|
|
|
}
|
|
|
|
|
2018-04-10 19:10:20 +02:00
|
|
|
template <class T>
|
2021-07-04 17:22:20 +02:00
|
|
|
class ToJsonImpl final : private Jsonable {
|
2018-04-10 19:10:20 +02:00
|
|
|
public:
|
|
|
|
explicit ToJsonImpl(const T &value) : value_(value) {
|
|
|
|
}
|
|
|
|
void store(JsonValueScope *scope) const {
|
|
|
|
to_json(*scope, value_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const T &value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
auto ToJson(const T &value) {
|
|
|
|
return ToJsonImpl<T>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void to_json(JsonValueScope &jv, const T &value) {
|
|
|
|
jv << value;
|
|
|
|
}
|
|
|
|
|
2018-04-11 12:49:04 +02:00
|
|
|
template <class F>
|
2021-07-04 17:22:20 +02:00
|
|
|
class JsonObjectImpl : private Jsonable {
|
2018-04-11 12:49:04 +02:00
|
|
|
public:
|
2018-04-11 16:21:24 +02:00
|
|
|
explicit JsonObjectImpl(F &&f) : f_(std::forward<F>(f)) {
|
2018-04-11 12:49:04 +02:00
|
|
|
}
|
|
|
|
void store(JsonValueScope *scope) const {
|
|
|
|
auto object = scope->enter_object();
|
|
|
|
f_(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
F f_;
|
|
|
|
};
|
2020-01-16 02:32:02 +01:00
|
|
|
|
2018-04-11 12:49:04 +02:00
|
|
|
template <class F>
|
|
|
|
auto json_object(F &&f) {
|
|
|
|
return JsonObjectImpl<F>(std::forward<F>(f));
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:55:07 +02:00
|
|
|
template <class F>
|
2021-07-04 17:22:20 +02:00
|
|
|
class JsonArrayImpl : private Jsonable {
|
2018-04-11 13:55:07 +02:00
|
|
|
public:
|
2018-04-11 16:21:24 +02:00
|
|
|
explicit JsonArrayImpl(F &&f) : f_(std::forward<F>(f)) {
|
2018-04-11 13:55:07 +02:00
|
|
|
}
|
|
|
|
void store(JsonValueScope *scope) const {
|
|
|
|
auto array = scope->enter_array();
|
|
|
|
f_(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
F f_;
|
|
|
|
};
|
2018-04-11 16:21:24 +02:00
|
|
|
|
2018-04-11 13:55:07 +02:00
|
|
|
template <class F>
|
|
|
|
auto json_array(F &&f) {
|
|
|
|
return JsonArrayImpl<F>(std::forward<F>(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class A, class F>
|
|
|
|
auto json_array(const A &a, F &&f) {
|
|
|
|
return json_array([&a, &f](auto &arr) {
|
|
|
|
for (auto &x : a) {
|
|
|
|
arr(f(x));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|