diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index e0ca501f..f83f4a37 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -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(']'); } diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 45632dec..18bcbc21 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -2237,11 +2237,11 @@ class basic_json if (indent >= 0) { - s.dump(*this, true, ensure_ascii, static_cast(indent), serialize_binary); + s.dump(*this, true, ensure_ascii, static_cast(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; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index eeb51864..22b832d0 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -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(indent), serialize_binary); + s.dump(*this, true, ensure_ascii, static_cast(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; diff --git a/test/src/unit-constructor1.cpp b/test/src/unit-constructor1.cpp index dc3507bc..2caf2653 100644 --- a/test/src/unit-constructor1.cpp +++ b/test/src/unit-constructor1.cpp @@ -826,7 +826,7 @@ TEST_CASE("constructors") // check that NaN is serialized to null CHECK(j.dump() == "null"); - } + } SECTION("infinity") { diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index 7d72fc86..6d3d65d8 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -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)