absent or null
Tags: option, toml, json, ron, choosing an engine
Features: json, toml, ron
What each engine writes for a value that is there and holds nothing.
{} and null are different documents, and an absent key is a third thing
again. Which of the three an engine produces for None decides what a
schema can say about it: a property that may be null is not a property that
may be missing, and the two are written differently.
The engines answer in two ways, and the run below says which is which.
Note what the two that do have a null still do differently: ron spells the
Option in the document, so a hand-edited file has to say Some(...),
while json writes the value bare and lets null carry the absence.
TOML has no null, so a key holding nothing is a key that is not written.
That is how every TOML config expresses an optional setting, and it means
the format answers set(None) and delete with the same document - a
distinction the other engines keep and this one cannot.
What was done
Section titled “What was done”let held = Held::new_with(&store)?;
held.note().durable().set(Some("here".to_string()))?;let with_value = std::fs::read_to_string(path.path())?;
held.note().durable().set(None)?;let with_nothing = std::fs::read_to_string(path.path())?;
let read: Option<Option<String>> = store.get(note_path())?;What the run printed
Section titled “What the run printed”json - a value
Section titled “json - a value”held.note().durable().set(Some("here".to_string()))?;let with_value = std::fs::read_to_string(path.path())?;{ "maybe": { "note": "here" }}json - the same key set to nothing
Section titled “json - the same key set to nothing”held.note().durable().set(None)?;let with_nothing = std::fs::read_to_string(path.path())?;{ "maybe": { "note": null }}get::<Option<String>> reads back Some(None).
ron - a value
Section titled “ron - a value”held.note().durable().set(Some("here".to_string()))?;let with_value = std::fs::read_to_string(path.path())?;{ "maybe": { "note": Some("here"), },}ron - the same key set to nothing
Section titled “ron - the same key set to nothing”held.note().durable().set(None)?;let with_nothing = std::fs::read_to_string(path.path())?;{ "maybe": { "note": None, },}get::<Option<String>> reads back Some(None).
toml - a value
Section titled “toml - a value”held.note().durable().set(Some("here".to_string()))?;let with_value = std::fs::read_to_string(path.path())?;[maybe]note = "here"toml - the same key set to nothing
Section titled “toml - the same key set to nothing”held.note().durable().set(None)?;let with_nothing = std::fs::read_to_string(path.path())?;[maybe]get::<Option<String>> reads back None.