diff --git a/proto/anki/import_export.proto b/proto/anki/import_export.proto index 553dfb2bc..7c2679c4a 100644 --- a/proto/anki/import_export.proto +++ b/proto/anki/import_export.proto @@ -18,7 +18,8 @@ service ImportExportService { rpc ExportAnkiPackage(ExportAnkiPackageRequest) returns (generic.UInt32); rpc GetCsvMetadata(CsvMetadataRequest) returns (CsvMetadata); rpc ImportCsv(ImportCsvRequest) returns (ImportResponse); - rpc ImportJson(generic.String) returns (ImportResponse); + rpc ImportJsonFile(generic.String) returns (ImportResponse); + rpc ImportJsonString(generic.String) returns (ImportResponse); } message ImportCollectionPackageRequest { diff --git a/pylib/anki/collection.py b/pylib/anki/collection.py index 86795387b..2bfe18ebf 100644 --- a/pylib/anki/collection.py +++ b/pylib/anki/collection.py @@ -428,8 +428,11 @@ class Collection(DeprecatedNamesMixin): is_html=is_html, ) - def import_json(self, json: str) -> ImportLogWithChanges: - return self._backend.import_json(json) + def import_json_file(self, path: str) -> ImportLogWithChanges: + return self._backend.import_json_file(path) + + def import_json_string(self, json: str) -> ImportLogWithChanges: + return self._backend.import_json_string(json) # Object helpers ########################################################################## diff --git a/qt/aqt/import_export/importing.py b/qt/aqt/import_export/importing.py index 81a4738b8..d059e6cbc 100644 --- a/qt/aqt/import_export/importing.py +++ b/qt/aqt/import_export/importing.py @@ -96,7 +96,7 @@ class MnemosyneImporter(Importer): QueryOp( parent=mw, op=lambda _: mnemosyne.serialize(path), - success=lambda json: import_json(mw, json), + success=lambda json: import_json_string(mw, json), ).with_progress().run_in_background() @@ -110,6 +110,19 @@ class CsvImporter(Importer): aqt.import_export.import_dialog.ImportDialog(mw, path) +class JsonImporter(Importer): + accepted_file_endings = [".json", ".anki-json"] + + @staticmethod + def do_import(mw: aqt.main.AnkiQt, path: str) -> None: + CollectionOp( + parent=mw, + op=lambda col: col.import_json_file(path), + ).with_backend_progress(import_progress_update).success( + show_import_log + ).run_in_background() + + IMPORTERS: list[Type[Importer]] = [ ColpkgImporter, ApkgImporter, @@ -168,10 +181,12 @@ def import_collection_package_op( ) -def import_json(mw: aqt.main.AnkiQt, json: str) -> None: - CollectionOp(parent=mw, op=lambda col: col.import_json(json)).with_backend_progress( - import_progress_update - ).success(show_import_log).run_in_background() +def import_json_string(mw: aqt.main.AnkiQt, json: str) -> None: + CollectionOp( + parent=mw, op=lambda col: col.import_json_string(json) + ).with_backend_progress(import_progress_update).success( + show_import_log + ).run_in_background() def show_import_log(log_with_changes: ImportLogWithChanges) -> None: diff --git a/rslib/src/backend/import_export.rs b/rslib/src/backend/import_export.rs index 4bfc29f1c..7664db428 100644 --- a/rslib/src/backend/import_export.rs +++ b/rslib/src/backend/import_export.rs @@ -101,8 +101,13 @@ impl ImportExportService for Backend { .map(Into::into) } - fn import_json(&self, input: pb::String) -> Result { - self.with_col(|col| col.import_json(&input.val)) + fn import_json_file(&self, input: pb::String) -> Result { + self.with_col(|col| col.import_json_file(&input.val)) + .map(Into::into) + } + + fn import_json_string(&self, input: pb::String) -> Result { + self.with_col(|col| col.import_json_string(&input.val)) .map(Into::into) } } diff --git a/rslib/src/import_export/text/json.rs b/rslib/src/import_export/text/json.rs index 8c955436c..07b25036b 100644 --- a/rslib/src/import_export/text/json.rs +++ b/rslib/src/import_export/text/json.rs @@ -7,7 +7,13 @@ use crate::{ }; impl Collection { - pub fn import_json(&mut self, json: &str) -> Result> { + pub fn import_json_file(&mut self, path: &str) -> Result> { + let slice = std::fs::read(path)?; + let data: ForeignData = serde_json::from_slice(&slice)?; + data.import(self) + } + + pub fn import_json_string(&mut self, json: &str) -> Result> { let data: ForeignData = serde_json::from_str(json)?; data.import(self) }