Serialize custom persistent fields as base64
This still takes up more space than plain JSON would, but I don't know if serde_json would allow inlining that JSON. Presumably not. It would get hairy quickly as a regular embedded JSON string due to the encoding.
This commit is contained in:
parent
29a0f83163
commit
3111d75b29
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -8,6 +8,12 @@ version = "1.0.53"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "94a45b455c14666b85fc40a019e8ab9eb75e3a124e05494f5397122bc9eb06e0"
|
checksum = "94a45b455c14666b85fc40a019e8ab9eb75e3a124e05494f5397122bc9eb06e0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "base64"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "gain"
|
name = "gain"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
@ -31,6 +37,7 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
name = "nih_plug"
|
name = "nih_plug"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"base64",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"nih_plug_derive",
|
"nih_plug_derive",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
|
@ -10,6 +10,7 @@ members = ["nih_plug_derive", "plugins/gain", "xtask"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nih_plug_derive = { path = "nih_plug_derive" }
|
nih_plug_derive = { path = "nih_plug_derive" }
|
||||||
|
base64 = "0.13"
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
//! Utilities for saving a [crate::plugin::Plugin]'s state.
|
//! Utilities for saving a [crate::plugin::Plugin]'s state.
|
||||||
|
|
||||||
|
use serde::de::Error;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
@ -39,7 +40,38 @@ pub(crate) struct State {
|
||||||
/// on the [Params] struct that's annotated with `#[persist = "stable_name"]` will be persisted
|
/// on the [Params] struct that's annotated with `#[persist = "stable_name"]` will be persisted
|
||||||
/// this way.
|
/// this way.
|
||||||
///
|
///
|
||||||
/// TODO: Serialize this as base64 or some other more densely packed representation. Currently
|
/// The individual JSON-serialized fields are encoded as base64 strings so they don't take up as
|
||||||
/// every bute takes up 2-4 ASCII characters
|
/// much space in the preset. Storing them as a plain JSON string would have also been possible,
|
||||||
|
/// but that can get messy with escaping since those will likely also contain double quotes.
|
||||||
|
#[serde(serialize_with = "encode_fields")]
|
||||||
|
#[serde(deserialize_with = "decode_fields")]
|
||||||
pub fields: HashMap<String, Vec<u8>>,
|
pub fields: HashMap<String, Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn encode_fields<S>(bytes: &HashMap<String, Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
serializer.collect_map(
|
||||||
|
bytes
|
||||||
|
.into_iter()
|
||||||
|
.map(|(id, json)| (id, base64::encode(json))),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode_fields<'de, D>(deserializer: D) -> Result<HashMap<String, Vec<u8>>, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
let base64_map: HashMap<String, String> = HashMap::deserialize(deserializer)?;
|
||||||
|
let decoded_map: Result<HashMap<String, Vec<u8>>, D::Error> = base64_map
|
||||||
|
.into_iter()
|
||||||
|
.map(|(id, base64)| {
|
||||||
|
base64::decode(base64)
|
||||||
|
.map(|decoded| (id, decoded))
|
||||||
|
.map_err(|err| D::Error::custom(format!("base64 decode failed: {}", err)))
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(decoded_map?)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue