1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 05:10:03 -04:00

Replace JSON with RON for persistence (epi/eframe/glium/web)

This commit is contained in:
Emil Ernerfeldt
2021-04-05 12:09:01 +02:00
parent 4fc3c6d375
commit aba2108159
9 changed files with 55 additions and 40 deletions

View File

@@ -294,18 +294,21 @@ impl Storage for DummyStorage {
fn flush(&mut self) {}
}
/// Get an deserialize the JSON stored at the given key.
#[cfg(feature = "serde_json")]
/// Get an deserialize the [RON](https://github.com/ron-rs/ron] stored at the given key.
#[cfg(feature = "ron")]
pub fn get_value<T: serde::de::DeserializeOwned>(storage: &dyn Storage, key: &str) -> Option<T> {
storage
.get_string(key)
.and_then(|value| serde_json::from_str(&value).ok())
.and_then(|value| ron::from_str(&value).ok())
}
/// Serialize the given value as JSON and store with the given key.
#[cfg(feature = "serde_json")]
/// Serialize the given value as [RON](https://github.com/ron-rs/ron] and store with the given key.
#[cfg(feature = "ron")]
pub fn set_value<T: serde::Serialize>(storage: &mut dyn Storage, key: &str, value: &T) {
storage.set_string(key, serde_json::to_string_pretty(value).unwrap());
storage.set_string(
key,
ron::ser::to_string_pretty(value, Default::default()).unwrap(),
);
}
/// [`Storage`] key used for app