tdlight/tdutils/td/utils/JsonBuilder.cpp

886 lines
26 KiB
C++

//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
//
// 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)
//
#include "td/utils/JsonBuilder.h"
#include "td/utils/misc.h"
#include "td/utils/ScopeGuard.h"
#include "td/utils/SliceBuilder.h"
#include <cstring>
namespace td {
StringBuilder &operator<<(StringBuilder &sb, const JsonRawString &val) {
sb << '"';
SCOPE_EXIT {
sb << '"';
};
auto *s = val.value_.begin();
auto len = val.value_.size();
for (size_t pos = 0; pos < len; pos++) {
auto ch = static_cast<unsigned char>(s[pos]);
switch (ch) {
case '"':
sb << '\\' << '"';
break;
case '\\':
sb << '\\' << '\\';
break;
case '\b':
sb << '\\' << 'b';
break;
case '\f':
sb << '\\' << 'f';
break;
case '\n':
sb << '\\' << 'n';
break;
case '\r':
sb << '\\' << 'r';
break;
case '\t':
sb << '\\' << 't';
break;
default:
if (ch <= 31) {
sb << JsonOneChar(s[pos]);
break;
}
sb << s[pos];
break;
}
}
return sb;
}
StringBuilder &operator<<(StringBuilder &sb, const JsonString &val) {
sb << '"';
SCOPE_EXIT {
sb << '"';
};
auto *s = val.str_.begin();
auto len = val.str_.size();
for (size_t pos = 0; pos < len; pos++) {
auto ch = static_cast<unsigned char>(s[pos]);
switch (ch) {
case '"':
sb << '\\' << '"';
break;
case '\\':
sb << '\\' << '\\';
break;
case '\b':
sb << '\\' << 'b';
break;
case '\f':
sb << '\\' << 'f';
break;
case '\n':
sb << '\\' << 'n';
break;
case '\r':
sb << '\\' << 'r';
break;
case '\t':
sb << '\\' << 't';
break;
default:
if (ch <= 31) {
sb << JsonOneChar(s[pos]);
break;
}
if (128 <= ch) {
uint32 a = ch;
CHECK((a & 0x40) != 0);
CHECK(pos + 1 < len);
uint32 b = static_cast<unsigned char>(s[++pos]);
CHECK((b & 0xc0) == 0x80);
if ((a & 0x20) == 0) {
CHECK((a & 0x1e) > 0);
sb << JsonChar(((a & 0x1f) << 6) | (b & 0x3f));
break;
}
CHECK(pos + 1 < len);
uint32 c = static_cast<unsigned char>(s[++pos]);
CHECK((c & 0xc0) == 0x80);
if ((a & 0x10) == 0) {
CHECK(((a & 0x0f) | (b & 0x20)) > 0);
sb << JsonChar(((a & 0x0f) << 12) | ((b & 0x3f) << 6) | (c & 0x3f));
break;
}
CHECK(pos + 1 < len);
uint32 d = static_cast<unsigned char>(s[++pos]);
CHECK((d & 0xc0) == 0x80);
if ((a & 0x08) == 0) {
CHECK(((a & 0x07) | (b & 0x30)) > 0);
sb << JsonChar(((a & 0x07) << 18) | ((b & 0x3f) << 12) | ((c & 0x3f) << 6) | (d & 0x3f));
break;
}
UNREACHABLE();
break;
}
sb << s[pos];
break;
}
}
return sb;
}
Result<MutableSlice> json_string_decode(Parser &parser) {
if (!parser.try_skip('"')) {
return Status::Error("Opening '\"' expected");
}
auto data = parser.data();
auto *result_start = data.ubegin();
auto *cur_src = result_start;
auto *cur_dest = result_start;
auto *end = data.uend();
while (true) {
if (cur_src == end) {
return Status::Error("Closing '\"' not found");
}
if (*cur_src == '"') {
parser.advance(cur_src + 1 - result_start);
return data.substr(0, cur_dest - result_start);
}
if (*cur_src == '\\') {
cur_src++;
if (cur_src == end) {
return Status::Error("Closing '\"' not found");
}
switch (*cur_src) {
case 'b':
*cur_dest++ = '\b';
cur_src++;
break;
case 'f':
*cur_dest++ = '\f';
cur_src++;
break;
case 'n':
*cur_dest++ = '\n';
cur_src++;
break;
case 'r':
*cur_dest++ = '\r';
cur_src++;
break;
case 't':
*cur_dest++ = '\t';
cur_src++;
break;
case 'u': {
cur_src++;
if (cur_src + 4 > end) {
return Status::Error("\\u has less than 4 symbols");
}
int num = 0;
for (int i = 0; i < 4; i++, cur_src++) {
int d = hex_to_int(*cur_src);
if (d == 16) {
return Status::Error("Invalid \\u -- not hex digit");
}
num = num * 16 + d;
}
if (0xD7FF < num && num < 0xE000) {
if (cur_src + 6 <= end && cur_src[0] == '\\' && cur_src[1] == 'u') {
cur_src += 2;
int new_num = 0;
for (int i = 0; i < 4; i++, cur_src++) {
int d = hex_to_int(*cur_src);
if (d == 16) {
return Status::Error("Invalid \\u -- not hex digit");
}
new_num = new_num * 16 + d;
}
if (0xD7FF < new_num && new_num < 0xE000) {
num = (((num & 0x3FF) << 10) | (new_num & 0x3FF)) + 0x10000;
} else {
cur_src -= 6;
}
}
}
if (num < 128) {
*cur_dest++ = static_cast<char>(num);
} else if (num < 0x800) {
*cur_dest++ = static_cast<char>(0xc0 + (num >> 6));
*cur_dest++ = static_cast<char>(0x80 + (num & 63));
} else if (num <= 0xffff) {
*cur_dest++ = static_cast<char>(0xe0 + (num >> 12));
*cur_dest++ = static_cast<char>(0x80 + ((num >> 6) & 63));
*cur_dest++ = static_cast<char>(0x80 + (num & 63));
} else {
*cur_dest++ = static_cast<char>(0xf0 + (num >> 18));
*cur_dest++ = static_cast<char>(0x80 + ((num >> 12) & 63));
*cur_dest++ = static_cast<char>(0x80 + ((num >> 6) & 63));
*cur_dest++ = static_cast<char>(0x80 + (num & 63));
}
break;
}
default:
*cur_dest++ = *cur_src++;
break;
}
} else {
*cur_dest++ = *cur_src++;
}
}
UNREACHABLE();
return {};
}
Status json_string_skip(Parser &parser) {
if (!parser.try_skip('"')) {
return Status::Error("Opening '\"' expected");
}
auto *begin_src = parser.data().data();
auto *cur_src = begin_src;
auto *end_src = parser.data().end();
auto *end = cur_src;
while (end < end_src && *end != '"') {
if (*end == '\\') {
end++;
}
end++;
}
if (end >= end_src) {
return Status::Error("Closing '\"' not found");
}
parser.advance(end + 1 - cur_src);
end_src = end;
while (cur_src != end_src) {
auto *slash = static_cast<char *>(std::memchr(cur_src, '\\', end_src - cur_src));
if (slash == nullptr) {
slash = end_src;
}
cur_src = slash;
if (cur_src != end_src) {
cur_src++;
if (cur_src == end_src) {
// TODO UNREACHABLE();
return Status::Error("Unexpected end of string");
}
switch (*cur_src) {
case '"':
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
cur_src++;
break;
case 'u': {
cur_src++;
if (cur_src + 4 > end_src) {
return Status::Error("\\u has less than 4 symbols");
}
int num = 0;
for (int i = 0; i < 4; i++, cur_src++) {
int d = hex_to_int(*cur_src);
if (d == 16) {
return Status::Error("Invalid \\u -- not hex digit");
}
num = num * 16 + d;
}
if (0xD7FF < num && num < 0xE000) {
if (cur_src + 6 <= end_src && cur_src[0] == '\\' && cur_src[1] == 'u') {
cur_src += 2;
int new_num = 0;
for (int i = 0; i < 4; i++, cur_src++) {
int d = hex_to_int(*cur_src);
if (d == 16) {
return Status::Error("Invalid \\u -- not hex digit");
}
new_num = new_num * 16 + d;
}
if (0xD7FF < new_num && new_num < 0xE000) {
// num = (((num & 0x3FF) << 10) | (new_num & 0x3FF)) + 0x10000;
} else {
cur_src -= 6;
}
}
}
break;
}
}
}
}
return Status::OK();
}
Result<JsonValue> do_json_decode(Parser &parser, int32 max_depth) {
if (max_depth < 0) {
return Status::Error("Too big object depth");
}
parser.skip_whitespaces();
switch (parser.peek_char()) {
case 'f':
if (parser.try_skip("false")) {
return JsonValue::create_boolean(false);
}
return Status::Error("Token starts with 'f' -- false expected");
case 't':
if (parser.try_skip("true")) {
return JsonValue::create_boolean(true);
}
return Status::Error("Token starts with 't' -- true expected");
case 'n':
if (parser.try_skip("null")) {
return JsonValue();
}
return Status::Error("Token starts with 'n' -- null expected");
case '"': {
TRY_RESULT(slice, json_string_decode(parser));
return JsonValue::create_string(slice);
}
case '[': {
parser.skip('[');
parser.skip_whitespaces();
std::vector<JsonValue> res;
if (parser.try_skip(']')) {
return JsonValue::create_array(std::move(res));
}
while (true) {
if (parser.empty()) {
return Status::Error("Unexpected string end");
}
TRY_RESULT(value, do_json_decode(parser, max_depth - 1));
res.emplace_back(std::move(value));
parser.skip_whitespaces();
if (parser.try_skip(']')) {
break;
}
if (parser.try_skip(',')) {
parser.skip_whitespaces();
continue;
}
if (parser.empty()) {
return Status::Error("Unexpected string end");
}
return Status::Error("Unexpected symbol while parsing JSON Array");
}
return JsonValue::create_array(std::move(res));
}
case '{': {
parser.skip('{');
parser.skip_whitespaces();
if (parser.try_skip('}')) {
return JsonValue::make_object(JsonObject());
}
vector<std::pair<Slice, JsonValue>> field_values;
while (true) {
if (parser.empty()) {
return Status::Error("Unexpected string end");
}
TRY_RESULT(field, json_string_decode(parser));
parser.skip_whitespaces();
if (!parser.try_skip(':')) {
return Status::Error("':' expected");
}
TRY_RESULT(value, do_json_decode(parser, max_depth - 1));
field_values.emplace_back(field, std::move(value));
parser.skip_whitespaces();
if (parser.try_skip('}')) {
break;
}
if (parser.try_skip(',')) {
parser.skip_whitespaces();
continue;
}
if (parser.empty()) {
return Status::Error("Unexpected string end");
}
return Status::Error("Unexpected symbol while parsing JSON Object");
}
return JsonValue::make_object(JsonObject(std::move(field_values)));
}
case '-':
case '+':
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': {
auto num = parser.read_while(
[](char c) { return c == '-' || ('0' <= c && c <= '9') || c == 'e' || c == 'E' || c == '+' || c == '.'; });
return JsonValue::create_number(num);
}
case 0:
return Status::Error("Unexpected string end");
default: {
char next = parser.peek_char();
if (0 < next && next < 127) {
return Status::Error(PSLICE() << "Unexpected symbol '" << parser.peek_char() << "'");
} else {
return Status::Error("Unexpected symbol");
}
}
}
UNREACHABLE();
}
Status do_json_skip(Parser &parser, int32 max_depth) {
if (max_depth < 0) {
return Status::Error("Too big object depth");
}
parser.skip_whitespaces();
switch (parser.peek_char()) {
case 'f':
if (parser.try_skip("false")) {
return Status::OK();
}
return Status::Error("Starts with 'f' -- false expected");
case 't':
if (parser.try_skip("true")) {
return Status::OK();
}
return Status::Error("Starts with 't' -- true expected");
case 'n':
if (parser.try_skip("null")) {
return Status::OK();
}
return Status::Error("Starts with 'n' -- null expected");
case '"': {
return json_string_skip(parser);
}
case '[': {
parser.skip('[');
parser.skip_whitespaces();
if (parser.try_skip(']')) {
return Status::OK();
}
while (true) {
if (parser.empty()) {
return Status::Error("Unexpected end");
}
TRY_STATUS(do_json_skip(parser, max_depth - 1));
parser.skip_whitespaces();
if (parser.try_skip(']')) {
break;
}
if (parser.try_skip(',')) {
parser.skip_whitespaces();
continue;
}
return Status::Error("Unexpected symbol");
}
return Status::OK();
}
case '{': {
parser.skip('{');
parser.skip_whitespaces();
if (parser.try_skip('}')) {
return Status::OK();
}
while (true) {
if (parser.empty()) {
return Status::Error("Unexpected end");
}
TRY_STATUS(json_string_skip(parser));
parser.skip_whitespaces();
if (!parser.try_skip(':')) {
return Status::Error("':' expected");
}
TRY_STATUS(do_json_skip(parser, max_depth - 1));
parser.skip_whitespaces();
if (parser.try_skip('}')) {
break;
}
if (parser.try_skip(',')) {
parser.skip_whitespaces();
continue;
}
return Status::Error("Unexpected symbol");
}
return Status::OK();
}
case '-':
case '+':
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': {
parser.read_while(
[](char c) { return c == '-' || ('0' <= c && c <= '9') || c == 'e' || c == 'E' || c == '+' || c == '.'; });
return Status::OK();
}
case 0:
return Status::Error("Unexpected end");
default: {
char next = parser.peek_char();
if (0 < next && next < 127) {
return Status::Error(PSLICE() << "Unexpected symbol '" << parser.peek_char() << "'");
} else {
return Status::Error("Unexpected symbol");
}
}
}
return Status::Error("Can't parse");
}
Slice JsonValue::get_type_name(Type type) {
switch (type) {
case Type::Null:
return Slice("Null");
case Type::Number:
return Slice("Number");
case Type::Boolean:
return Slice("Boolean");
case Type::String:
return Slice("String");
case Type::Array:
return Slice("Array");
case Type::Object:
return Slice("Object");
default:
UNREACHABLE();
return Slice("Unknown");
}
}
JsonValue JsonObject::extract_field(Slice name) {
for (auto &field_value : field_values_) {
if (field_value.first == name) {
return std::move(field_value.second);
}
}
return JsonValue();
}
Result<JsonValue> JsonObject::extract_optional_field(Slice name, JsonValueType type) {
for (auto &field_value : field_values_) {
if (field_value.first == name) {
if (type != JsonValue::Type::Null && field_value.second.type() != type) {
return Status::Error(400, PSLICE()
<< "Field \"" << name << "\" must be of type " << JsonValue::get_type_name(type));
}
return std::move(field_value.second);
}
}
return JsonValue();
}
Result<JsonValue> JsonObject::extract_required_field(Slice name, JsonValueType type) {
for (auto &field_value : field_values_) {
if (field_value.first == name) {
if (type != JsonValue::Type::Null && field_value.second.type() != type) {
return Status::Error(400, PSLICE()
<< "Field \"" << name << "\" must be of type " << JsonValue::get_type_name(type));
}
return std::move(field_value.second);
}
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << "\"");
}
const JsonValue *JsonObject::get_field(Slice name) const {
for (auto &field_value : field_values_) {
if (field_value.first == name) {
return &field_value.second;
}
}
return nullptr;
}
bool JsonObject::has_field(Slice name) const {
return get_field(name) != nullptr;
}
Result<bool> JsonObject::get_optional_bool_field(Slice name, bool default_value) const {
auto value = get_field(name);
if (value != nullptr) {
if (value->type() == JsonValue::Type::Boolean) {
return value->get_boolean();
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Boolean");
}
return default_value;
}
Result<bool> JsonObject::get_required_bool_field(Slice name) const {
auto value = get_field(name);
if (value != nullptr) {
if (value->type() == JsonValue::Type::Boolean) {
return value->get_boolean();
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Boolean");
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<int32> JsonObject::get_optional_int_field(Slice name, int32 default_value) const {
auto value = get_field(name);
if (value != nullptr) {
if (value->type() == JsonValue::Type::String) {
return to_integer_safe<int32>(value->get_string());
}
if (value->type() == JsonValue::Type::Number) {
return to_integer_safe<int32>(value->get_number());
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Number");
}
return default_value;
}
Result<int32> JsonObject::get_required_int_field(Slice name) const {
auto value = get_field(name);
if (value != nullptr) {
if (value->type() == JsonValue::Type::String) {
return to_integer_safe<int32>(value->get_string());
}
if (value->type() == JsonValue::Type::Number) {
return to_integer_safe<int32>(value->get_number());
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Number");
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<int64> JsonObject::get_optional_long_field(Slice name, int64 default_value) const {
auto value = get_field(name);
if (value != nullptr) {
if (value->type() == JsonValue::Type::String) {
return to_integer_safe<int64>(value->get_string());
}
if (value->type() == JsonValue::Type::Number) {
return to_integer_safe<int64>(value->get_number());
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be a Number");
}
return default_value;
}
Result<int64> JsonObject::get_required_long_field(Slice name) const {
auto value = get_field(name);
if (value != nullptr) {
if (value->type() == JsonValue::Type::String) {
return to_integer_safe<int64>(value->get_string());
}
if (value->type() == JsonValue::Type::Number) {
return to_integer_safe<int64>(value->get_number());
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be a Number");
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<double> JsonObject::get_optional_double_field(Slice name, double default_value) const {
auto value = get_field(name);
if (value != nullptr) {
if (value->type() == JsonValue::Type::Number) {
return to_double(value->get_number());
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Number");
}
return default_value;
}
Result<double> JsonObject::get_required_double_field(Slice name) const {
auto value = get_field(name);
if (value != nullptr) {
if (value->type() == JsonValue::Type::Number) {
return to_double(value->get_number());
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Number");
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<string> JsonObject::get_optional_string_field(Slice name, string default_value) const {
auto value = get_field(name);
if (value != nullptr) {
if (value->type() == JsonValue::Type::String) {
return value->get_string().str();
}
if (value->type() == JsonValue::Type::Number) {
return value->get_number().str();
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type String");
}
return std::move(default_value);
}
Result<string> JsonObject::get_required_string_field(Slice name) const {
auto value = get_field(name);
if (value != nullptr) {
if (value->type() == JsonValue::Type::String) {
return value->get_string().str();
}
if (value->type() == JsonValue::Type::Number) {
return value->get_number().str();
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type String");
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
void JsonObject::foreach(const std::function<void(Slice name, const JsonValue &value)> &callback) const {
for (auto &field_value : field_values_) {
callback(field_value.first, field_value.second);
}
}
bool has_json_object_field(const JsonObject &object, Slice name) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
return true;
}
}
return false;
}
JsonValue get_json_object_field_force(JsonObject &object, Slice name) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
return std::move(field_value.second);
}
}
return JsonValue();
}
Result<JsonValue> get_json_object_field(JsonObject &object, Slice name, JsonValue::Type type, bool is_optional) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (type != JsonValue::Type::Null && field_value.second.type() != type) {
return Status::Error(400, PSLICE()
<< "Field \"" << name << "\" must be of type " << JsonValue::get_type_name(type));
}
return std::move(field_value.second);
}
}
if (!is_optional) {
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
return JsonValue();
}
Result<bool> get_json_object_bool_field(const JsonObject &object, Slice name, bool is_optional, bool default_value) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (field_value.second.type() != JsonValue::Type::Boolean) {
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Boolean");
}
return field_value.second.get_boolean();
}
}
if (is_optional) {
return default_value;
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<int32> get_json_object_int_field(const JsonObject &object, Slice name, bool is_optional, int32 default_value) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (field_value.second.type() == JsonValue::Type::String) {
return to_integer_safe<int32>(field_value.second.get_string());
}
if (field_value.second.type() == JsonValue::Type::Number) {
return to_integer_safe<int32>(field_value.second.get_number());
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Number");
}
}
if (is_optional) {
return default_value;
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<int64> get_json_object_long_field(const JsonObject &object, Slice name, bool is_optional, int64 default_value) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (field_value.second.type() == JsonValue::Type::String) {
return to_integer_safe<int64>(field_value.second.get_string());
}
if (field_value.second.type() == JsonValue::Type::Number) {
return to_integer_safe<int64>(field_value.second.get_number());
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be a Number");
}
}
if (is_optional) {
return default_value;
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<double> get_json_object_double_field(const JsonObject &object, Slice name, bool is_optional,
double default_value) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (field_value.second.type() != JsonValue::Type::Number) {
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Number");
}
return to_double(field_value.second.get_number());
}
}
if (is_optional) {
return default_value;
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<string> get_json_object_string_field(const JsonObject &object, Slice name, bool is_optional,
string default_value) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (field_value.second.type() == JsonValue::Type::String) {
return field_value.second.get_string().str();
}
if (field_value.second.type() == JsonValue::Type::Number) {
return field_value.second.get_number().str();
}
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type String");
}
}
if (is_optional) {
return std::move(default_value);
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
} // namespace td