2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-07-01 10:32:37 +00:00
|
|
|
#include <AK/CharacterTypes.h>
|
2022-10-12 00:22:45 +00:00
|
|
|
#include <AK/FloatingPointStringConversions.h>
|
2019-06-24 11:38:59 +00:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonParser.h>
|
2022-02-11 08:29:08 +00:00
|
|
|
#include <math.h>
|
2019-06-24 11:38:59 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2021-07-01 10:32:37 +00:00
|
|
|
constexpr bool is_space(int ch)
|
|
|
|
{
|
|
|
|
return ch == '\t' || ch == '\n' || ch == '\r' || ch == ' ';
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
ErrorOr<DeprecatedString> JsonParser::consume_and_unescape_string()
|
2019-06-24 11:38:59 +00:00
|
|
|
{
|
2020-06-11 04:40:27 +00:00
|
|
|
if (!consume_specific('"'))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected '\"'");
|
2020-06-19 21:46:25 +00:00
|
|
|
StringBuilder final_sb;
|
2019-08-07 09:57:51 +00:00
|
|
|
|
2019-06-25 14:56:33 +00:00
|
|
|
for (;;) {
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t peek_index = m_index;
|
2019-08-07 09:57:51 +00:00
|
|
|
char ch = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (peek_index == m_input.length())
|
|
|
|
break;
|
|
|
|
ch = m_input[peek_index];
|
|
|
|
if (ch == '"' || ch == '\\')
|
|
|
|
break;
|
2021-07-01 10:32:37 +00:00
|
|
|
if (is_ascii_c0_control(ch))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Error while parsing string");
|
2019-08-07 09:57:51 +00:00
|
|
|
++peek_index;
|
|
|
|
}
|
|
|
|
|
2020-08-09 09:34:26 +00:00
|
|
|
while (peek_index != m_index) {
|
|
|
|
final_sb.append(m_input[m_index]);
|
|
|
|
m_index++;
|
2019-08-07 09:57:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 07:31:48 +00:00
|
|
|
if (m_index == m_input.length())
|
|
|
|
break;
|
2019-06-25 14:56:33 +00:00
|
|
|
if (ch == '"')
|
|
|
|
break;
|
|
|
|
if (ch != '\\') {
|
2020-06-19 21:46:25 +00:00
|
|
|
final_sb.append(consume());
|
2019-06-25 14:56:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-09 09:34:26 +00:00
|
|
|
ignore();
|
2021-07-01 10:32:37 +00:00
|
|
|
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();
|
2020-06-19 21:46:25 +00:00
|
|
|
final_sb.append('\n');
|
2021-07-01 10:32:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next_is('r')) {
|
|
|
|
ignore();
|
2020-06-19 21:46:25 +00:00
|
|
|
final_sb.append('\r');
|
2021-07-01 10:32:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next_is('t')) {
|
|
|
|
ignore();
|
2020-06-19 21:46:25 +00:00
|
|
|
final_sb.append('\t');
|
2021-07-01 10:32:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next_is('b')) {
|
|
|
|
ignore();
|
2020-06-19 21:46:25 +00:00
|
|
|
final_sb.append('\b');
|
2021-07-01 10:32:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next_is('f')) {
|
|
|
|
ignore();
|
2020-06-19 21:46:25 +00:00
|
|
|
final_sb.append('\f');
|
2021-07-01 10:32:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next_is('u')) {
|
|
|
|
ignore();
|
|
|
|
if (tell_remaining() < 4)
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: EOF while parsing Unicode escape");
|
2021-07-01 10:32:37 +00:00
|
|
|
|
2020-08-09 09:34:26 +00:00
|
|
|
auto code_point = AK::StringUtils::convert_to_uint_from_hex(consume(4));
|
2021-07-01 10:32:37 +00:00
|
|
|
if (code_point.has_value()) {
|
2020-08-05 20:31:20 +00:00
|
|
|
final_sb.append_code_point(code_point.value());
|
2021-07-01 10:32:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Error while parsing Unicode escape");
|
2019-06-25 14:56:33 +00:00
|
|
|
}
|
2021-07-01 10:32:37 +00:00
|
|
|
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Error while parsing string");
|
2019-06-25 14:56:33 +00:00
|
|
|
}
|
2020-06-11 04:40:27 +00:00
|
|
|
if (!consume_specific('"'))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected '\"'");
|
2019-08-04 16:22:41 +00:00
|
|
|
|
2022-12-06 01:12:49 +00:00
|
|
|
return final_sb.to_deprecated_string();
|
2019-06-24 11:38:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 00:46:51 +00:00
|
|
|
ErrorOr<JsonValue> JsonParser::parse_object()
|
2019-06-24 11:38:59 +00:00
|
|
|
{
|
|
|
|
JsonObject object;
|
2020-06-11 04:40:27 +00:00
|
|
|
if (!consume_specific('{'))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected '{'");
|
2019-06-24 11:38:59 +00:00
|
|
|
for (;;) {
|
2021-07-01 10:32:37 +00:00
|
|
|
ignore_while(is_space);
|
2019-06-24 11:38:59 +00:00
|
|
|
if (peek() == '}')
|
|
|
|
break;
|
2021-07-01 10:32:37 +00:00
|
|
|
ignore_while(is_space);
|
2021-11-15 00:46:51 +00:00
|
|
|
auto name = TRY(consume_and_unescape_string());
|
2020-06-11 04:40:27 +00:00
|
|
|
if (name.is_null())
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected object property name");
|
2021-07-01 10:32:37 +00:00
|
|
|
ignore_while(is_space);
|
2020-06-11 04:40:27 +00:00
|
|
|
if (!consume_specific(':'))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected ':'");
|
2021-07-01 10:32:37 +00:00
|
|
|
ignore_while(is_space);
|
2021-11-15 00:46:51 +00:00
|
|
|
auto value = TRY(parse_helper());
|
|
|
|
object.set(name, move(value));
|
2021-07-01 10:32:37 +00:00
|
|
|
ignore_while(is_space);
|
2019-06-24 11:38:59 +00:00
|
|
|
if (peek() == '}')
|
|
|
|
break;
|
2020-06-11 04:40:27 +00:00
|
|
|
if (!consume_specific(','))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected ','");
|
2021-07-01 10:32:37 +00:00
|
|
|
ignore_while(is_space);
|
2020-06-11 04:40:27 +00:00
|
|
|
if (peek() == '}')
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Unexpected '}'");
|
2019-06-24 11:38:59 +00:00
|
|
|
}
|
2020-06-11 04:40:27 +00:00
|
|
|
if (!consume_specific('}'))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected '}'");
|
2021-05-14 09:54:43 +00:00
|
|
|
return JsonValue { move(object) };
|
2019-06-24 11:38:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 00:46:51 +00:00
|
|
|
ErrorOr<JsonValue> JsonParser::parse_array()
|
2019-06-24 11:38:59 +00:00
|
|
|
{
|
|
|
|
JsonArray array;
|
2020-06-11 04:40:27 +00:00
|
|
|
if (!consume_specific('['))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected '['");
|
2019-06-24 11:38:59 +00:00
|
|
|
for (;;) {
|
2021-07-01 10:32:37 +00:00
|
|
|
ignore_while(is_space);
|
2019-06-24 11:38:59 +00:00
|
|
|
if (peek() == ']')
|
|
|
|
break;
|
2021-11-15 00:46:51 +00:00
|
|
|
auto element = TRY(parse_helper());
|
2023-04-17 05:51:17 +00:00
|
|
|
TRY(array.append(move(element)));
|
2021-07-01 10:32:37 +00:00
|
|
|
ignore_while(is_space);
|
2019-06-24 11:38:59 +00:00
|
|
|
if (peek() == ']')
|
|
|
|
break;
|
2020-06-11 04:40:27 +00:00
|
|
|
if (!consume_specific(','))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected ','");
|
2021-07-01 10:32:37 +00:00
|
|
|
ignore_while(is_space);
|
2020-06-11 04:40:27 +00:00
|
|
|
if (peek() == ']')
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Unexpected ']'");
|
2019-06-24 11:38:59 +00:00
|
|
|
}
|
2021-07-01 10:32:37 +00:00
|
|
|
ignore_while(is_space);
|
2020-06-11 04:40:27 +00:00
|
|
|
if (!consume_specific(']'))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected ']'");
|
2021-05-14 09:54:43 +00:00
|
|
|
return JsonValue { move(array) };
|
2019-06-24 11:38:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 00:46:51 +00:00
|
|
|
ErrorOr<JsonValue> JsonParser::parse_string()
|
2019-06-24 11:38:59 +00:00
|
|
|
{
|
2021-11-15 00:46:51 +00:00
|
|
|
auto string = TRY(consume_and_unescape_string());
|
|
|
|
return JsonValue(move(string));
|
2019-06-24 11:38:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 00:46:51 +00:00
|
|
|
ErrorOr<JsonValue> JsonParser::parse_number()
|
2019-06-24 11:38:59 +00:00
|
|
|
{
|
2022-10-12 00:22:45 +00:00
|
|
|
Vector<char, 32> number_buffer;
|
|
|
|
|
|
|
|
auto start_index = tell();
|
|
|
|
|
|
|
|
bool negative = false;
|
|
|
|
if (peek() == '-') {
|
|
|
|
number_buffer.append('-');
|
|
|
|
++m_index;
|
|
|
|
negative = true;
|
|
|
|
|
|
|
|
if (!is_ascii_digit(peek()))
|
|
|
|
return Error::from_string_literal("JsonParser: Unexpected '-' without further digits");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto fallback_to_double_parse = [&]() -> ErrorOr<JsonValue> {
|
|
|
|
#ifdef KERNEL
|
|
|
|
# error JSONParser is currently not available for the Kernel because it disallows floating point. \
|
|
|
|
If you want to make this KERNEL compatible you can just make this fallback_to_double \
|
|
|
|
function fail with an error in KERNEL mode.
|
|
|
|
#endif
|
|
|
|
// FIXME: Since we know all the characters so far are ascii digits (and one . or e) we could
|
|
|
|
// use that in the floating point parser.
|
|
|
|
|
|
|
|
// The first part should be just ascii digits
|
|
|
|
StringView view = m_input.substring_view(start_index);
|
|
|
|
|
|
|
|
char const* start = view.characters_without_null_termination();
|
|
|
|
auto parse_result = parse_first_floating_point(start, start + view.length());
|
|
|
|
|
|
|
|
if (parse_result.parsed_value()) {
|
|
|
|
auto characters_parsed = parse_result.end_ptr - start;
|
|
|
|
m_index = start_index + characters_parsed;
|
|
|
|
|
|
|
|
return JsonValue(parse_result.value);
|
|
|
|
}
|
|
|
|
return Error::from_string_literal("JsonParser: Invalid floating point");
|
|
|
|
};
|
|
|
|
|
|
|
|
if (peek() == '0') {
|
|
|
|
if (is_ascii_digit(peek(1)))
|
|
|
|
return Error::from_string_literal("JsonParser: Cannot have leading zeros");
|
|
|
|
|
|
|
|
// Leading zeros are not allowed, however we can have a '.' or 'e' with
|
|
|
|
// valid digits after just a zero. These cases will be detected by having the next element
|
|
|
|
// start with a '.' or 'e'.
|
|
|
|
}
|
2020-03-24 20:59:22 +00:00
|
|
|
|
2022-01-19 16:40:18 +00:00
|
|
|
bool all_zero = true;
|
2019-08-04 18:23:03 +00:00
|
|
|
for (;;) {
|
|
|
|
char ch = peek();
|
2020-03-24 20:59:22 +00:00
|
|
|
if (ch == '.') {
|
2022-10-12 00:22:45 +00:00
|
|
|
if (!is_ascii_digit(peek(1)))
|
|
|
|
return Error::from_string_literal("JsonParser: Must have digits after decimal point");
|
2021-07-01 10:32:37 +00:00
|
|
|
|
2022-10-12 00:22:45 +00:00
|
|
|
return fallback_to_double_parse();
|
2020-03-24 20:59:22 +00:00
|
|
|
}
|
2022-10-12 00:22:45 +00:00
|
|
|
if (ch == 'e' || ch == 'E') {
|
|
|
|
char next = peek(1);
|
|
|
|
if (!is_ascii_digit(next) && ((next != '+' && next != '-') || !is_ascii_digit(peek(2))))
|
|
|
|
return Error::from_string_literal("JsonParser: Must have digits after exponent with an optional sign inbetween");
|
2021-07-01 10:32:37 +00:00
|
|
|
|
2022-10-12 00:22:45 +00:00
|
|
|
return fallback_to_double_parse();
|
|
|
|
}
|
2021-07-01 10:32:37 +00:00
|
|
|
|
2022-10-12 00:22:45 +00:00
|
|
|
if (is_ascii_digit(ch)) {
|
|
|
|
if (ch != '0')
|
|
|
|
all_zero = false;
|
2021-07-01 10:32:37 +00:00
|
|
|
|
2022-10-12 00:22:45 +00:00
|
|
|
number_buffer.append(ch);
|
2019-08-04 18:23:03 +00:00
|
|
|
++m_index;
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-12 00:22:45 +00:00
|
|
|
|
2019-08-04 18:23:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-12 00:22:45 +00:00
|
|
|
// Negative zero is always a double
|
|
|
|
if (negative && all_zero)
|
|
|
|
return JsonValue(-0.0);
|
2022-08-29 23:59:32 +00:00
|
|
|
|
2019-08-04 09:47:21 +00:00
|
|
|
StringView number_string(number_buffer.data(), number_buffer.size());
|
2020-03-24 20:59:22 +00:00
|
|
|
|
2022-10-12 00:22:45 +00:00
|
|
|
auto to_unsigned_result = number_string.to_uint<u64>();
|
|
|
|
if (to_unsigned_result.has_value()) {
|
|
|
|
if (*to_unsigned_result <= NumericLimits<u32>::max())
|
|
|
|
return JsonValue((u32)*to_unsigned_result);
|
2022-01-19 16:40:18 +00:00
|
|
|
|
2022-10-12 00:22:45 +00:00
|
|
|
return JsonValue(*to_unsigned_result);
|
|
|
|
} else if (auto signed_number = number_string.to_int<i64>(); signed_number.has_value()) {
|
2022-08-29 23:59:32 +00:00
|
|
|
|
2022-10-12 00:22:45 +00:00
|
|
|
if (*signed_number <= NumericLimits<i32>::max())
|
|
|
|
return JsonValue((i32)*signed_number);
|
2020-03-24 20:59:22 +00:00
|
|
|
|
2022-10-12 00:22:45 +00:00
|
|
|
return JsonValue(*signed_number);
|
2020-03-24 20:59:22 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 00:22:45 +00:00
|
|
|
// It's possible the unsigned value is bigger than u64 max
|
|
|
|
return fallback_to_double_parse();
|
2019-06-24 11:38:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 00:46:51 +00:00
|
|
|
ErrorOr<JsonValue> JsonParser::parse_true()
|
2019-06-24 11:38:59 +00:00
|
|
|
{
|
2020-08-09 09:34:26 +00:00
|
|
|
if (!consume_specific("true"))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected 'true'");
|
2019-06-24 11:38:59 +00:00
|
|
|
return JsonValue(true);
|
|
|
|
}
|
|
|
|
|
2021-11-15 00:46:51 +00:00
|
|
|
ErrorOr<JsonValue> JsonParser::parse_false()
|
2019-06-24 11:38:59 +00:00
|
|
|
{
|
2020-08-09 09:34:26 +00:00
|
|
|
if (!consume_specific("false"))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected 'false'");
|
2019-06-24 11:38:59 +00:00
|
|
|
return JsonValue(false);
|
|
|
|
}
|
|
|
|
|
2021-11-15 00:46:51 +00:00
|
|
|
ErrorOr<JsonValue> JsonParser::parse_null()
|
2019-06-24 11:38:59 +00:00
|
|
|
{
|
2020-08-09 09:34:26 +00:00
|
|
|
if (!consume_specific("null"))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Expected 'null'");
|
2019-06-24 11:38:59 +00:00
|
|
|
return JsonValue(JsonValue::Type::Null);
|
|
|
|
}
|
|
|
|
|
2021-11-15 00:46:51 +00:00
|
|
|
ErrorOr<JsonValue> JsonParser::parse_helper()
|
2019-06-24 11:38:59 +00:00
|
|
|
{
|
2021-07-01 10:32:37 +00:00
|
|
|
ignore_while(is_space);
|
2019-06-24 11:38:59 +00:00
|
|
|
auto type_hint = peek();
|
|
|
|
switch (type_hint) {
|
|
|
|
case '{':
|
|
|
|
return parse_object();
|
|
|
|
case '[':
|
|
|
|
return parse_array();
|
|
|
|
case '"':
|
|
|
|
return parse_string();
|
|
|
|
case '-':
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
return parse_number();
|
|
|
|
case 'f':
|
|
|
|
return parse_false();
|
|
|
|
case 't':
|
|
|
|
return parse_true();
|
|
|
|
case 'n':
|
|
|
|
return parse_null();
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Unexpected character");
|
2020-06-11 04:40:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 00:46:51 +00:00
|
|
|
ErrorOr<JsonValue> JsonParser::parse()
|
2020-08-09 09:34:26 +00:00
|
|
|
{
|
2021-11-15 00:46:51 +00:00
|
|
|
auto result = TRY(parse_helper());
|
2021-07-01 10:32:37 +00:00
|
|
|
ignore_while(is_space);
|
2020-08-09 09:34:26 +00:00
|
|
|
if (!is_eof())
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("JsonParser: Didn't consume all input");
|
2020-06-11 04:40:27 +00:00
|
|
|
return result;
|
2019-06-24 11:38:59 +00:00
|
|
|
}
|
2020-06-11 04:40:27 +00:00
|
|
|
|
2019-06-24 11:38:59 +00:00
|
|
|
}
|