From 70c4acb2008e45a7e7ced49dc0fe2c79cfd8d3f3 Mon Sep 17 00:00:00 2001 From: Pyfisch Date: Mon, 7 Oct 2019 09:11:57 +0200 Subject: [PATCH] Implement the serialization of tagged values Add a new method called serialize_tagged_value to the Serializer trait. The method has a default implementation serializing just the value for all formats without tags. --- serde/src/ser/mod.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/serde/src/ser/mod.rs b/serde/src/ser/mod.rs index 8fb2434fa..d89cd3a0b 100644 --- a/serde/src/ser/mod.rs +++ b/serde/src/ser/mod.rs @@ -1247,6 +1247,27 @@ pub trait Serializer: Sized { len: usize, ) -> Result; + /// Serialize a value with a semantic tag for the given format. + /// + /// Tagged values are a mechanism supported by some data formats + /// to add additional information to serialized values and to + /// introduce user-defined subtypes. + /// + /// The default behavior is to ignore the tag and to serialize + /// just the value. + fn serialize_tagged_value( + self, + _format: &'static str, + _tag: &U, + value: &T, + ) -> Result + where + T: Serialize, + U: Serialize, + { + value.serialize(self) + } + /// Collect an iterator as a sequence. /// /// The default implementation serializes each item yielded by the iterator