Skip to content

Commit 83ee2c8

Browse files
committed
Merge pull request #274 from redboltz/fix_issue_273
Fixed #273
2 parents 0bfbd8d + 993d007 commit 83ee2c8

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

include/msgpack/object.hpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,40 @@ inline std::ostream& operator<< (std::ostream& s, const msgpack::object& o)
583583
break;
584584

585585
case msgpack::type::STR:
586-
(s << '"').write(o.via.str.ptr, o.via.str.size) << '"';
586+
s << '"';
587+
for (uint32_t i = 0; i < o.via.str.size; ++i) {
588+
char c = o.via.str.ptr[i];
589+
switch (c) {
590+
case '\\':
591+
s << "\\\\";
592+
break;
593+
case '"':
594+
s << "\\\"";
595+
break;
596+
case '/':
597+
s << "\\/";
598+
break;
599+
case '\b':
600+
s << "\\b";
601+
break;
602+
case '\f':
603+
s << "\\f";
604+
break;
605+
case '\n':
606+
s << "\\n";
607+
break;
608+
case '\r':
609+
s << "\\r";
610+
break;
611+
case '\t':
612+
s << "\\t";
613+
break;
614+
default:
615+
s << c;
616+
break;
617+
}
618+
}
619+
s << '"';
587620
break;
588621

589622
case msgpack::type::BIN:
@@ -612,11 +645,11 @@ inline std::ostream& operator<< (std::ostream& s, const msgpack::object& o)
612645
s << "{";
613646
if(o.via.map.size != 0) {
614647
msgpack::object_kv* p(o.via.map.ptr);
615-
s << p->key << "=>" << p->val;
648+
s << p->key << ':' << p->val;
616649
++p;
617650
for(msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size);
618651
p < pend; ++p) {
619-
s << ", " << p->key << "=>" << p->val;
652+
s << ", " << p->key << ':' << p->val;
620653
}
621654
}
622655
s << "}";

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ LIST (APPEND check_PROGRAMS
2929
msgpack_c.cpp
3030
reference.cpp
3131
limit.cpp
32+
json.cpp
3233
)
3334

3435
IF (MSGPACK_BOOST)

test/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ check_PROGRAMS = \
2626
reference_cpp11 \
2727
reference \
2828
limit \
29+
json \
2930
iterator_cpp11 \
3031
boost_optional
3132

@@ -78,6 +79,8 @@ reference_cpp11_SOURCES = reference_cpp11.cpp
7879

7980
limit_SOURCES = limit.cpp
8081

82+
json_SOURCES = json.cpp
83+
8184
iterator_cpp11_SOURCES = iterator_cpp11.cpp
8285

8386
boost_optional_SOURCES = boost_optional.cpp

test/json.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <msgpack.hpp>
2+
#include <fstream>
3+
#include <sstream>
4+
#include <gtest/gtest.h>
5+
6+
TEST(json, basic_elements)
7+
{
8+
typedef std::map<std::string, int> map_s_i;
9+
map_s_i msi;
10+
msi.insert(map_s_i::value_type("Hello", 789));
11+
msi.insert(map_s_i::value_type("World", -789));
12+
13+
msgpack::type::tuple<int, int, double, double, bool, bool, std::string, map_s_i>
14+
t1(12, -34, 1.23, -4.56, true, false, "ABC", msi);
15+
16+
msgpack::zone z;
17+
msgpack::object o(t1, z);
18+
std::stringstream ss;
19+
ss << o;
20+
EXPECT_EQ(ss.str(), "[12, -34, 1.23, -4.56, true, false, \"ABC\", {\"Hello\":789, \"World\":-789}]");
21+
}
22+
23+
TEST(json, escape)
24+
{
25+
std::string s = "\"\\/\b\f\n\r\tabc";
26+
27+
msgpack::zone z;
28+
msgpack::object o(s, z);
29+
std::stringstream ss;
30+
ss << o;
31+
EXPECT_EQ(ss.str(), "\"\\\"\\\\\\/\\b\\f\\n\\r\\tabc\"");
32+
}

0 commit comments

Comments
 (0)