2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-06-24 08:38:59 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-08-09 06:34:26 -03:00
|
|
|
#include <AK/GenericLexer.h>
|
2020-09-07 08:30:40 -03:00
|
|
|
#include <AK/JsonValue.h>
|
2019-06-24 08:38:59 -03:00
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
2020-08-09 06:34:26 -03:00
|
|
|
class JsonParser : private GenericLexer {
|
2019-06-24 08:38:59 -03:00
|
|
|
public:
|
2025-03-19 18:47:25 -03:00
|
|
|
static ErrorOr<JsonValue> parse(StringView);
|
|
|
|
|
|
|
|
|
|
private:
|
2021-11-10 20:55:02 -03:00
|
|
|
explicit JsonParser(StringView input)
|
2020-08-09 06:34:26 -03:00
|
|
|
: GenericLexer(input)
|
2019-06-24 08:38:59 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-19 18:47:25 -03:00
|
|
|
ErrorOr<JsonValue> parse_json();
|
2021-11-14 21:46:51 -03:00
|
|
|
ErrorOr<JsonValue> parse_helper();
|
|
|
|
|
|
2026-04-28 15:32:57 -03:00
|
|
|
ErrorOr<String> consume_and_unescape_string();
|
2021-11-14 21:46:51 -03:00
|
|
|
ErrorOr<JsonValue> parse_array();
|
|
|
|
|
ErrorOr<JsonValue> parse_object();
|
|
|
|
|
ErrorOr<JsonValue> parse_number();
|
|
|
|
|
ErrorOr<JsonValue> parse_string();
|
|
|
|
|
ErrorOr<JsonValue> parse_false();
|
|
|
|
|
ErrorOr<JsonValue> parse_true();
|
|
|
|
|
ErrorOr<JsonValue> parse_null();
|
2026-03-01 06:19:07 -03:00
|
|
|
|
|
|
|
|
// Keep recursive parsing depth bounded so untrusted JSON cannot overflow the call stack.
|
|
|
|
|
static constexpr size_t max_nesting_depth { 512 };
|
|
|
|
|
size_t m_current_nesting_depth { 0 };
|
2019-06-24 08:38:59 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-26 08:18:30 -03:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-06-24 08:38:59 -03:00
|
|
|
using AK::JsonParser;
|
2022-11-26 08:18:30 -03:00
|
|
|
#endif
|