diff options
author | Sergey Minakov <naithar@icloud.com> | 2020-07-28 21:09:01 +0300 |
---|---|---|
committer | Sergey Minakov <naithar@icloud.com> | 2021-02-03 21:10:54 +0300 |
commit | a2676ff810f2769d010d5c48c7f72b86194bec64 (patch) | |
tree | 2b1969ca384c22723583cfebb738ca14881af710 | |
parent | 5f8f049ddb1f322d48b35eec27d594d8eb97acd6 (diff) |
Core: add EOF check for json parser
Additionally reset parse result if error was found.
-rw-r--r-- | core/io/json.cpp | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/core/io/json.cpp b/core/io/json.cpp index bc4527869b..f51f0ceca4 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -301,7 +301,6 @@ Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, in return err; } value = d; - return OK; } else if (token.type == TK_BRACKET_OPEN) { Array a; Error err = _parse_array(a, p_str, index, p_len, line, r_err_str); @@ -309,8 +308,6 @@ Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, in return err; } value = a; - return OK; - } else if (token.type == TK_IDENTIFIER) { String id = token.value; if (id == "true") { @@ -323,18 +320,16 @@ Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, in r_err_str = "Expected 'true','false' or 'null', got '" + id + "'."; return ERR_PARSE_ERROR; } - return OK; - } else if (token.type == TK_NUMBER) { value = token.value; - return OK; } else if (token.type == TK_STRING) { value = token.value; - return OK; } else { r_err_str = "Expected value, got " + String(tk_name[token.type]) + "."; return ERR_PARSE_ERROR; } + + return OK; } Error JSON::_parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) { @@ -453,6 +448,19 @@ Error JSON::parse(const String &p_json, Variant &r_ret, String &r_err_str, int & err = _parse_value(r_ret, token, str, idx, len, r_err_line, r_err_str); + // Check if EOF is reached + // or it's a type of the next token. + if (err == OK && idx < len) { + err = _get_token(str, idx, len, token, r_err_line, r_err_str); + + if (err || token.type != TK_EOF) { + r_err_str = "Expected 'EOF'"; + // Reset return value to empty `Variant` + r_ret = Variant(); + return ERR_PARSE_ERROR; + } + } + return err; } |