Skip to content

Commit a5960b9

Browse files
committed
feat: add insert helper to otherfields
1 parent 4cd7313 commit a5960b9

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

crates/serde/src/other/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ impl OtherFields {
3535
Self { inner }
3636
}
3737

38+
/// Inserts a given value as serialized [`serde_json::Value`] into the map.
39+
pub fn insert_value(&mut self, key: String, value: impl Serialize) -> serde_json::Result<()> {
40+
self.inner.insert(key, serde_json::to_value(value)?);
41+
Ok(())
42+
}
43+
44+
/// Inserts a given value as serialized [`serde_json::Value`] into the map and returns the
45+
/// updated instance.
46+
pub fn with_value(mut self, key: String, value: impl Serialize) -> serde_json::Result<Self> {
47+
self.insert_value(key, value)?;
48+
Ok(self)
49+
}
50+
3851
/// Deserialized this type into another container type.
3952
pub fn deserialize_as<T: DeserializeOwned>(&self) -> serde_json::Result<T> {
4053
serde_json::from_value(Value::Object(self.inner.clone().into_iter().collect()))

0 commit comments

Comments
 (0)