Serde2File
serialize some data to a file or deserialize a data from a file by serde_json
- Support:
- Encrypted storage is supported when storing files
- Support user-defined encryption method and parameter transfer
- Provide default aes256gsm encryption implementation
- derive macro FileSerializeAndDeserialize Attributes(Optional):
- file_encrypt
- encrypt Data encryption method
- decrypt Data decryption method
- The above encryption and decryption methods must be set at the same time, otherwise encryption and decryption will not be performed.
- #[file_encrypt(encrypt="TestEncryptTool::encrypt",decrypt="TestEncryptTool::decrypt")]
- dump_file_name
- Custom dump file name
- If not set ,the default dump file name is the full name of the current Struct.
- For example, the default dump file name corresponding to serde2file::test::TestData is serde2file-test-TestData.json
- #[dump_file_name = "test_data.json"]
- encrypt_extra_arg_type
- Define additional encryption and decryption parameter types
- Used to pass custom parameters to encryption or decryption functions
- #[encrypt_extra_arg_type = "&'static str"]
将struct序列化化存储为文件,或者从文件反序列化为struct
===
- 支持:
- 文件存储时支持加密存储
- 支持自定义加密方法和参数传递
- 提供默认的aes256gsm加密实现
- 派生宏FileSerializeAndDeserialize属性(可选)
- file_encrypt
- encrypt 数据加密方法
- decrypt 数据解密方法
- 以上加密和解密方法必须同时设置,否则不进行加解密。
- #[file_encrypt(encrypt="TestEncryptTool::encrypt",decrypt="TestEncryptTool::decrypt")]
- dump_file_name
- 设置自定义的转储文件名称
- 自定义转储文件名称
- 如未设置,默认为当前Struct的完整名称,
- 如serde2file::test::TestData对应的缺省转储文件名称为serde2file-test-TestData.json
- #[dump_file_name = "test_data.json"]
- encrypt_extra_arg_type
- 定义额外的加解密参数类型
- 用于向加密或解密函数传递自定义参数使用
- #[encrypt_extra_arg_type = "&'static str"]
- Example1 / 示例1
(1) Simplest use / 最简单用法
Without encryption, dump the struct directly to a json file. The dump file name is the default name
无需加密,直接将struct转储到json文件,转储文件名称为默认名称
use ;
use *;
#cfg
(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.
use ;
use Aes256GcmEncryptUtil;
use *;
/// a encryption and decryption tools
;
/// Test serialization and encrypted storage to file
/// 测试序列化并加密存储到文件
(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
use ;
use Aes256GcmEncryptUtil;
use crate*;
/// a encryption and decryption tools
;
/// Test serialization and encrypted storage to file with extra param
/// 测试序列化并加密存储到文件
/// Test serialization and encrypted storage to file
/// 测试序列化并加密存储到文件