Browse Source

Merge pull request #40804 from naithar/fix/json-parse

JSON parser fix
Rémi Verschelde 4 năm trước cách đây
mục cha
commit
6610289fdd
1 tập tin đã thay đổi với 15 bổ sung7 xóa
  1. 15 7
      core/io/json.cpp

+ 15 - 7
core/io/json.cpp

@@ -347,7 +347,6 @@ Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, in
 			return err;
 			return err;
 		}
 		}
 		value = d;
 		value = d;
-		return OK;
 	} else if (token.type == TK_BRACKET_OPEN) {
 	} else if (token.type == TK_BRACKET_OPEN) {
 		Array a;
 		Array a;
 		Error err = _parse_array(a, p_str, index, p_len, line, r_err_str);
 		Error err = _parse_array(a, p_str, index, p_len, line, r_err_str);
@@ -355,8 +354,6 @@ Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, in
 			return err;
 			return err;
 		}
 		}
 		value = a;
 		value = a;
-		return OK;
-
 	} else if (token.type == TK_IDENTIFIER) {
 	} else if (token.type == TK_IDENTIFIER) {
 		String id = token.value;
 		String id = token.value;
 		if (id == "true") {
 		if (id == "true") {
@@ -369,18 +366,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 + "'.";
 			r_err_str = "Expected 'true','false' or 'null', got '" + id + "'.";
 			return ERR_PARSE_ERROR;
 			return ERR_PARSE_ERROR;
 		}
 		}
-		return OK;
-
 	} else if (token.type == TK_NUMBER) {
 	} else if (token.type == TK_NUMBER) {
 		value = token.value;
 		value = token.value;
-		return OK;
 	} else if (token.type == TK_STRING) {
 	} else if (token.type == TK_STRING) {
 		value = token.value;
 		value = token.value;
-		return OK;
 	} else {
 	} else {
 		r_err_str = "Expected value, got " + String(tk_name[token.type]) + ".";
 		r_err_str = "Expected value, got " + String(tk_name[token.type]) + ".";
 		return ERR_PARSE_ERROR;
 		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) {
 Error JSON::_parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) {
@@ -499,6 +494,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);
 	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;
 	return err;
 }
 }