Skip to content

Commit

Permalink
🐛 properly pass serialize_binary to dump function #2067
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Apr 27, 2020
1 parent d9d1279 commit 2e5727d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
16 changes: 8 additions & 8 deletions include/nlohmann/detail/output/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class serializer
o->write_character('\"');
dump_escaped(i->first, ensure_ascii);
o->write_characters("\": ", 3);
dump(i->second, true, ensure_ascii, indent_step, new_indent);
dump(i->second, true, ensure_ascii, indent_step, new_indent, serialize_binary);
o->write_characters(",\n", 2);
}

Expand All @@ -138,7 +138,7 @@ class serializer
o->write_character('\"');
dump_escaped(i->first, ensure_ascii);
o->write_characters("\": ", 3);
dump(i->second, true, ensure_ascii, indent_step, new_indent);
dump(i->second, true, ensure_ascii, indent_step, new_indent, serialize_binary);

o->write_character('\n');
o->write_characters(indent_string.c_str(), current_indent);
Expand All @@ -155,7 +155,7 @@ class serializer
o->write_character('\"');
dump_escaped(i->first, ensure_ascii);
o->write_characters("\":", 2);
dump(i->second, false, ensure_ascii, indent_step, current_indent);
dump(i->second, false, ensure_ascii, indent_step, current_indent, serialize_binary);
o->write_character(',');
}

Expand All @@ -165,7 +165,7 @@ class serializer
o->write_character('\"');
dump_escaped(i->first, ensure_ascii);
o->write_characters("\":", 2);
dump(i->second, false, ensure_ascii, indent_step, current_indent);
dump(i->second, false, ensure_ascii, indent_step, current_indent, serialize_binary);

o->write_character('}');
}
Expand Down Expand Up @@ -197,14 +197,14 @@ class serializer
i != val.m_value.array->cend() - 1; ++i)
{
o->write_characters(indent_string.c_str(), new_indent);
dump(*i, true, ensure_ascii, indent_step, new_indent);
dump(*i, true, ensure_ascii, indent_step, new_indent, serialize_binary);
o->write_characters(",\n", 2);
}

// last element
assert(not val.m_value.array->empty());
o->write_characters(indent_string.c_str(), new_indent);
dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent, serialize_binary);

o->write_character('\n');
o->write_characters(indent_string.c_str(), current_indent);
Expand All @@ -218,13 +218,13 @@ class serializer
for (auto i = val.m_value.array->cbegin();
i != val.m_value.array->cend() - 1; ++i)
{
dump(*i, false, ensure_ascii, indent_step, current_indent);
dump(*i, false, ensure_ascii, indent_step, current_indent, serialize_binary);
o->write_character(',');
}

// last element
assert(not val.m_value.array->empty());
dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent, serialize_binary);

o->write_character(']');
}
Expand Down
4 changes: 2 additions & 2 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2237,11 +2237,11 @@ class basic_json

if (indent >= 0)
{
s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent), serialize_binary);
s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent), 0, serialize_binary);
}
else
{
s.dump(*this, false, ensure_ascii, 0, serialize_binary);
s.dump(*this, false, ensure_ascii, 0, 0, serialize_binary);
}

return result;
Expand Down
20 changes: 10 additions & 10 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14760,7 +14760,7 @@ class serializer
o->write_character('\"');
dump_escaped(i->first, ensure_ascii);
o->write_characters("\": ", 3);
dump(i->second, true, ensure_ascii, indent_step, new_indent);
dump(i->second, true, ensure_ascii, indent_step, new_indent, serialize_binary);
o->write_characters(",\n", 2);
}

Expand All @@ -14771,7 +14771,7 @@ class serializer
o->write_character('\"');
dump_escaped(i->first, ensure_ascii);
o->write_characters("\": ", 3);
dump(i->second, true, ensure_ascii, indent_step, new_indent);
dump(i->second, true, ensure_ascii, indent_step, new_indent, serialize_binary);

o->write_character('\n');
o->write_characters(indent_string.c_str(), current_indent);
Expand All @@ -14788,7 +14788,7 @@ class serializer
o->write_character('\"');
dump_escaped(i->first, ensure_ascii);
o->write_characters("\":", 2);
dump(i->second, false, ensure_ascii, indent_step, current_indent);
dump(i->second, false, ensure_ascii, indent_step, current_indent, serialize_binary);
o->write_character(',');
}

Expand All @@ -14798,7 +14798,7 @@ class serializer
o->write_character('\"');
dump_escaped(i->first, ensure_ascii);
o->write_characters("\":", 2);
dump(i->second, false, ensure_ascii, indent_step, current_indent);
dump(i->second, false, ensure_ascii, indent_step, current_indent, serialize_binary);

o->write_character('}');
}
Expand Down Expand Up @@ -14830,14 +14830,14 @@ class serializer
i != val.m_value.array->cend() - 1; ++i)
{
o->write_characters(indent_string.c_str(), new_indent);
dump(*i, true, ensure_ascii, indent_step, new_indent);
dump(*i, true, ensure_ascii, indent_step, new_indent, serialize_binary);
o->write_characters(",\n", 2);
}

// last element
assert(not val.m_value.array->empty());
o->write_characters(indent_string.c_str(), new_indent);
dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent, serialize_binary);

o->write_character('\n');
o->write_characters(indent_string.c_str(), current_indent);
Expand All @@ -14851,13 +14851,13 @@ class serializer
for (auto i = val.m_value.array->cbegin();
i != val.m_value.array->cend() - 1; ++i)
{
dump(*i, false, ensure_ascii, indent_step, current_indent);
dump(*i, false, ensure_ascii, indent_step, current_indent, serialize_binary);
o->write_character(',');
}

// last element
assert(not val.m_value.array->empty());
dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent, serialize_binary);

o->write_character(']');
}
Expand Down Expand Up @@ -17723,11 +17723,11 @@ class basic_json

if (indent >= 0)
{
s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent), serialize_binary);
s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent), 0, serialize_binary);
}
else
{
s.dump(*this, false, ensure_ascii, 0, serialize_binary);
s.dump(*this, false, ensure_ascii, 0, 0, serialize_binary);
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion test/src/unit-constructor1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ TEST_CASE("constructors")

// check that NaN is serialized to null
CHECK(j.dump() == "null");
}
}

SECTION("infinity")
{
Expand Down
14 changes: 14 additions & 0 deletions test/src/unit-regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1904,6 +1904,20 @@ TEST_CASE("regression tests")
const auto result = json::diff(source, target);
CHECK(result.dump() == R"([{"op":"add","path":"/foo/-","value":"3"}])");
}

SECTION("issue #2067 - cannot serialize binary data to text JSON")
{
const unsigned char data[] = {0x81, 0xA4, 0x64, 0x61, 0x74, 0x61, 0xC4, 0x0F, 0x33, 0x30, 0x30, 0x32, 0x33, 0x34, 0x30, 0x31, 0x30, 0x37, 0x30, 0x35, 0x30, 0x31, 0x30};
json j = json::from_msgpack(data, sizeof(data) / sizeof(data[0]));
CHECK_NOTHROW(
j.dump(4, // Indent
' ', // Indent char
false, // Ensure ascii
json::error_handler_t::strict, // Error
true // Allow binary data
)
);
}
}

#if not defined(JSON_NOEXCEPTION)
Expand Down

0 comments on commit 2e5727d

Please sign in to comment.