Merge pull request #274 from redboltz/fix_issue_273

Fixed #273
This commit is contained in:
Nobuyuki Kubota 2015-04-27 23:47:43 -07:00
commit 83ee2c82df
4 changed files with 72 additions and 3 deletions

View File

@ -583,7 +583,40 @@ inline std::ostream& operator<< (std::ostream& s, const msgpack::object& o)
break;
case msgpack::type::STR:
(s << '"').write(o.via.str.ptr, o.via.str.size) << '"';
s << '"';
for (uint32_t i = 0; i < o.via.str.size; ++i) {
char c = o.via.str.ptr[i];
switch (c) {
case '\\':
s << "\\\\";
break;
case '"':
s << "\\\"";
break;
case '/':
s << "\\/";
break;
case '\b':
s << "\\b";
break;
case '\f':
s << "\\f";
break;
case '\n':
s << "\\n";
break;
case '\r':
s << "\\r";
break;
case '\t':
s << "\\t";
break;
default:
s << c;
break;
}
}
s << '"';
break;
case msgpack::type::BIN:
@ -612,11 +645,11 @@ inline std::ostream& operator<< (std::ostream& s, const msgpack::object& o)
s << "{";
if(o.via.map.size != 0) {
msgpack::object_kv* p(o.via.map.ptr);
s << p->key << "=>" << p->val;
s << p->key << ':' << p->val;
++p;
for(msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size);
p < pend; ++p) {
s << ", " << p->key << "=>" << p->val;
s << ", " << p->key << ':' << p->val;
}
}
s << "}";

View File

@ -29,6 +29,7 @@ LIST (APPEND check_PROGRAMS
msgpack_c.cpp
reference.cpp
limit.cpp
json.cpp
)
IF (MSGPACK_BOOST)

View File

@ -26,6 +26,7 @@ check_PROGRAMS = \
reference_cpp11 \
reference \
limit \
json \
iterator_cpp11 \
boost_optional
@ -78,6 +79,8 @@ reference_cpp11_SOURCES = reference_cpp11.cpp
limit_SOURCES = limit.cpp
json_SOURCES = json.cpp
iterator_cpp11_SOURCES = iterator_cpp11.cpp
boost_optional_SOURCES = boost_optional.cpp

32
test/json.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <msgpack.hpp>
#include <fstream>
#include <sstream>
#include <gtest/gtest.h>
TEST(json, basic_elements)
{
typedef std::map<std::string, int> map_s_i;
map_s_i msi;
msi.insert(map_s_i::value_type("Hello", 789));
msi.insert(map_s_i::value_type("World", -789));
msgpack::type::tuple<int, int, double, double, bool, bool, std::string, map_s_i>
t1(12, -34, 1.23, -4.56, true, false, "ABC", msi);
msgpack::zone z;
msgpack::object o(t1, z);
std::stringstream ss;
ss << o;
EXPECT_EQ(ss.str(), "[12, -34, 1.23, -4.56, true, false, \"ABC\", {\"Hello\":789, \"World\":-789}]");
}
TEST(json, escape)
{
std::string s = "\"\\/\b\f\n\r\tabc";
msgpack::zone z;
msgpack::object o(s, z);
std::stringstream ss;
ss << o;
EXPECT_EQ(ss.str(), "\"\\\"\\\\\\/\\b\\f\\n\\r\\tabc\"");
}