Merge pull request #2071 from nlohmann/issue2067

Properly pass serialize_binary to dump function
This commit is contained in:
Niels Lohmann 2020-04-29 19:39:58 +02:00 committed by GitHub
commit db013c9428
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 20 deletions

View file

@ -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);
}
@ -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);
@ -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(',');
}
@ -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('}');
}
@ -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);
@ -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(']');
}

View file

@ -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;

View file

@ -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);
}
@ -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);
@ -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(',');
}
@ -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('}');
}
@ -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);
@ -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(']');
}
@ -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;

View file

@ -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)