Parcourir la source

Moved JSON functions to built-in to_json, parse_json, validate_json

Juan Linietsky il y a 8 ans
Parent
commit
62273e51a2

+ 0 - 22
core/dictionary.cpp

@@ -29,7 +29,6 @@
 #include "dictionary.h"
 #include "safe_refcount.h"
 #include "variant.h"
-#include "io/json.h"
 
 struct _DictionaryVariantHash {
 
@@ -277,22 +276,6 @@ const Variant* Dictionary::next(const Variant* p_key) const {
 	return _p->variant_map.next(p_key);
 }
 
-
-Error Dictionary::parse_json(const String& p_json) {
-
-	String errstr;
-	int errline=0;
-	if (p_json != ""){
-	Error err = JSON::parse(p_json,*this,errstr,errline);
-	if (err!=OK) {
-		ERR_EXPLAIN("Error parsing JSON: "+errstr+" at line: "+itos(errline));
-		ERR_FAIL_COND_V(err!=OK,err);
-		}
-	}
-
-	return OK;
-}
-
 Dictionary Dictionary::copy() const {
 
 	Dictionary n(is_shared());
@@ -307,11 +290,6 @@ Dictionary Dictionary::copy() const {
 	return n;
 }
 
-String Dictionary::to_json() const {
-
-	return JSON::print(*this);
-}
-
 
 void Dictionary::operator=(const Dictionary& p_dictionary) {
 

+ 0 - 3
core/dictionary.h

@@ -63,9 +63,6 @@ public:
 	void clear();
 
 
-	Error parse_json(const String& p_json);
-	String to_json() const;
-
 	bool is_shared() const;
 
 	bool has(const Variant& p_key) const;

+ 8 - 11
core/io/json.cpp

@@ -92,9 +92,9 @@ String JSON::_print_var(const Variant& p_var) {
 
 }
 
-String JSON::print(const Dictionary& p_dict) {
+String JSON::print(const Variant& p_var) {
 
-	return _print_var(p_dict);
+	return _print_var(p_var);
 }
 
 
@@ -450,27 +450,24 @@ Error JSON::_parse_object(Dictionary &object,const CharType *p_str,int &index, i
 }
 
 
-Error JSON::parse(const String& p_json,Dictionary& r_ret,String &r_err_str,int &r_err_line) {
+Error JSON::parse(const String& p_json, Variant &r_ret, String &r_err_str, int &r_err_line) {
 
 
 	const CharType *str = p_json.ptr();
 	int idx = 0;
 	int len = p_json.length();
 	Token token;
-	int line=0;
+	r_err_line=0;
 	String aux_key;
 
-	Error err = _get_token(str,idx,len,token,line,r_err_str);
+	Error err = _get_token(str,idx,len,token,r_err_line,r_err_str);
 	if (err)
 		return err;
 
-	if (token.type!=TK_CURLY_BRACKET_OPEN) {
+	err = _parse_value(r_ret,token,str,idx,len,r_err_line,r_err_str);
 
-		r_err_str="Expected '{'";
-		return ERR_PARSE_ERROR;
-	}
-
-	return _parse_object(r_ret,str,idx,len,r_err_line,r_err_str);
+	return err;
+	
 
 }
 

+ 2 - 2
core/io/json.h

@@ -74,8 +74,8 @@ class JSON {
 	static Error _parse_object(Dictionary &object,const CharType *p_str,int &index, int p_len,int &line,String &r_err_str);
 
 public:
-	static String print(const Dictionary& p_dict);
-	static Error parse(const String& p_json,Dictionary& r_ret,String &r_err_str,int &r_err_line);
+	static String print(const Variant &p_var);
+	static Error parse(const String& p_json,Variant& r_ret,String &r_err_str,int &r_err_line);
 };
 
 #endif // JSON_H

+ 11 - 5
core/variant_call.cpp

@@ -32,6 +32,7 @@
 #include "core_string_names.h"
 #include "script_language.h"
 
+
 typedef void (*VariantFunc)(Variant& r_ret,Variant& p_self,const Variant** p_args);
 typedef void (*VariantConstructFunc)(Variant& r_ret,const Variant** p_args);
 
@@ -146,6 +147,15 @@ struct _VariantCall {
 	};
 
 //	void addfunc(Variant::Type p_type, const StringName& p_name,VariantFunc p_func);
+
+	static void make_func_return_variant(Variant::Type p_type,const StringName& p_name) {
+
+#ifdef DEBUG_ENABLED
+		type_funcs[p_type].functions[p_name].returns=true;
+#endif
+	}
+
+
 	static void addfunc(Variant::Type p_type, Variant::Type p_return,const StringName& p_name,VariantFunc p_func, const Vector<Variant>& p_defaultarg,const Arg& p_argtype1=Arg(),const Arg& p_argtype2=Arg(),const Arg& p_argtype3=Arg(),const Arg& p_argtype4=Arg(),const Arg& p_argtype5=Arg()) {
 
 		FuncData funcdata;
@@ -455,8 +465,6 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var
 	VCALL_LOCALMEM0R(Dictionary,hash);
 	VCALL_LOCALMEM0R(Dictionary,keys);
 	VCALL_LOCALMEM0R(Dictionary,values);
-	VCALL_LOCALMEM1R(Dictionary,parse_json);
-	VCALL_LOCALMEM0R(Dictionary,to_json);
 
 	VCALL_LOCALMEM2(Array,set);
 	VCALL_LOCALMEM1R(Array,get);
@@ -1436,6 +1444,7 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl
 	ADDFUNC0(STRING,RAW_ARRAY,String,to_utf8,varray());
 
 
+
 	ADDFUNC0(VECTOR2,VECTOR2,Vector2,normalized,varray());
 	ADDFUNC0(VECTOR2,REAL,Vector2,length,varray());
 	ADDFUNC0(VECTOR2,REAL,Vector2,angle,varray());
@@ -1556,9 +1565,6 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl
 	ADDFUNC0(DICTIONARY,ARRAY,Dictionary,keys,varray());
 	ADDFUNC0(DICTIONARY,ARRAY,Dictionary,values,varray());
 
-	ADDFUNC1(DICTIONARY,INT,Dictionary,parse_json,STRING,"json",varray());
-	ADDFUNC0(DICTIONARY,STRING,Dictionary,to_json,varray());
-
 	ADDFUNC0(ARRAY,INT,Array,size,varray());
 	ADDFUNC0(ARRAY,BOOL,Array,empty,varray());
 	ADDFUNC0(ARRAY,NIL,Array,clear,varray());

+ 74 - 0
modules/gdscript/gd_functions.cpp

@@ -35,6 +35,7 @@
 #include "os/os.h"
 #include "variant_parser.h"
 #include "io/marshalls.h"
+#include "io/json.h"
 
 const char *GDFunctions::get_func_name(Function p_func) {
 
@@ -103,6 +104,9 @@ const char *GDFunctions::get_func_name(Function p_func) {
 		"load",
 		"inst2dict",
 		"dict2inst",
+		"validate_json",
+		"parse_json",
+		"to_json",
 		"hash",
 		"Color8",
 		"print_stack",
@@ -846,6 +850,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
 			if (p_args[0]->get_type()!=Variant::STRING) {
 				r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
 				r_error.argument=0;
+				r_error.expected=Variant::STRING;
 				r_ret=Variant();
 			} else {
 				r_ret=ResourceLoader::load(*p_args[0]);
@@ -1024,6 +1029,57 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
 			}
 
 		} break;
+		case VALIDATE_JSON: {
+
+			VALIDATE_ARG_COUNT(1);
+
+			if (p_args[0]->get_type()!=Variant::STRING) {
+				r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
+				r_error.argument=0;
+				r_error.expected=Variant::STRING;
+				r_ret=Variant();
+				return;
+			}
+
+			String errs;
+			int errl;
+
+			Error err = JSON::parse(*p_args[0],r_ret,errs,errl);
+
+			if (err!=OK) {
+				r_ret=itos(errl)+":"+errs;
+			} else {
+				r_ret="";
+			}
+
+		} break;
+		case PARSE_JSON: {
+
+			VALIDATE_ARG_COUNT(1);
+
+			if (p_args[0]->get_type()!=Variant::STRING) {
+				r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
+				r_error.argument=0;
+				r_error.expected=Variant::STRING;
+				r_ret=Variant();
+				return;
+			}
+
+			String errs;
+			int errl;
+
+			Error err = JSON::parse(*p_args[0],r_ret,errs,errl);
+
+			if (err!=OK) {
+				r_ret=Variant();
+			}
+
+		} break;
+		case TO_JSON: {
+			VALIDATE_ARG_COUNT(1);
+
+			r_ret = JSON::print(*p_args[0]);
+		} break;
 		case HASH: {
 
 			VALIDATE_ARG_COUNT(1);
@@ -1510,6 +1566,24 @@ MethodInfo GDFunctions::get_info(Function p_func) {
 			mi.return_val.type=Variant::OBJECT;
 			return mi;
 		} break;
+		case VALIDATE_JSON: {
+
+			MethodInfo mi("validate_json:Variant",PropertyInfo(Variant::STRING,"json"));
+			mi.return_val.type=Variant::STRING;
+			return mi;
+		} break;
+		case PARSE_JSON: {
+
+			MethodInfo mi("parse_json:Variant",PropertyInfo(Variant::STRING,"json"));
+			mi.return_val.type=Variant::NIL;
+			return mi;
+		} break;
+		case TO_JSON: {
+
+			MethodInfo mi("to_json",PropertyInfo(Variant::NIL,"var:Variant"));
+			mi.return_val.type=Variant::STRING;
+			return mi;
+		} break;
 		case HASH: {
 
 			MethodInfo mi("hash",PropertyInfo(Variant::NIL,"var:Variant"));

+ 3 - 0
modules/gdscript/gd_functions.h

@@ -97,6 +97,9 @@ public:
 		RESOURCE_LOAD,
 		INST2DICT,
 		DICT2INST,
+		VALIDATE_JSON,
+		PARSE_JSON,
+		TO_JSON,
 		HASH,
 		COLOR8,
 		PRINT_STACK,

+ 1 - 1
modules/gdscript/gd_tokenizer.cpp

@@ -1060,7 +1060,7 @@ void GDTokenizerText::advance(int p_amount) {
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////
 
-#define BYTECODE_VERSION 11
+#define BYTECODE_VERSION 12
 
 Error GDTokenizerBuffer::set_code_buffer(const Vector<uint8_t> & p_buffer) {
 

+ 10 - 2
tools/editor/asset_library_editor_plugin.cpp

@@ -29,7 +29,7 @@
 #include "asset_library_editor_plugin.h"
 #include "editor_node.h"
 #include "editor_settings.h"
-
+#include "io/json.h"
 
 
 
@@ -1076,8 +1076,16 @@ void EditorAssetLibrary::_http_request_completed(int p_status, int p_code, const
 	}
 
 	print_line("response: "+itos(p_status)+" code: "+itos(p_code));
+
 	Dictionary d;
-	d.parse_json(str);
+	{
+		Variant js;
+		String errs;
+		int errl;
+		JSON::parse(str,js,errs,errl);
+		d=js;
+	}
+
 
 	print_line(Variant(d).get_construct_string());
 

+ 3 - 3
tools/editor/editor_import_export.cpp

@@ -783,7 +783,7 @@ Error EditorExportPlatform::export_project_files(EditorExportSaveFunction p_func
 
 		if (atlas_valid) {
 			//compare options
-			Dictionary options;
+			/*Dictionary options;
 			options.parse_json(f->get_line());
 			if (!options.has("lossy_quality") || float(options["lossy_quality"])!=group_lossy_quality)
 				atlas_valid=false;
@@ -794,7 +794,7 @@ Error EditorExportPlatform::export_project_files(EditorExportSaveFunction p_func
 
 			if (!atlas_valid)
 				print_line("JSON INVALID");
-
+*/
 		}
 
 
@@ -921,7 +921,7 @@ Error EditorExportPlatform::export_project_files(EditorExportSaveFunction p_func
 			options["lossy_quality"]=group_lossy_quality;
 			options["shrink"]=EditorImportExport::get_singleton()->image_export_group_get_shrink(E->get());
 			options["image_format"]=group_format;
-			f->store_line(options.to_json());
+//			f->store_line(options.to_json());
 			f->store_line(image_list_md5);
 		}