serialize some data to a file or deserialize a data from a file by serde_json
```rust
encrypt = "TestEncryptTool::encrypt",
decrypt = "TestEncryptTool::decrypt",
crypt_arg_type = "&'static str",
dump_file_name = "test_data.json"
)] ```
将struct序列化化存储为文件,或者从文件反序列化为struct
===
```rust
encrypt = "TestEncryptTool::encrypt",
decrypt = "TestEncryptTool::decrypt",
crypt_arg_type = "&'static str",
dump_file_name = "test_data.json"
)] ```
(1) Simplest use / 最简单用法
Without encryption, dump the struct directly to a json file. The dump file name is the default name
无需加密,直接将struct转储到json文件,转储文件名称为默认名称
```rust use serde::{Deserialize, Serialize}; use serde2file::prelude::*;
struct TestData { id: String, name: String, }
mod test { use super::*; /// Test serialization and encrypted storage to file /// 测试序列化并加密存储到文件 #[test] fn testserializetofile() { let data = TestData { id: "01".into(), name: "hy".into(), }; asserteq!("/tmp/[package-name]-Test-Data.json",data.dumptofile("/tmp").unwrap()); } /// Test deserialization from encrypted file /// 测试从加密文件反序列化 #[test] fn testdeserializefromfile() { let data = TestData { id: "01".into(), name: "hy".into(), }; let sdata = TestData::loadfromfile("/tmp").unwrap(); asserteq!(data, sdata) } } ```
(2) Encrypted dump to custom file / 加密转储为自定义文件
Encrypt the dump file. The default aes256gsm encryption is used for encryption.
No additional encryption parameters are required.
Set the dump file name to test_ data.json.
加密转储文件,加密时使用默认的aes256gsm加密实现,无需额外加密参数,设置转储文件名称为test_data.json.
```rust use serde::{Deserialize, Serialize}; use serde2file::encrypt::Aes256GcmEncryptUtil; use serde2file::prelude::*;
dump_file_name = "test_data.json",
encrypt = "TestEncryptTool::encrypt",
decrypt = "TestEncryptTool::decrypt"
)] struct TestData { id: String, name: String, } /// a encryption and decryption tools
struct TestEncryptTool;
impl Aes256GcmEncryptUtil for TestEncryptTool{
type CryptArgType = ();
fn getaeskeynonce(extra:Option
/// Test serialization and encrypted storage to file /// 测试序列化并加密存储到文件
fn testserdefile() { let data = TestData { id: "01".into(), name: "hy".into(), }; asserteq!("/tmp/testdata.json",data.dumptofile("/tmp").unwrap()); let sdata = TestData::loadfromfile("/tmp").unwrap(); asserteq!(data, s_data) }
```
(3) 自定义加密参数加密转储文件
The default aes256gsm encryption is used for encryption, and additional encryption and decryption parameters are required. Set the dump file name to test_ data.json.
加密时使用默认的aes256gsm加密实现,并需要额外加解密参数,设置转储文件名称为test_data.json
```rust use serde::{Deserialize, Serialize}; use super::encrypt::Aes256GcmEncryptUtil; use crate::prelude::*;
dump_file_name = "test_data2.json",
encrypt = "TestEncryptTool::encrypt",
decrypt = "TestEncryptTool::decrypt",
crypt_arg_type = "&'static str"
)] struct TestData2 { id: String, name: String, }
/// a encryption and decryption tools
struct TestEncryptTool;
impl Aes256GcmEncryptUtil for TestEncryptTool{
type CryptArgType = &'static str;
fn getaeskeynonce(extra:Option
/// Test serialization and encrypted storage to file with extra param /// 测试序列化并加密存储到文件
fn testserdefileextra2() { let data = TestData2 { id: "01".into(), name: "hy".into(), }; asserteq!("/tmp/testdata2.json",data.dumptofileextra("/tmp",Some("14H701")).unwrap()); let sdata = TestData2::loadfromfileextra("/tmp",Some("14H701")).unwrap(); asserteq!(data, sdata) }
/// Test serialization and encrypted storage to file /// 测试序列化并加密存储到文件
fn testserdefile2() { let data = TestData2 { id: "01".into(), name: "hy".into(), }; asserteq!("/tmp/testdata2.json",data.dumptofile("/tmp").unwrap()); let sdata = TestData2::loadfromfile("/tmp").unwrap(); asserteq!(data, s_data) }
```