mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-05-01 23:31:03 +02:00
Merge pull request #301 from redboltz/json_escape
Added JSON escape for values between 0x00 and 0x1f, and 0x7f.
This commit is contained in:
commit
e0a2c2a4bf
@ -29,6 +29,7 @@
|
||||
#include <limits>
|
||||
#include <ostream>
|
||||
#include <typeinfo>
|
||||
#include <iomanip>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
@ -724,9 +725,15 @@ inline std::ostream& operator<< (std::ostream& s, const msgpack::object& o)
|
||||
case '\t':
|
||||
s << "\\t";
|
||||
break;
|
||||
default:
|
||||
s << c;
|
||||
break;
|
||||
default: {
|
||||
unsigned int code = static_cast<unsigned int>(c);
|
||||
if (code < 0x20 || code == 0x7f) {
|
||||
s << "\\u" << std::hex << std::setw(4) << std::setfill('0') << (code & 0xff);
|
||||
}
|
||||
else {
|
||||
s << c;
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
s << '"';
|
||||
|
@ -30,3 +30,17 @@ TEST(json, escape)
|
||||
ss << o;
|
||||
EXPECT_EQ(ss.str(), "\"\\\"\\\\\\/\\b\\f\\n\\r\\tabc\"");
|
||||
}
|
||||
|
||||
TEST(json, escape_cc)
|
||||
{
|
||||
std::string s;
|
||||
for (int i = 0; i < 0x20; ++i)
|
||||
s.push_back(static_cast<char>(i));
|
||||
s.push_back(0x7f);
|
||||
s.push_back(0x20);
|
||||
msgpack::zone z;
|
||||
msgpack::object o(s, z);
|
||||
std::stringstream ss;
|
||||
ss << o;
|
||||
EXPECT_EQ(ss.str(), "\"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f\\u007f \"");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user