Function write_json_atomic

Source
pub fn write_json_atomic<T>(path: impl AsRef<Path>, value: &T) -> Result<()>
where T: Serialize,
Expand description

Pretty-serializes value and writes it through a same-directory temp file.

The complete JSON representation is buffered in memory before writing. See atomic_write_bytes for rename, concurrency, and durability semantics.

§Errors

Returns AppStorageError::Json when serialization fails, or propagates atomic_write_bytes destination and filesystem errors.

§Examples

use ailloli_ui_app_storage::write_json_atomic;
use serde_json::json;
let path = std::env::temp_dir().join(format!("ailloli-doc-json-{}.json", std::process::id()));
write_json_atomic(&path, &json!({"ready": true}))?;
assert!(std::fs::read_to_string(&path)?.contains("ready"));
std::fs::remove_file(path)?;