diff --git a/src/json.hpp b/src/json.hpp index 5190c64e9..879d8be57 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -3978,7 +3978,7 @@ class basic_json { // control characters (everything between 0x00 and 0x1f) // -> create four-digit hex representation - o << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(c); + o << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(c) << std::dec; } else { diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 3650c8e73..5a9d4ab33 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -3978,7 +3978,7 @@ class basic_json { // control characters (everything between 0x00 and 0x1f) // -> create four-digit hex representation - o << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(c); + o << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(c) << std::dec; } else { diff --git a/test/unit.cpp b/test/unit.cpp index 64e67752d..935c665fe 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -9276,4 +9276,16 @@ TEST_CASE("regression tests") // we need to cast to int to compile with Catch - the value is int32_t CHECK(static_cast(j["int_1"]) == 1); } + + SECTION("issue #101 - binary string causes numbers to be dumped as hex") + { + int64_t number = 10; + std::string bytes{"\x00" "asdf\n", 6}; + json j; + j["int64"] = number; + j["binary string"] = bytes; + // make sure the number is really printed as decimal "10" and not as + // hexadecimal "a" + CHECK(j.dump() == "{\"binary string\":\"\\u0000asdf\\n\",\"int64\":10}"); + } }