2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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/tests.h"
|
|
|
|
|
|
|
|
#include "td/utils/JsonBuilder.h"
|
|
|
|
#include "td/utils/logging.h"
|
2018-12-11 21:18:58 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
|
|
|
#include <tuple>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
REGISTER_TESTS(json)
|
|
|
|
|
|
|
|
using namespace td;
|
|
|
|
|
2018-03-09 18:43:24 +01:00
|
|
|
static void decode_encode(string str, string result = "") {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto str_copy = str;
|
|
|
|
auto r_value = json_decode(str_copy);
|
|
|
|
ASSERT_TRUE(r_value.is_ok());
|
|
|
|
if (r_value.is_error()) {
|
|
|
|
LOG(INFO) << r_value.error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto new_str = json_encode<string>(r_value.ok());
|
2018-03-09 18:43:24 +01:00
|
|
|
if (result.empty()) {
|
|
|
|
result = str;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(result, new_str);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(JSON, array) {
|
|
|
|
char tmp[1000];
|
2018-10-30 15:22:44 +01:00
|
|
|
StringBuilder sb(MutableSlice{tmp, sizeof(tmp)});
|
2018-12-31 20:04:05 +01:00
|
|
|
JsonBuilder jb(std::move(sb));
|
|
|
|
jb.enter_value().enter_array() << "Hello" << -123;
|
|
|
|
ASSERT_EQ(jb.string_builder().is_error(), false);
|
|
|
|
auto encoded = jb.string_builder().as_cslice().str();
|
|
|
|
ASSERT_EQ("[\"Hello\",-123]", encoded);
|
|
|
|
decode_encode(encoded);
|
|
|
|
}
|
|
|
|
TEST(JSON, object) {
|
|
|
|
char tmp[1000];
|
2018-10-30 15:22:44 +01:00
|
|
|
StringBuilder sb(MutableSlice{tmp, sizeof(tmp)});
|
2018-12-31 20:04:05 +01:00
|
|
|
JsonBuilder jb(std::move(sb));
|
|
|
|
auto c = jb.enter_object();
|
2019-12-08 06:50:43 +01:00
|
|
|
c("key", "value");
|
2019-12-08 07:02:20 +01:00
|
|
|
c("1", 2);
|
2018-12-31 20:04:05 +01:00
|
|
|
c.leave();
|
|
|
|
ASSERT_EQ(jb.string_builder().is_error(), false);
|
|
|
|
auto encoded = jb.string_builder().as_cslice().str();
|
|
|
|
ASSERT_EQ("{\"key\":\"value\",\"1\":2}", encoded);
|
|
|
|
decode_encode(encoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(JSON, nested) {
|
|
|
|
char tmp[1000];
|
2018-10-30 15:22:44 +01:00
|
|
|
StringBuilder sb(MutableSlice{tmp, sizeof(tmp)});
|
2018-12-31 20:04:05 +01:00
|
|
|
JsonBuilder jb(std::move(sb));
|
|
|
|
{
|
|
|
|
auto a = jb.enter_array();
|
|
|
|
a << 1;
|
|
|
|
{ a.enter_value().enter_array() << 2; }
|
|
|
|
a << 3;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(jb.string_builder().is_error(), false);
|
|
|
|
auto encoded = jb.string_builder().as_cslice().str();
|
|
|
|
ASSERT_EQ("[1,[2],3]", encoded);
|
|
|
|
decode_encode(encoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(JSON, kphp) {
|
|
|
|
decode_encode("[]");
|
|
|
|
decode_encode("[[]]");
|
|
|
|
decode_encode("{}");
|
|
|
|
decode_encode("{}");
|
|
|
|
decode_encode("\"\\n\"");
|
|
|
|
decode_encode(
|
|
|
|
"\""
|
|
|
|
"some long string \\t \\r \\\\ \\n \\f \\\" "
|
|
|
|
"\\u1234"
|
|
|
|
"\"");
|
|
|
|
decode_encode(
|
|
|
|
"{\"keyboard\":[[\"\\u2022 abcdefg\"],[\"\\u2022 hijklmnop\"],[\"\\u2022 "
|
|
|
|
"qrstuvwxyz\"]],\"one_time_keyboard\":true}");
|
2018-03-09 18:43:24 +01:00
|
|
|
decode_encode(
|
|
|
|
" \n { \"keyboard\" : \n [[ \"\\u2022 abcdefg\" ] , \n [ \"\\u2022 hijklmnop\" \n ],[ \n \"\\u2022 "
|
|
|
|
"qrstuvwxyz\"]], \n \"one_time_keyboard\"\n:\ntrue\n}\n \n",
|
|
|
|
"{\"keyboard\":[[\"\\u2022 abcdefg\"],[\"\\u2022 hijklmnop\"],[\"\\u2022 "
|
|
|
|
"qrstuvwxyz\"]],\"one_time_keyboard\":true}");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|