diff --git a/AK/JsonObject.h b/AK/JsonObject.h index a61ab44ed3..5b95005495 100644 --- a/AK/JsonObject.h +++ b/AK/JsonObject.h @@ -31,6 +31,7 @@ #include #include #include +#include namespace AK { @@ -136,9 +137,36 @@ template inline void JsonValue::serialize(Builder& builder) const { switch (m_type) { - case Type::String: - builder.appendf("\"%s\"", m_value.as_string->characters()); - break; + case Type::String: { + auto size = m_value.as_string->length(); + builder.append("\""); + for (size_t i = 0; i < size; i++) { + char ch = m_value.as_string->characters()[i]; + switch (ch) { + case '\e': + builder.append("\\u001B"); + break; + case '\b': + builder.append("\\b"); + break; + case '\n': + builder.append("\\n"); + break; + case '\t': + builder.append("\\t"); + break; + case '\"': + builder.append("\\\""); + break; + case '\\': + builder.append("\\\\"); + break; + default: + builder.appendf("%c", ch); + } + } + builder.append("\""); + } break; case Type::Array: m_value.as_array->serialize(builder); break; diff --git a/AK/JsonParser.cpp b/AK/JsonParser.cpp index 2beb1a060f..dcf23ece38 100644 --- a/AK/JsonParser.cpp +++ b/AK/JsonParser.cpp @@ -116,14 +116,22 @@ String JsonParser::consume_quoted_string() case 'f': buffer.append('\f'); break; - case 'u': - consume(); - consume(); - consume(); - consume(); - // FIXME: This is obviously not correct, but we don't have non-ASCII support so meh. - buffer.append('?'); - break; + case 'u': { + StringBuilder sb; + sb.append(consume()); + sb.append(consume()); + sb.append(consume()); + sb.append(consume()); + + bool ok; + u32 codepoint = AK::StringUtils::convert_to_uint_from_hex(sb.to_string(), ok); + if (ok && codepoint < 128) { + buffer.append((char)codepoint); + } else { + // FIXME: This is obviously not correct, but we don't have non-ASCII support so meh. + buffer.append('?'); + } + } break; default: buffer.append(escaped_ch); break;