summaryrefslogtreecommitdiff
path: root/AK/JsonParser.cpp
diff options
context:
space:
mode:
authorstelar7 <dudedbz@gmail.com>2021-07-01 12:32:37 +0200
committerAndreas Kling <kling@serenityos.org>2021-07-05 12:36:19 +0200
commitce314c54bdef56e0f210b42c362a3a7caed370c1 (patch)
tree83710e5bd54c2d413e3917da4b0ed45089949505 /AK/JsonParser.cpp
parent31a2f109277fb8e9baee248b280b99c4037a0fa3 (diff)
downloadserenity-ce314c54bdef56e0f210b42c362a3a7caed370c1.zip
JsonParser: Bring parser more to spec
Diffstat (limited to 'AK/JsonParser.cpp')
-rw-r--r--AK/JsonParser.cpp129
1 files changed, 94 insertions, 35 deletions
diff --git a/AK/JsonParser.cpp b/AK/JsonParser.cpp
index 80d1794c26..3beda694dd 100644
--- a/AK/JsonParser.cpp
+++ b/AK/JsonParser.cpp
@@ -4,13 +4,18 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/CharacterTypes.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/JsonParser.h>
-#include <ctype.h>
namespace AK {
+constexpr bool is_space(int ch)
+{
+ return ch == '\t' || ch == '\n' || ch == '\r' || ch == ' ';
+}
+
String JsonParser::consume_and_unescape_string()
{
if (!consume_specific('"'))
@@ -26,6 +31,8 @@ String JsonParser::consume_and_unescape_string()
ch = m_input[peek_index];
if (ch == '"' || ch == '\\')
break;
+ if (is_ascii_c0_control(ch))
+ return {};
++peek_index;
}
@@ -43,34 +50,69 @@ String JsonParser::consume_and_unescape_string()
continue;
}
ignore();
- char escaped_ch = consume();
- switch (escaped_ch) {
- case 'n':
+ if (next_is('"')) {
+ ignore();
+ final_sb.append('"');
+ continue;
+ }
+
+ if (next_is('\\')) {
+ ignore();
+ final_sb.append('\\');
+ continue;
+ }
+
+ if (next_is('/')) {
+ ignore();
+ final_sb.append('/');
+ continue;
+ }
+
+ if (next_is('n')) {
+ ignore();
final_sb.append('\n');
- break;
- case 'r':
+ continue;
+ }
+
+ if (next_is('r')) {
+ ignore();
final_sb.append('\r');
- break;
- case 't':
+ continue;
+ }
+
+ if (next_is('t')) {
+ ignore();
final_sb.append('\t');
- break;
- case 'b':
+ continue;
+ }
+
+ if (next_is('b')) {
+ ignore();
final_sb.append('\b');
- break;
- case 'f':
+ continue;
+ }
+
+ if (next_is('f')) {
+ ignore();
final_sb.append('\f');
- break;
- case 'u': {
+ continue;
+ }
+
+ if (next_is('u')) {
+ ignore();
+ if (tell_remaining() < 4)
+ return {};
+
auto code_point = AK::StringUtils::convert_to_uint_from_hex(consume(4));
- if (code_point.has_value())
+ if (code_point.has_value()) {
final_sb.append_code_point(code_point.value());
- else
- final_sb.append('?');
- } break;
- default:
- final_sb.append(escaped_ch);
- break;
+ continue;
+ } else {
+ return {};
+ }
}
+
+ return {};
}
if (!consume_specific('"'))
return {};
@@ -84,27 +126,27 @@ Optional<JsonValue> JsonParser::parse_object()
if (!consume_specific('{'))
return {};
for (;;) {
- ignore_while(isspace);
+ ignore_while(is_space);
if (peek() == '}')
break;
- ignore_while(isspace);
+ ignore_while(is_space);
auto name = consume_and_unescape_string();
if (name.is_null())
return {};
- ignore_while(isspace);
+ ignore_while(is_space);
if (!consume_specific(':'))
return {};
- ignore_while(isspace);
+ ignore_while(is_space);
auto value = parse_helper();
if (!value.has_value())
return {};
object.set(name, value.release_value());
- ignore_while(isspace);
+ ignore_while(is_space);
if (peek() == '}')
break;
if (!consume_specific(','))
return {};
- ignore_while(isspace);
+ ignore_while(is_space);
if (peek() == '}')
return {};
}
@@ -119,23 +161,23 @@ Optional<JsonValue> JsonParser::parse_array()
if (!consume_specific('['))
return {};
for (;;) {
- ignore_while(isspace);
+ ignore_while(is_space);
if (peek() == ']')
break;
auto element = parse_helper();
if (!element.has_value())
return {};
array.append(element.release_value());
- ignore_while(isspace);
+ ignore_while(is_space);
if (peek() == ']')
break;
if (!consume_specific(','))
return {};
- ignore_while(isspace);
+ ignore_while(is_space);
if (peek() == ']')
return {};
}
- ignore_while(isspace);
+ ignore_while(is_space);
if (!consume_specific(']'))
return {};
return JsonValue { move(array) };
@@ -159,15 +201,32 @@ Optional<JsonValue> JsonParser::parse_number()
for (;;) {
char ch = peek();
if (ch == '.') {
+ if (is_double)
+ return {};
+
is_double = true;
++m_index;
continue;
}
if (ch == '-' || (ch >= '0' && ch <= '9')) {
- if (is_double)
+ if (is_double) {
+ if (ch == '-')
+ return {};
+
fraction_buffer.append(ch);
- else
+ } else {
+ if (number_buffer.size() > 0) {
+ if (number_buffer.at(0) == '0')
+ return {};
+ }
+
+ if (number_buffer.size() > 1) {
+ if (number_buffer.at(0) == '-' && number_buffer.at(1) == '0')
+ return {};
+ }
+
number_buffer.append(ch);
+ }
++m_index;
continue;
}
@@ -247,7 +306,7 @@ Optional<JsonValue> JsonParser::parse_null()
Optional<JsonValue> JsonParser::parse_helper()
{
- ignore_while(isspace);
+ ignore_while(is_space);
auto type_hint = peek();
switch (type_hint) {
case '{':
@@ -284,7 +343,7 @@ Optional<JsonValue> JsonParser::parse()
auto result = parse_helper();
if (!result.has_value())
return {};
- ignore_while(isspace);
+ ignore_while(is_space);
if (!is_eof())
return {};
return result;