From c54855682c3f51e7eacf1217789ca02ae3df78ff Mon Sep 17 00:00:00 2001 From: Emanuel Sprung Date: Tue, 31 Mar 2020 13:30:09 +0200 Subject: [PATCH] AK: A few JSON improvements * Add double number to object serializer * Handle negative double numbers correctly * Handle \r and \n in quoted strings independently This improves the situation when keys contain \r or \n that currently has the effect that "a\rkey" and "a\nkey" in an JSON object are the same key value. --- AK/JsonObjectSerializer.h | 6 ++++++ AK/JsonParser.cpp | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/AK/JsonObjectSerializer.h b/AK/JsonObjectSerializer.h index 0c6b1eda97..8de17cead6 100644 --- a/AK/JsonObjectSerializer.h +++ b/AK/JsonObjectSerializer.h @@ -109,6 +109,12 @@ public: m_builder.appendf("%llu", value); } + void add(const StringView& key, double value) + { + begin_item(key); + m_builder.appendf("%f", value); + } + JsonArraySerializer add_array(const StringView& key) { begin_item(key); diff --git a/AK/JsonParser.cpp b/AK/JsonParser.cpp index 0f2fccfa64..1007c46779 100644 --- a/AK/JsonParser.cpp +++ b/AK/JsonParser.cpp @@ -100,9 +100,11 @@ String JsonParser::consume_quoted_string() char escaped_ch = consume(); switch (escaped_ch) { case 'n': - case 'r': buffer.append('\n'); break; + case 'r': + buffer.append('\r'); + break; case 't': buffer.append('\t'); break; @@ -225,6 +227,7 @@ JsonValue JsonParser::parse_number() ASSERT(ok); int fraction = fraction_string.to_uint(ok); + fraction *= (whole < 0) ? -1 : 1; ASSERT(ok); auto divider = 1;