diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-15 01:46:51 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-17 00:21:10 +0100 |
commit | 587f9af960daa9f003ec9e41751cdc4ce50b87dd (patch) | |
tree | 41bc5b51741dffcc23e3e4b63d4c17a37e2e03d7 /AK/JsonParser.cpp | |
parent | 304c03f457e294a3d756860325d93b809f50f7a6 (diff) | |
download | serenity-587f9af960daa9f003ec9e41751cdc4ce50b87dd.zip |
AK: Make JSON parser return ErrorOr<JsonValue> (instead of Optional)
Also add slightly richer parse errors now that we can include a string
literal with returned errors.
This will allow us to use TRY() when working with JSON data.
Diffstat (limited to 'AK/JsonParser.cpp')
-rw-r--r-- | AK/JsonParser.cpp | 101 |
1 files changed, 46 insertions, 55 deletions
diff --git a/AK/JsonParser.cpp b/AK/JsonParser.cpp index fafb6736a6..ce2caf1f27 100644 --- a/AK/JsonParser.cpp +++ b/AK/JsonParser.cpp @@ -16,10 +16,10 @@ constexpr bool is_space(int ch) return ch == '\t' || ch == '\n' || ch == '\r' || ch == ' '; } -String JsonParser::consume_and_unescape_string() +ErrorOr<String> JsonParser::consume_and_unescape_string() { if (!consume_specific('"')) - return {}; + return Error::from_string_literal("JsonParser: Expected '\"'"sv); StringBuilder final_sb; for (;;) { @@ -32,7 +32,7 @@ String JsonParser::consume_and_unescape_string() if (ch == '"' || ch == '\\') break; if (is_ascii_c0_control(ch)) - return {}; + return Error::from_string_literal("JsonParser: Error while parsing string"sv); ++peek_index; } @@ -101,97 +101,90 @@ String JsonParser::consume_and_unescape_string() if (next_is('u')) { ignore(); if (tell_remaining() < 4) - return {}; + return Error::from_string_literal("JsonParser: EOF while parsing Unicode escape"sv); auto code_point = AK::StringUtils::convert_to_uint_from_hex(consume(4)); if (code_point.has_value()) { final_sb.append_code_point(code_point.value()); continue; - } else { - return {}; } + return Error::from_string_literal("JsonParser: Error while parsing Unicode escape"sv); } - return {}; + return Error::from_string_literal("JsonParser: Error while parsing string"sv); } if (!consume_specific('"')) - return {}; + return Error::from_string_literal("JsonParser: Expected '\"'"sv); return final_sb.to_string(); } -Optional<JsonValue> JsonParser::parse_object() +ErrorOr<JsonValue> JsonParser::parse_object() { JsonObject object; if (!consume_specific('{')) - return {}; + return Error::from_string_literal("JsonParser: Expected '{'"sv); for (;;) { ignore_while(is_space); if (peek() == '}') break; ignore_while(is_space); - auto name = consume_and_unescape_string(); + auto name = TRY(consume_and_unescape_string()); if (name.is_null()) - return {}; + return Error::from_string_literal("JsonParser: Expected object property name"sv); ignore_while(is_space); if (!consume_specific(':')) - return {}; + return Error::from_string_literal("JsonParser: Expected ':'"sv); ignore_while(is_space); - auto value = parse_helper(); - if (!value.has_value()) - return {}; - object.set(name, value.release_value()); + auto value = TRY(parse_helper()); + object.set(name, move(value)); ignore_while(is_space); if (peek() == '}') break; if (!consume_specific(',')) - return {}; + return Error::from_string_literal("JsonParser: Expected ','"sv); ignore_while(is_space); if (peek() == '}') - return {}; + return Error::from_string_literal("JsonParser: Unexpected '}'"sv); } if (!consume_specific('}')) - return {}; + return Error::from_string_literal("JsonParser: Expected '}'"sv); return JsonValue { move(object) }; } -Optional<JsonValue> JsonParser::parse_array() +ErrorOr<JsonValue> JsonParser::parse_array() { JsonArray array; if (!consume_specific('[')) - return {}; + return Error::from_string_literal("JsonParser: Expected '['"sv); for (;;) { ignore_while(is_space); if (peek() == ']') break; - auto element = parse_helper(); - if (!element.has_value()) - return {}; - array.append(element.release_value()); + auto element = TRY(parse_helper()); + array.append(move(element)); ignore_while(is_space); if (peek() == ']') break; if (!consume_specific(',')) - return {}; + return Error::from_string_literal("JsonParser: Expected ','"sv); ignore_while(is_space); if (peek() == ']') - return {}; + return Error::from_string_literal("JsonParser: Unexpected ']'"sv); } ignore_while(is_space); if (!consume_specific(']')) - return {}; + return Error::from_string_literal("JsonParser: Expected ']'"sv); return JsonValue { move(array) }; } -Optional<JsonValue> JsonParser::parse_string() +ErrorOr<JsonValue> JsonParser::parse_string() { - auto result = consume_and_unescape_string(); - if (result.is_null()) - return {}; - return JsonValue(result); + auto string = TRY(consume_and_unescape_string()); + return JsonValue(move(string)); } -Optional<JsonValue> JsonParser::parse_number() +ErrorOr<JsonValue> JsonParser::parse_number() { JsonValue value; Vector<char, 128> number_buffer; @@ -202,7 +195,7 @@ Optional<JsonValue> JsonParser::parse_number() char ch = peek(); if (ch == '.') { if (is_double) - return {}; + return Error::from_string_literal("JsonParser: Multiple '.' in number"sv); is_double = true; ++m_index; @@ -211,18 +204,18 @@ Optional<JsonValue> JsonParser::parse_number() if (ch == '-' || (ch >= '0' && ch <= '9')) { if (is_double) { if (ch == '-') - return {}; + return Error::from_string_literal("JsonParser: Error while parsing number"sv); fraction_buffer.append(ch); } else { if (number_buffer.size() > 0) { if (number_buffer.at(0) == '0') - return {}; + return Error::from_string_literal("JsonParser: Error while parsing number"sv); } if (number_buffer.size() > 1) { if (number_buffer.at(0) == '-' && number_buffer.at(1) == '0') - return {}; + return Error::from_string_literal("JsonParser: Error while parsing number"sv); } number_buffer.append(ch); @@ -245,14 +238,14 @@ Optional<JsonValue> JsonParser::parse_number() } else { auto number = number_string.to_int(); if (!number.has_value()) - return {}; + return Error::from_string_literal("JsonParser: Error while parsing number"sv); whole = number.value(); } StringView fraction_string(fraction_buffer.data(), fraction_buffer.size()); auto fraction_string_uint = fraction_string.to_uint(); if (!fraction_string_uint.has_value()) - return {}; + return Error::from_string_literal("JsonParser: Error while parsing number"sv); int fraction = fraction_string_uint.value(); fraction *= (whole < 0) ? -1 : 1; @@ -273,7 +266,7 @@ Optional<JsonValue> JsonParser::parse_number() } else { auto number = number_string.to_int<i64>(); if (!number.has_value()) - return {}; + return Error::from_string_literal("JsonParser: Error while parsing number"sv); if (number.value() <= NumericLimits<i32>::max()) { value = JsonValue((i32)number.value()); } else { @@ -287,28 +280,28 @@ Optional<JsonValue> JsonParser::parse_number() return value; } -Optional<JsonValue> JsonParser::parse_true() +ErrorOr<JsonValue> JsonParser::parse_true() { if (!consume_specific("true")) - return {}; + return Error::from_string_literal("JsonParser: Expected 'true'"sv); return JsonValue(true); } -Optional<JsonValue> JsonParser::parse_false() +ErrorOr<JsonValue> JsonParser::parse_false() { if (!consume_specific("false")) - return {}; + return Error::from_string_literal("JsonParser: Expected 'false'"sv); return JsonValue(false); } -Optional<JsonValue> JsonParser::parse_null() +ErrorOr<JsonValue> JsonParser::parse_null() { if (!consume_specific("null")) - return {}; + return Error::from_string_literal("JsonParser: Expected 'null'"sv); return JsonValue(JsonValue::Type::Null); } -Optional<JsonValue> JsonParser::parse_helper() +ErrorOr<JsonValue> JsonParser::parse_helper() { ignore_while(is_space); auto type_hint = peek(); @@ -339,17 +332,15 @@ Optional<JsonValue> JsonParser::parse_helper() return parse_null(); } - return {}; + return Error::from_string_literal("JsonParser: Unexpected character"sv); } -Optional<JsonValue> JsonParser::parse() +ErrorOr<JsonValue> JsonParser::parse() { - auto result = parse_helper(); - if (!result.has_value()) - return {}; + auto result = TRY(parse_helper()); ignore_while(is_space); if (!is_eof()) - return {}; + return Error::from_string_literal("JsonParser: Didn't consume all input"sv); return result; } |