diff --git a/src/coders/json.cpp b/src/coders/json.cpp index 56356a40..19110f90 100644 --- a/src/coders/json.cpp +++ b/src/coders/json.cpp @@ -41,7 +41,8 @@ void stringifyObj( std::stringstream& ss, int indent, const std::string& indentstr, - bool nice + bool nice, + bool escapeUtf8 ); void stringifyList( @@ -49,7 +50,8 @@ void stringifyList( std::stringstream& ss, int indent, const std::string& indentstr, - bool nice + bool nice, + bool escapeUtf8 ); void stringifyValue( @@ -57,16 +59,17 @@ void stringifyValue( std::stringstream& ss, int indent, const std::string& indentstr, - bool nice + bool nice, + bool escapeUtf8 ) { using dv::value_type; switch (value.getType()) { case value_type::object: - stringifyObj(value, ss, indent, indentstr, nice); + stringifyObj(value, ss, indent, indentstr, nice, escapeUtf8); break; case value_type::list: - stringifyList(value, ss, indent, indentstr, nice); + stringifyList(value, ss, indent, indentstr, nice, escapeUtf8); break; case value_type::bytes: { const auto& bytes = value.asBytes(); @@ -75,7 +78,7 @@ void stringifyValue( break; } case value_type::string: - ss << util::escape(value.asString(), !nice); + ss << util::escape(value.asString(), escapeUtf8); break; case value_type::number: ss << std::setprecision(15) << value.asNumber(); @@ -97,7 +100,8 @@ void stringifyList( std::stringstream& ss, int indent, const std::string& indentstr, - bool nice + bool nice, + bool escapeUtf8 ) { if (list.empty()) { ss << "[]"; @@ -109,7 +113,7 @@ void stringifyList( newline(ss, nice, indent, indentstr); } const auto& value = list[i]; - stringifyValue(value, ss, indent + 1, indentstr, nice); + stringifyValue(value, ss, indent + 1, indentstr, nice, escapeUtf8); if (i + 1 < list.size()) { ss << ','; } @@ -125,7 +129,8 @@ void stringifyObj( std::stringstream& ss, int indent, const std::string& indentstr, - bool nice + bool nice, + bool escapeUtf8 ) { if (obj.empty()) { ss << "{}"; @@ -138,7 +143,7 @@ void stringifyObj( newline(ss, nice, indent, indentstr); } ss << util::escape(key) << ": "; - stringifyValue(value, ss, indent + 1, indentstr, nice); + stringifyValue(value, ss, indent + 1, indentstr, nice, escapeUtf8); index++; if (index < obj.size()) { ss << ','; @@ -151,10 +156,13 @@ void stringifyObj( } std::string json::stringify( - const dv::value& value, bool nice, const std::string& indent + const dv::value& value, + bool nice, + const std::string& indent, + bool escapeUtf8 ) { std::stringstream ss; - stringifyValue(value, ss, 1, indent, nice); + stringifyValue(value, ss, 1, indent, nice, escapeUtf8); return ss.str(); } diff --git a/src/coders/json.hpp b/src/coders/json.hpp index f79b5d46..5c4c3ec7 100644 --- a/src/coders/json.hpp +++ b/src/coders/json.hpp @@ -11,6 +11,9 @@ namespace json { dv::value parse(std::string_view source); std::string stringify( - const dv::value& value, bool nice, const std::string& indent=" " + const dv::value& value, + bool nice, + const std::string& indent = " ", + bool escapeUtf8 = false ); } diff --git a/src/logic/EngineController.cpp b/src/logic/EngineController.cpp index 72ce6e90..7a933bd9 100644 --- a/src/logic/EngineController.cpp +++ b/src/logic/EngineController.cpp @@ -178,7 +178,7 @@ void EngineController::onMissingContent(const std::shared_ptr& re if (engine.isHeadless()) { throw std::runtime_error( "missing content: " + - json::stringify(create_missing_content_report(report), true) + json::stringify(create_missing_content_report(report), true, " ") ); } else { engine.setScreen(std::make_shared(engine)); diff --git a/src/logic/scripting/lua/libs/libjson.cpp b/src/logic/scripting/lua/libs/libjson.cpp index 693b83be..503173bd 100644 --- a/src/logic/scripting/lua/libs/libjson.cpp +++ b/src/logic/scripting/lua/libs/libjson.cpp @@ -5,7 +5,8 @@ static int l_json_stringify(lua::State* L) { auto value = lua::tovalue(L, 1); bool nice = lua::toboolean(L, 2); - auto string = json::stringify(value, nice, " "); + bool escapeUTF = lua::toboolean(L, 3); + auto string = json::stringify(value, nice, " ", escapeUTF); return lua::pushstring(L, string); } diff --git a/src/logic/scripting/lua/libs/libnetwork.cpp b/src/logic/scripting/lua/libs/libnetwork.cpp index 678d46f2..dce54833 100644 --- a/src/logic/scripting/lua/libs/libnetwork.cpp +++ b/src/logic/scripting/lua/libs/libnetwork.cpp @@ -44,7 +44,12 @@ static int l_post(lua::State* L) { lua::pushvalue(L, 3); auto onResponse = lua::create_lambda_nothrow(L); - auto string = json::stringify(data, false); + std::string string; + if (data.isString()) { + string = data.asString(); + } else { + string = json::stringify(data, false); + } engine->getNetwork().post(url, string, [onResponse](std::vector bytes) { auto buffer = std::make_shared>( reinterpret_cast(bytes.data()), bytes.size()