Skip to content

Serializing CDATA #353

@keithmss

Description

@keithmss

My apologies if this is an inappropriate place to raise this issue.

I have the following structure that I am trying to serialize:

use serde::Serialize;

/// `Description` field for `Tag`.
#[derive(Serialize)]
pub(super) struct Description {
    #[serde(rename = "$value")]
    value: String,
}

impl Description {
    /// Create a new `Description`.
    pub(super) fn new(input: &str) -> Self {
        let value = format!("<![CDATA[{}]]>", input);
        Self { value }
    }
}

As you can see I'm wrapping it in CDATA as per a file format that I'll be writing that I have no control over. This structure is actually just part of a larger structure:

#[derive(Serialize)]
#[serde(rename_all = "PascalCase")]
pub(crate) struct Tag {
    name: String,
    tag_type: TagType,
    data_type: DataType,
    dimensions: Option<usize>,
    radix: Radix,
    constant: Constant,
    external_access: ExternalAccess,
    description: Description,
}

impl Tag {
    /// Return an XML `String` representation of `Tag`.
    pub(crate) fn to_xml(&self) -> Result<String, DeError> {
        to_string(self)
    }
}

Which is serializing perfectly (except for the description field):

<Tag Name="test_dint" TagType="Base" DataType="DINT" Radix="Decimal" Constant="false" ExternalAccess="Read Only"><Description>&lt;![CDATA[Test DINT]]&gt;</Description></Tag>

It looks like when it serializes the CDATA portion is being escaped:

<Description>&lt;![CDATA[Test DINT]]&gt;</Description>

Is there anything I can do to unescape it ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions