@@ -26,6 +26,8 @@ void SQLite::_bind_methods() {
2626
2727 ClassDB::bind_method (D_METHOD (" import_from_json" , " import_path" ), &SQLite::import_from_json);
2828 ClassDB::bind_method (D_METHOD (" export_to_json" , " export_path" ), &SQLite::export_to_json);
29+ ClassDB::bind_method (D_METHOD (" import_from_buffer" , " json_buffer" ), &SQLite::import_from_buffer);
30+ ClassDB::bind_method (D_METHOD (" export_to_buffer" ), &SQLite::export_to_buffer);
2931
3032 ClassDB::bind_method (D_METHOD (" get_autocommit" ), &SQLite::get_autocommit);
3133 ClassDB::bind_method (D_METHOD (" compileoption_used" , " option_name" ), &SQLite::compileoption_used);
@@ -819,15 +821,53 @@ bool SQLite::import_from_json(String import_path) {
819821 ERR_PRINT (" GDSQLite Error: " + String (std::strerror (errno)) + " (" + import_path + " )" );
820822 return false ;
821823 }
824+
822825 std::stringstream buffer;
823826 buffer << ifs.rdbuf ();
824827 std::string str = buffer.str ();
825- String json_string = String::utf8 (str.c_str ());
826828 ifs.close ();
827829
828- /* Attempt to parse the result and, if unsuccessful, throw a parse error specifying the erroneous line */
830+ String json_string = String::utf8 (str.c_str ());
831+ PackedByteArray json_buffer = json_string.to_utf8_buffer ();
832+
833+ return import_from_buffer (json_buffer);
834+ }
835+
836+ bool SQLite::export_to_json (String export_path) {
837+ PackedByteArray json_buffer = export_to_buffer ();
838+ String json_string = json_buffer.get_string_from_utf8 ();
839+
840+ /* Add .json to the import_path String if not present */
841+ String ending = String (" .json" );
842+ if (!export_path.ends_with (ending)) {
843+ export_path += ending;
844+ }
845+ /* Find the real path */
846+ export_path = ProjectSettings::get_singleton ()->globalize_path (export_path.strip_edges ());
847+ CharString dummy_path = export_path.utf8 ();
848+ const char *char_path = dummy_path.get_data ();
849+ // const char *char_path = export_path.alloc_c_string();
850+
851+ std::ofstream ofs (char_path, std::ios::trunc);
852+ if (ofs.fail ()) {
853+ ERR_PRINT (" GDSQLite Error: " + String (std::strerror (errno)) + " (" + export_path + " )" );
854+ return false ;
855+ }
856+
857+ CharString dummy_string = json_string.utf8 ();
858+ ofs << dummy_string.get_data ();
859+ // ofs << json_string.alloc_c_string();
860+ ofs.close ();
861+
862+ return true ;
863+ }
864+
865+ bool SQLite::import_from_buffer (PackedByteArray json_buffer) {
866+ /* Attempt to parse the input json_string and, if unsuccessful, throw a parse error specifying the erroneous line */
867+
829868 Ref<JSON> json;
830869 json.instantiate ();
870+ String json_string = json_buffer.get_string_from_utf8 ();
831871 Error error = json->parse (json_string);
832872 if (error != Error::OK) {
833873 /* Throw a parsing error */
@@ -934,7 +974,7 @@ bool SQLite::import_from_json(String import_path) {
934974 return true ;
935975}
936976
937- bool SQLite::export_to_json (String export_path ) {
977+ PackedByteArray SQLite::export_to_buffer ( ) {
938978 /* Get all names and sql templates for all tables present in the database */
939979 query (String (" SELECT name,sql,type FROM sqlite_master;" ));
940980 TypedArray<Dictionary> database_array = query_result.duplicate (true );
@@ -943,7 +983,7 @@ bool SQLite::export_to_json(String export_path) {
943983 remove_shadow_tables (database_array);
944984#endif
945985 int64_t number_of_objects = database_array.size ();
946- /* Construct a Dictionary for each table, convert it to JSON and write it to file */
986+ /* Construct a Dictionary for each table, convert it to JSON and write it to String */
947987 for (int64_t i = 0 ; i <= number_of_objects - 1 ; i++) {
948988 Dictionary object_dict = database_array[i];
949989
@@ -989,31 +1029,11 @@ bool SQLite::export_to_json(String export_path) {
9891029 }
9901030 }
9911031
992- /* Add .json to the import_path String if not present */
993- String ending = String (" .json" );
994- if (!export_path.ends_with (ending)) {
995- export_path += ending;
996- }
997- /* Find the real path */
998- export_path = ProjectSettings::get_singleton ()->globalize_path (export_path.strip_edges ());
999- CharString dummy_path = export_path.utf8 ();
1000- const char *char_path = dummy_path.get_data ();
1001- // const char *char_path = export_path.alloc_c_string();
1002-
1003- std::ofstream ofs (char_path, std::ios::trunc);
1004- if (ofs.fail ()) {
1005- ERR_PRINT (" GDSQLite Error: " + String (std::strerror (errno)) + " (" + export_path + " )" );
1006- return false ;
1007- }
10081032 Ref<JSON> json;
10091033 json.instantiate ();
10101034 String json_string = json->stringify (database_array, " \t " );
1011- CharString dummy_string = json_string.utf8 ();
1012- ofs << dummy_string.get_data ();
1013- // ofs << json_string.alloc_c_string();
1014- ofs.close ();
1015-
1016- return true ;
1035+ PackedByteArray json_buffer = json_string.to_utf8_buffer ();
1036+ return json_buffer;
10171037}
10181038
10191039bool SQLite::validate_json (const Array &database_array, std::vector<object_struct> &objects_to_import) {
0 commit comments