Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_json: use letter escapes #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/eosio/to_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ void to_json(std::string_view sv, S& stream) {
stream.write("\\\"", 2);
} else if (*begin == '\\') {
stream.write("\\\\", 2);
} else if (*begin == '\b') {
stream.write("\\b", 2);
} else if (*begin == '\f') {
stream.write("\\f", 2);
} else if (*begin == '\n') {
stream.write("\\n", 2);
} else if (*begin == '\r') {
stream.write("\\r", 2);
} else if (*begin == '\t') {
stream.write("\\t", 2);
} else {
stream.write("\\u00", 4);
stream.write(hex_digits[(unsigned char)(*begin) >> 4]);
Expand Down
4 changes: 3 additions & 1 deletion src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,9 @@ void check_types() {
check_type(context, 0, "string", R"("' + '*'.repeat(128) + '")");
check_type(context, 0, "string", R"("\u0000 这是一个测试 Это тест هذا اختبار 👍")");
check(abieos_bin_to_json(context, 0, "string", "\x11invalid utf8: \xff\xfe\xfd", 18) == std::string(R"("invalid utf8: ???")"), "invalid utf8");
check(abieos_bin_to_json(context, 0, "string", "\4\xe8\xbf\x99\n", 5) == std::string("\"\xe8\xbf\x99\\u000A\""), "escaping");
check(abieos_bin_to_json(context, 0, "string", "\x08\xe8\xbf\x99\b\f\n\r\t", 9) ==
std::string("\"\xe8\xbf\x99\\b\\f\\n\\r\\t\""),
"escaping");
check_error(context, "Stream overrun", [&] { return abieos_hex_to_json(context, 0, "string", "01"); });
check_type(context, 0, "checksum160", R"("0000000000000000000000000000000000000000")");
check_type(context, 0, "checksum160", R"("123456789ABCDEF01234567890ABCDEF70123456")");
Expand Down