godot/core/io/json.cpp

552 lines
15 KiB
C++
Raw Normal View History

2014-02-10 09:10:30 +08:00
/*************************************************************************/
/* json.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 09:10:30 +08:00
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 09:10:30 +08:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2014-02-10 09:10:30 +08:00
#include "json.h"
#include "core/string/print_string.h"
2014-02-10 09:10:30 +08:00
const char *JSON::tk_name[TK_MAX] = {
2014-02-10 09:10:30 +08:00
"'{'",
"'}'",
"'['",
"']'",
"identifier",
"string",
"number",
"':'",
"','",
"EOF",
};
String JSON::_make_indent(const String &p_indent, int p_size) {
String indent_text = "";
2020-12-15 20:04:21 +08:00
if (!p_indent.is_empty()) {
for (int i = 0; i < p_size; i++) {
indent_text += p_indent;
}
}
return indent_text;
}
String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, Set<const void *> &p_markers, bool p_full_precision) {
String colon = ":";
String end_statement = "";
2020-12-15 20:04:21 +08:00
if (!p_indent.is_empty()) {
colon += " ";
end_statement += "\n";
}
2014-02-10 09:10:30 +08:00
switch (p_var.get_type()) {
case Variant::NIL:
return "null";
case Variant::BOOL:
return p_var.operator bool() ? "true" : "false";
case Variant::INT:
return itos(p_var);
case Variant::FLOAT: {
double num = p_var;
if (p_full_precision) {
// Store unreliable digits (17) instead of just reliable
// digits (14) so that the value can be decoded exactly.
return String::num(num, 17 - (int)floor(log10(num)));
} else {
// Store only reliable digits (14) by default.
return String::num(num, 14 - (int)floor(log10(num)));
}
}
case Variant::PACKED_INT32_ARRAY:
case Variant::PACKED_INT64_ARRAY:
case Variant::PACKED_FLOAT32_ARRAY:
case Variant::PACKED_FLOAT64_ARRAY:
case Variant::PACKED_STRING_ARRAY:
2014-02-10 09:10:30 +08:00
case Variant::ARRAY: {
String s = "[";
s += end_statement;
2014-02-10 09:10:30 +08:00
Array a = p_var;
ERR_FAIL_COND_V_MSG(p_markers.has(a.id()), "\"[...]\"", "Converting circular structure to JSON.");
p_markers.insert(a.id());
for (int i = 0; i < a.size(); i++) {
if (i > 0) {
s += ",";
s += end_statement;
}
s += _make_indent(p_indent, p_cur_indent + 1) + _stringify(a[i], p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
2014-02-10 09:10:30 +08:00
}
s += end_statement + _make_indent(p_indent, p_cur_indent) + "]";
p_markers.erase(a.id());
2014-02-10 09:10:30 +08:00
return s;
}
2014-02-10 09:10:30 +08:00
case Variant::DICTIONARY: {
String s = "{";
s += end_statement;
2014-02-10 09:10:30 +08:00
Dictionary d = p_var;
ERR_FAIL_COND_V_MSG(p_markers.has(d.id()), "\"{...}\"", "Converting circular structure to JSON.");
p_markers.insert(d.id());
2014-02-10 09:10:30 +08:00
List<Variant> keys;
d.get_key_list(&keys);
if (p_sort_keys) {
keys.sort();
}
bool first_key = true;
for (Variant &E : keys) {
if (first_key) {
first_key = false;
} else {
s += ",";
s += end_statement;
}
s += _make_indent(p_indent, p_cur_indent + 1) + _stringify(String(E), p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
s += colon;
s += _stringify(d[E], p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
2014-02-10 09:10:30 +08:00
}
s += end_statement + _make_indent(p_indent, p_cur_indent) + "}";
p_markers.erase(d.id());
2014-02-10 09:10:30 +08:00
return s;
}
default:
return "\"" + String(p_var).json_escape() + "\"";
2014-02-10 09:10:30 +08:00
}
}
Error JSON::_get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str) {
while (p_len > 0) {
switch (p_str[index]) {
2014-02-10 09:10:30 +08:00
case '\n': {
line++;
index++;
2014-02-10 09:10:30 +08:00
break;
}
2014-02-10 09:10:30 +08:00
case 0: {
r_token.type = TK_EOF;
2014-02-10 09:10:30 +08:00
return OK;
} break;
case '{': {
r_token.type = TK_CURLY_BRACKET_OPEN;
index++;
2014-02-10 09:10:30 +08:00
return OK;
}
2014-02-10 09:10:30 +08:00
case '}': {
r_token.type = TK_CURLY_BRACKET_CLOSE;
index++;
2014-02-10 09:10:30 +08:00
return OK;
}
2014-02-10 09:10:30 +08:00
case '[': {
r_token.type = TK_BRACKET_OPEN;
index++;
2014-02-10 09:10:30 +08:00
return OK;
}
2014-02-10 09:10:30 +08:00
case ']': {
r_token.type = TK_BRACKET_CLOSE;
index++;
2014-02-10 09:10:30 +08:00
return OK;
}
2014-02-10 09:10:30 +08:00
case ':': {
r_token.type = TK_COLON;
index++;
2014-02-10 09:10:30 +08:00
return OK;
}
2014-02-10 09:10:30 +08:00
case ',': {
r_token.type = TK_COMMA;
index++;
2014-02-10 09:10:30 +08:00
return OK;
}
2014-02-10 09:10:30 +08:00
case '"': {
index++;
2014-02-10 09:10:30 +08:00
String str;
while (true) {
if (p_str[index] == 0) {
r_err_str = "Unterminated String";
2014-02-10 09:10:30 +08:00
return ERR_PARSE_ERROR;
} else if (p_str[index] == '"') {
index++;
2014-02-10 09:10:30 +08:00
break;
} else if (p_str[index] == '\\') {
2014-02-10 09:10:30 +08:00
//escaped characters...
index++;
char32_t next = p_str[index];
if (next == 0) {
r_err_str = "Unterminated String";
return ERR_PARSE_ERROR;
2014-02-10 09:10:30 +08:00
}
char32_t res = 0;
2014-02-10 09:10:30 +08:00
switch (next) {
case 'b':
res = 8;
break;
case 't':
res = 9;
break;
case 'n':
res = 10;
break;
case 'f':
res = 12;
break;
case 'r':
res = 13;
break;
2014-02-10 09:10:30 +08:00
case 'u': {
// hex number
for (int j = 0; j < 4; j++) {
char32_t c = p_str[index + j + 1];
if (c == 0) {
r_err_str = "Unterminated String";
2014-02-10 09:10:30 +08:00
return ERR_PARSE_ERROR;
}
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
r_err_str = "Malformed hex constant in string";
2014-02-10 09:10:30 +08:00
return ERR_PARSE_ERROR;
}
char32_t v;
if (c >= '0' && c <= '9') {
v = c - '0';
} else if (c >= 'a' && c <= 'f') {
v = c - 'a';
v += 10;
} else if (c >= 'A' && c <= 'F') {
v = c - 'A';
v += 10;
2014-02-10 09:10:30 +08:00
} else {
ERR_PRINT("Bug parsing hex constant.");
v = 0;
2014-02-10 09:10:30 +08:00
}
res <<= 4;
res |= v;
2014-02-10 09:10:30 +08:00
}
index += 4; //will add at the end anyway
2014-02-10 09:10:30 +08:00
if ((res & 0xfffffc00) == 0xd800) {
if (p_str[index + 1] != '\\' || p_str[index + 2] != 'u') {
r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
return ERR_PARSE_ERROR;
}
index += 2;
char32_t trail = 0;
for (int j = 0; j < 4; j++) {
char32_t c = p_str[index + j + 1];
if (c == 0) {
r_err_str = "Unterminated String";
return ERR_PARSE_ERROR;
}
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
r_err_str = "Malformed hex constant in string";
return ERR_PARSE_ERROR;
}
char32_t v;
if (c >= '0' && c <= '9') {
v = c - '0';
} else if (c >= 'a' && c <= 'f') {
v = c - 'a';
v += 10;
} else if (c >= 'A' && c <= 'F') {
v = c - 'A';
v += 10;
} else {
ERR_PRINT("Bug parsing hex constant.");
v = 0;
}
trail <<= 4;
trail |= v;
}
if ((trail & 0xfffffc00) == 0xdc00) {
res = (res << 10UL) + trail - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
index += 4; //will add at the end anyway
} else {
r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
return ERR_PARSE_ERROR;
}
} else if ((res & 0xfffffc00) == 0xdc00) {
r_err_str = "Invalid UTF-16 sequence in string, unpaired trail surrogate";
return ERR_PARSE_ERROR;
}
2014-02-10 09:10:30 +08:00
} break;
default: {
res = next;
2014-02-10 09:10:30 +08:00
} break;
}
str += res;
2014-02-10 09:10:30 +08:00
} else {
if (p_str[index] == '\n') {
2014-02-10 09:10:30 +08:00
line++;
}
str += p_str[index];
2014-02-10 09:10:30 +08:00
}
index++;
2014-02-10 09:10:30 +08:00
}
r_token.type = TK_STRING;
r_token.value = str;
2014-02-10 09:10:30 +08:00
return OK;
} break;
default: {
if (p_str[index] <= 32) {
index++;
2014-02-10 09:10:30 +08:00
break;
}
if (p_str[index] == '-' || (p_str[index] >= '0' && p_str[index] <= '9')) {
2014-02-10 09:10:30 +08:00
//a number
const char32_t *rptr;
double number = String::to_float(&p_str[index], &rptr);
index += (rptr - &p_str[index]);
r_token.type = TK_NUMBER;
r_token.value = number;
2014-02-10 09:10:30 +08:00
return OK;
} else if ((p_str[index] >= 'A' && p_str[index] <= 'Z') || (p_str[index] >= 'a' && p_str[index] <= 'z')) {
2014-02-10 09:10:30 +08:00
String id;
while ((p_str[index] >= 'A' && p_str[index] <= 'Z') || (p_str[index] >= 'a' && p_str[index] <= 'z')) {
id += p_str[index];
index++;
2014-02-10 09:10:30 +08:00
}
r_token.type = TK_IDENTIFIER;
r_token.value = id;
2014-02-10 09:10:30 +08:00
return OK;
} else {
r_err_str = "Unexpected character.";
2014-02-10 09:10:30 +08:00
return ERR_PARSE_ERROR;
}
}
}
}
return ERR_PARSE_ERROR;
}
Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) {
if (token.type == TK_CURLY_BRACKET_OPEN) {
Dictionary d;
Error err = _parse_object(d, p_str, index, p_len, line, r_err_str);
if (err) {
2014-02-10 09:10:30 +08:00
return err;
}
value = d;
} else if (token.type == TK_BRACKET_OPEN) {
Array a;
Error err = _parse_array(a, p_str, index, p_len, line, r_err_str);
if (err) {
2014-02-10 09:10:30 +08:00
return err;
}
value = a;
} else if (token.type == TK_IDENTIFIER) {
2014-02-10 09:10:30 +08:00
String id = token.value;
if (id == "true") {
value = true;
} else if (id == "false") {
value = false;
} else if (id == "null") {
value = Variant();
} else {
r_err_str = "Expected 'true','false' or 'null', got '" + id + "'.";
2014-02-10 09:10:30 +08:00
return ERR_PARSE_ERROR;
}
} else if (token.type == TK_NUMBER) {
value = token.value;
} else if (token.type == TK_STRING) {
value = token.value;
2014-02-10 09:10:30 +08:00
} else {
r_err_str = "Expected value, got " + String(tk_name[token.type]) + ".";
2014-02-10 09:10:30 +08:00
return ERR_PARSE_ERROR;
}
return OK;
2014-02-10 09:10:30 +08:00
}
Error JSON::_parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) {
2014-02-10 09:10:30 +08:00
Token token;
bool need_comma = false;
2014-02-10 09:10:30 +08:00
while (index < p_len) {
Error err = _get_token(p_str, index, p_len, token, line, r_err_str);
if (err != OK) {
2014-02-10 09:10:30 +08:00
return err;
}
2014-02-10 09:10:30 +08:00
if (token.type == TK_BRACKET_CLOSE) {
2014-02-10 09:10:30 +08:00
return OK;
}
if (need_comma) {
if (token.type != TK_COMMA) {
r_err_str = "Expected ','";
2014-02-10 09:10:30 +08:00
return ERR_PARSE_ERROR;
} else {
need_comma = false;
2014-02-10 09:10:30 +08:00
continue;
}
}
Variant v;
err = _parse_value(v, token, p_str, index, p_len, line, r_err_str);
if (err) {
2014-02-10 09:10:30 +08:00
return err;
}
2014-02-10 09:10:30 +08:00
array.push_back(v);
need_comma = true;
2014-02-10 09:10:30 +08:00
}
r_err_str = "Expected ']'";
return ERR_PARSE_ERROR;
2014-02-10 09:10:30 +08:00
}
Error JSON::_parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) {
bool at_key = true;
2014-02-10 09:10:30 +08:00
String key;
Token token;
bool need_comma = false;
2014-02-10 09:10:30 +08:00
while (index < p_len) {
2014-02-10 09:10:30 +08:00
if (at_key) {
Error err = _get_token(p_str, index, p_len, token, line, r_err_str);
if (err != OK) {
2014-02-10 09:10:30 +08:00
return err;
}
2014-02-10 09:10:30 +08:00
if (token.type == TK_CURLY_BRACKET_CLOSE) {
2014-02-10 09:10:30 +08:00
return OK;
}
if (need_comma) {
if (token.type != TK_COMMA) {
r_err_str = "Expected '}' or ','";
2014-02-10 09:10:30 +08:00
return ERR_PARSE_ERROR;
} else {
need_comma = false;
2014-02-10 09:10:30 +08:00
continue;
}
}
if (token.type != TK_STRING) {
r_err_str = "Expected key";
2014-02-10 09:10:30 +08:00
return ERR_PARSE_ERROR;
}
key = token.value;
err = _get_token(p_str, index, p_len, token, line, r_err_str);
if (err != OK) {
2014-02-10 09:10:30 +08:00
return err;
}
if (token.type != TK_COLON) {
r_err_str = "Expected ':'";
2014-02-10 09:10:30 +08:00
return ERR_PARSE_ERROR;
}
at_key = false;
2014-02-10 09:10:30 +08:00
} else {
Error err = _get_token(p_str, index, p_len, token, line, r_err_str);
if (err != OK) {
2014-02-10 09:10:30 +08:00
return err;
}
2014-02-10 09:10:30 +08:00
Variant v;
err = _parse_value(v, token, p_str, index, p_len, line, r_err_str);
if (err) {
2014-02-10 09:10:30 +08:00
return err;
}
object[key] = v;
need_comma = true;
at_key = true;
2014-02-10 09:10:30 +08:00
}
}
r_err_str = "Expected '}'";
return ERR_PARSE_ERROR;
2014-02-10 09:10:30 +08:00
}
Error JSON::_parse_string(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line) {
const char32_t *str = p_json.ptr();
2014-02-10 09:10:30 +08:00
int idx = 0;
int len = p_json.length();
Token token;
r_err_line = 0;
2014-02-10 09:10:30 +08:00
String aux_key;
Error err = _get_token(str, idx, len, token, r_err_line, r_err_str);
if (err) {
2014-02-10 09:10:30 +08:00
return err;
}
2014-02-10 09:10:30 +08:00
err = _parse_value(r_ret, token, str, idx, len, r_err_line, r_err_str);
2014-02-10 09:10:30 +08:00
// 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;
2014-02-10 09:10:30 +08:00
}
String JSON::stringify(const Variant &p_var, const String &p_indent, bool p_sort_keys, bool p_full_precision) {
Set<const void *> markers;
return _stringify(p_var, p_indent, 0, p_sort_keys, markers, p_full_precision);
}
Error JSON::parse(const String &p_json_string) {
Error err = _parse_string(p_json_string, data, err_str, err_line);
if (err == Error::OK) {
err_line = 0;
}
return err;
}
void JSON::_bind_methods() {
ClassDB::bind_method(D_METHOD("stringify", "data", "indent", "sort_keys", "full_precision"), &JSON::stringify, DEFVAL(""), DEFVAL(true), DEFVAL(false));
ClassDB::bind_method(D_METHOD("parse", "json_string"), &JSON::parse);
ClassDB::bind_method(D_METHOD("get_data"), &JSON::get_data);
ClassDB::bind_method(D_METHOD("get_error_line"), &JSON::get_error_line);
ClassDB::bind_method(D_METHOD("get_error_message"), &JSON::get_error_message);
}