Show case how to use pretty json

pull/1860/head
Tony 8 months ago
parent 4f13a03401
commit 73f365d3f0
No known key found for this signature in database
GPG Key ID: 34BDD3EA27824956

@ -37,7 +37,11 @@ pub fn run() {
.plugin(tauri_plugin_os::init()) .plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_process::init()) .plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_store::Builder::default().build()) .plugin(
tauri_plugin_store::Builder::default()
.register_serialize_fn("pretty-json".to_owned(), pretty_json)
.build(),
)
.setup(move |app| { .setup(move |app| {
#[cfg(desktop)] #[cfg(desktop)]
{ {
@ -159,3 +163,9 @@ pub fn run() {
} }
}) })
} }
fn pretty_json(
cache: &std::collections::HashMap<String, serde_json::Value>,
) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
Ok(serde_json::to_vec_pretty(&cache)?)
}

@ -7,7 +7,7 @@
let key; let key;
let value; let value;
let store = new LazyStore("cache.json"); let store = new LazyStore("cache.json", { serializeFnName: "pretty-json" });
let cache = {}; let cache = {};
async function refreshEntries() { async function refreshEntries() {

@ -254,6 +254,23 @@ impl<R: Runtime> Builder<R> {
} }
/// Register a serialize function to access it from the JavaScript side /// Register a serialize function to access it from the JavaScript side
///
/// # Examples
///
/// ```
/// fn pretty_json(
/// cache: &std::collections::HashMap<String, serde_json::Value>,
/// ) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
/// Ok(serde_json::to_vec_pretty(&cache)?)
/// }
///
/// tauri::Builder::default()
/// .plugin(
/// tauri_plugin_store::Builder::default()
/// .register_serialize_fn("pretty-json".to_owned(), pretty_json)
/// .build(),
/// )
/// ```
pub fn register_serialize_fn(mut self, name: String, serialize_fn: SerializeFn) -> Self { pub fn register_serialize_fn(mut self, name: String, serialize_fn: SerializeFn) -> Self {
self.serialize_fns.insert(name, serialize_fn); self.serialize_fns.insert(name, serialize_fn);
self self

Loading…
Cancel
Save