Skip to content

Custom serialization

By default, all stores are serialized and deserialized as JSON using the serde_json crate. You can customize this behavior by changing the Marshaler that the plugin uses.

rust
use tauri_plugin_svelte::{PrettyJsonMarshaler, TomlMarshaler};

tauri_plugin_svelte::Builder::new()
  // Sets the default marshaler.
  .marshaler(Box::new(PrettyJsonMarshaler))
  // Sets the marshaler for a specific store.
  .marshaler_of("my-store", Box::new(TomlMarshaler))
  .build();

Currently, the following marshalers are available:

Custom marshaler

You can also implement your own marshaler to serialize and deserialize it in any way you prefer. This is particularly useful if, for instance, you want to encrypt the stored data.

rust
use tauri_plugin_svelte::{Marshaler, MarshalingError, StoreState};

struct SecureMarshaler;

impl Marshaler for SecureMarshaler {
  fn serialize(&self, state: &StoreState) -> Result<Vec<u8>, MarshalingError> {
    Ok(secure_serializer_fn(state)?)
  }

  fn deserialize(&self, bytes: &[u8]) -> Result<StoreState, MarshalingError> {
    Ok(secure_deserializer_fn(bytes)?)
  }
}