Function read_optional_json

Source
pub fn read_optional_json<T>(path: impl AsRef<Path>) -> Result<Option<T>>
where T: for<'de> Deserialize<'de>,
Expand description

Deserializes an optional UTF-8 or binary JSON file into T.

A missing path returns Ok(None); an existing JSON null is still Some(T) when T accepts it. The whole file is read into memory.

§Errors

Returns AppStorageError::Io for filesystem failures other than not found, or AppStorageError::Json when the bytes do not deserialize as T.

§Examples

use ailloli_ui_app_storage::read_optional_json;
use serde_json::Value;
let path = std::env::temp_dir().join(format!("ailloli-doc-missing-json-{}", std::process::id()));
let value: Option<Value> = read_optional_json(path)?;
assert_eq!(value, None);