Skip to content

Commit

Permalink
Merge pull request #274 from redboltz/fix_issue_273
Browse files Browse the repository at this point in the history
Fixed #273
  • Loading branch information
nobu-k committed Apr 28, 2015
2 parents 0bfbd8d + 993d007 commit 83ee2c8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
39 changes: 36 additions & 3 deletions include/msgpack/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 << "}";
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ LIST (APPEND check_PROGRAMS
msgpack_c.cpp
reference.cpp
limit.cpp
json.cpp
)

IF (MSGPACK_BOOST)
Expand Down
3 changes: 3 additions & 0 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ check_PROGRAMS = \
reference_cpp11 \
reference \
limit \
json \
iterator_cpp11 \
boost_optional

Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions test/json.cpp
Original file line number Diff line number Diff line change
@@ -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\"");
}

0 comments on commit 83ee2c8

Please sign in to comment.