Hello,
I am relatively new to rust and I am currently struggling with the following issue. I have the following class:
#[derive(Serialize, Deserialize, Debug)]
pub struct XmlCountry {
#[serde(rename = "ShortName")]
pub short_name: String,
#[serde(rename = "Name")]
pub full_name: String,
#[serde(rename = "Comment", default)]
pub comment: String,
#[serde(rename = "PrivateComment", default)]
pub private_comment: Option<String>,
#[serde(rename = "Order", default)]
pub order: i32,
#[serde(skip_serializing, skip_deserializing)]
_extra: Option<serde_json::Value>, // To ignore any extra elements
}
I want the Comment and PrivateComment elements to be always written wrapped in:
<![CDATA[]]>
I have tried multiple methods, such as custom serializers etc, but I cannot get it to work. The closest I got was creating a custom CData class and a custom serializer like this:
fn serialize_cdata<S>(value: &CData, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let cdata_str = format!("<![CDATA[{}]]>", value.0);
serializer.serialize_str(&cdata_str)
}
But even with this, in the XML file I get:
<Comment><![CDATA[myvalue]]></Comment>
instead of:
<Comment><![CDATA[myvalue]]></Comment>
Is there really no way to write proper CDATA sections with rust?