2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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)
|
|
|
|
//
|
|
|
|
#include "td/utils/JsonBuilder.h"
|
|
|
|
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
namespace td {
|
2019-02-15 19:49:02 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
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) {
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 a = ch;
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK((a & 0x40) != 0);
|
|
|
|
|
|
|
|
CHECK(pos + 1 < len);
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 b = static_cast<unsigned char>(s[++pos]);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK((b & 0xc0) == 0x80);
|
|
|
|
if ((a & 0x20) == 0) {
|
|
|
|
CHECK((a & 0x1e) > 0);
|
|
|
|
sb << JsonChar(((a & 0x1f) << 6) | (b & 0x3f));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(pos + 1 < len);
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 c = static_cast<unsigned char>(s[++pos]);
|
2018-12-31 20:04:05 +01:00
|
|
|
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);
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 d = static_cast<unsigned char>(s[++pos]);
|
2018-12-31 20:04:05 +01:00
|
|
|
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;
|
|
|
|
}
|
2019-02-15 19:49:02 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<MutableSlice> json_string_decode(Parser &parser) {
|
|
|
|
if (!parser.try_skip('"')) {
|
|
|
|
return Status::Error("Opening '\"' expected");
|
|
|
|
}
|
|
|
|
auto *cur_src = parser.data().data();
|
|
|
|
auto *end_src = parser.data().end();
|
|
|
|
auto *end = cur_src;
|
|
|
|
while (end < end_src && end[0] != '"') {
|
|
|
|
if (end[0] == '\\') {
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
if (end >= end_src) {
|
|
|
|
return Status::Error("Closing '\"' not found");
|
|
|
|
}
|
|
|
|
parser.advance(end + 1 - cur_src);
|
|
|
|
end_src = end;
|
|
|
|
|
|
|
|
auto *cur_dest = cur_src;
|
|
|
|
auto *begin_dest = cur_src;
|
|
|
|
|
|
|
|
while (cur_src != end_src) {
|
|
|
|
auto *slash = static_cast<char *>(std::memchr(cur_src, '\\', end_src - cur_src));
|
|
|
|
if (slash == nullptr) {
|
|
|
|
slash = end_src;
|
|
|
|
}
|
|
|
|
std::memmove(cur_dest, cur_src, slash - cur_src);
|
|
|
|
cur_dest += slash - cur_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 '/':
|
|
|
|
*cur_dest++ = *cur_src++;
|
|
|
|
break;
|
|
|
|
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_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2018-07-19 15:54:44 +02:00
|
|
|
} else if (num <= 0xffff) {
|
2018-12-31 20:04:05 +01:00
|
|
|
*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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(cur_dest <= end_src);
|
|
|
|
return MutableSlice(begin_dest, cur_dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
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':
|
2020-07-21 13:24:55 +02:00
|
|
|
if (parser.try_skip("false")) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return JsonValue::create_boolean(false);
|
|
|
|
}
|
2018-12-10 02:01:02 +01:00
|
|
|
return Status::Error("Token starts with 'f' -- false expected");
|
2018-12-31 20:04:05 +01:00
|
|
|
case 't':
|
2020-07-21 13:24:55 +02:00
|
|
|
if (parser.try_skip("true")) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return JsonValue::create_boolean(true);
|
|
|
|
}
|
2018-12-10 02:01:02 +01:00
|
|
|
return Status::Error("Token starts with 't' -- true expected");
|
2018-12-31 20:04:05 +01:00
|
|
|
case 'n':
|
2020-07-21 13:24:55 +02:00
|
|
|
if (parser.try_skip("null")) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return JsonValue();
|
|
|
|
}
|
2018-12-10 02:01:02 +01:00
|
|
|
return Status::Error("Token starts with 'n' -- null expected");
|
2018-12-31 20:04:05 +01:00
|
|
|
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()) {
|
2018-12-10 02:01:02 +01:00
|
|
|
return Status::Error("Unexpected string end");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2018-12-10 02:01:02 +01:00
|
|
|
if (parser.empty()) {
|
|
|
|
return Status::Error("Unexpected string end");
|
|
|
|
}
|
|
|
|
return Status::Error("Unexpected symbol while parsing JSON Array");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return JsonValue::create_array(std::move(res));
|
|
|
|
}
|
|
|
|
case '{': {
|
|
|
|
parser.skip('{');
|
|
|
|
parser.skip_whitespaces();
|
2023-07-31 12:31:48 +02:00
|
|
|
JsonObject res;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (parser.try_skip('}')) {
|
|
|
|
return JsonValue::make_object(std::move(res));
|
|
|
|
}
|
|
|
|
while (true) {
|
|
|
|
if (parser.empty()) {
|
2018-12-10 02:01:02 +01:00
|
|
|
return Status::Error("Unexpected string end");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-07-31 12:31:48 +02:00
|
|
|
TRY_RESULT(field, json_string_decode(parser));
|
2018-12-31 20:04:05 +01:00
|
|
|
parser.skip_whitespaces();
|
|
|
|
if (!parser.try_skip(':')) {
|
|
|
|
return Status::Error("':' expected");
|
|
|
|
}
|
|
|
|
TRY_RESULT(value, do_json_decode(parser, max_depth - 1));
|
2023-07-31 12:31:48 +02:00
|
|
|
res.field_values_.emplace_back(field, std::move(value));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
parser.skip_whitespaces();
|
|
|
|
if (parser.try_skip('}')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (parser.try_skip(',')) {
|
|
|
|
parser.skip_whitespaces();
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-10 02:01:02 +01:00
|
|
|
if (parser.empty()) {
|
|
|
|
return Status::Error("Unexpected string end");
|
|
|
|
}
|
|
|
|
return Status::Error("Unexpected symbol while parsing JSON Object");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return JsonValue::make_object(std::move(res));
|
|
|
|
}
|
|
|
|
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:
|
2018-12-10 02:01:02 +01:00
|
|
|
return Status::Error("Unexpected string end");
|
2018-12-31 20:04:05 +01:00
|
|
|
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':
|
2020-07-21 13:24:55 +02:00
|
|
|
if (parser.try_skip("false")) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
return Status::Error("Starts with 'f' -- false expected");
|
|
|
|
case 't':
|
2020-07-21 13:24:55 +02:00
|
|
|
if (parser.try_skip("true")) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
return Status::Error("Starts with 't' -- true expected");
|
|
|
|
case 'n':
|
2020-07-21 13:24:55 +02:00
|
|
|
if (parser.try_skip("null")) {
|
2018-12-31 20:04:05 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 14:03:38 +01:00
|
|
|
bool has_json_object_field(const JsonObject &object, Slice name) {
|
2023-07-31 12:31:48 +02:00
|
|
|
for (auto &field_value : object.field_values_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (field_value.first == name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-17 21:22:42 +02:00
|
|
|
JsonValue get_json_object_field_force(JsonObject &object, Slice name) {
|
2023-07-31 12:31:48 +02:00
|
|
|
for (auto &field_value : object.field_values_) {
|
2019-10-17 21:22:42 +02:00
|
|
|
if (field_value.first == name) {
|
|
|
|
return std::move(field_value.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JsonValue();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<JsonValue> get_json_object_field(JsonObject &object, Slice name, JsonValue::Type type, bool is_optional) {
|
2023-07-31 12:31:48 +02:00
|
|
|
for (auto &field_value : object.field_values_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
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(JsonObject &object, Slice name, bool is_optional, bool default_value) {
|
|
|
|
TRY_RESULT(value, get_json_object_field(object, name, JsonValue::Type::Boolean, is_optional));
|
|
|
|
if (value.type() == JsonValue::Type::Null) {
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
return value.get_boolean();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<int32> get_json_object_int_field(JsonObject &object, Slice name, bool is_optional, int32 default_value) {
|
2023-07-31 12:31:48 +02:00
|
|
|
for (auto &field_value : object.field_values_) {
|
2019-03-25 16:09:46 +01:00
|
|
|
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) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return default_value;
|
|
|
|
}
|
2019-03-25 16:09:46 +01:00
|
|
|
return Status::Error(400, PSLICE() << "Can't find field \"" << name << "\"");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-26 01:57:38 +01:00
|
|
|
Result<int64> get_json_object_long_field(JsonObject &object, Slice name, bool is_optional, int64 default_value) {
|
2023-07-31 12:31:48 +02:00
|
|
|
for (auto &field_value : object.field_values_) {
|
2019-03-26 01:57:38 +01:00
|
|
|
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 << "\"");
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<double> get_json_object_double_field(JsonObject &object, Slice name, bool is_optional, double default_value) {
|
|
|
|
TRY_RESULT(value, get_json_object_field(object, name, JsonValue::Type::Number, is_optional));
|
|
|
|
if (value.type() == JsonValue::Type::Null) {
|
|
|
|
return default_value;
|
|
|
|
}
|
2018-02-03 21:01:33 +01:00
|
|
|
return to_double(value.get_number());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<string> get_json_object_string_field(JsonObject &object, Slice name, bool is_optional, string default_value) {
|
2023-07-31 12:31:48 +02:00
|
|
|
for (auto &field_value : object.field_values_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
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 default_value;
|
|
|
|
}
|
|
|
|
return Status::Error(400, PSLICE() << "Can't find field \"" << name << "\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|