mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-24 21:40:48 +02:00
Show case how to use pretty json
This commit is contained in:
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user