Fix json_decode.

GitOrigin-RevId: 82f3beeb83aaa6052207123f22a44da88972e545
This commit is contained in:
levlam 2018-03-09 20:43:24 +03:00
parent 2a58cfd1b9
commit 8a8ea50a81
2 changed files with 15 additions and 4 deletions

View File

@ -720,8 +720,11 @@ inline Result<JsonValue> json_decode(MutableSlice from) {
Parser parser(from); Parser parser(from);
const int32 DEFAULT_MAX_DEPTH = 100; const int32 DEFAULT_MAX_DEPTH = 100;
auto result = do_json_decode(parser, DEFAULT_MAX_DEPTH); auto result = do_json_decode(parser, DEFAULT_MAX_DEPTH);
if (result.is_ok() && !parser.empty()) { if (result.is_ok()) {
return Status::Error("Expected string end"); parser.skip_whitespaces();
if (!parser.empty()) {
return Status::Error("Expected string end");
}
} }
return result; return result;
} }

View File

@ -17,7 +17,7 @@ REGISTER_TESTS(json)
using namespace td; using namespace td;
static void decode_encode(string str) { static void decode_encode(string str, string result = "") {
auto str_copy = str; auto str_copy = str;
auto r_value = json_decode(str_copy); auto r_value = json_decode(str_copy);
ASSERT_TRUE(r_value.is_ok()); ASSERT_TRUE(r_value.is_ok());
@ -26,7 +26,10 @@ static void decode_encode(string str) {
return; return;
} }
auto new_str = json_encode<string>(r_value.ok()); auto new_str = json_encode<string>(r_value.ok());
ASSERT_EQ(str, new_str); if (result.empty()) {
result = str;
}
ASSERT_EQ(result, new_str);
} }
TEST(JSON, array) { TEST(JSON, array) {
@ -83,4 +86,9 @@ TEST(JSON, kphp) {
decode_encode( decode_encode(
"{\"keyboard\":[[\"\\u2022 abcdefg\"],[\"\\u2022 hijklmnop\"],[\"\\u2022 " "{\"keyboard\":[[\"\\u2022 abcdefg\"],[\"\\u2022 hijklmnop\"],[\"\\u2022 "
"qrstuvwxyz\"]],\"one_time_keyboard\":true}"); "qrstuvwxyz\"]],\"one_time_keyboard\":true}");
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}");
} }