Added EncodeSpecialCharacters setting to XmlNodeConverter (#1866)
This commit is contained in:
@@ -159,6 +159,116 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
Assert.AreEqual("<Time>0001-01-01T00:00:00</Time>", xmlNode.OuterXml);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void XmlNode_Roundtrip_PropertyNameWithColon()
|
||||
{
|
||||
const string initialJson = @"{""Be:fore:After!"":""Value!""}";
|
||||
|
||||
XmlDocument xmlNode = JsonConvert.DeserializeXmlNode(initialJson, null, false, true);
|
||||
|
||||
Assert.AreEqual("<Be_x003A_fore_x003A_After_x0021_>Value!</Be_x003A_fore_x003A_After_x0021_>", xmlNode.OuterXml);
|
||||
|
||||
string json = JsonConvert.SerializeXmlNode(xmlNode);
|
||||
|
||||
Assert.AreEqual(initialJson, json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void XmlNode_Roundtrip_PropertyNameWithEscapedValue()
|
||||
{
|
||||
const string initialJson = @"{""BeforeAfter!"":""Value!""}";
|
||||
|
||||
XmlDocument xmlNode = JsonConvert.DeserializeXmlNode(initialJson);
|
||||
|
||||
Assert.AreEqual("<BeforeAfter_x0021_>Value!</BeforeAfter_x0021_>", xmlNode.OuterXml);
|
||||
|
||||
string json = JsonConvert.SerializeXmlNode(xmlNode);
|
||||
|
||||
Assert.AreEqual(initialJson, json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void XmlNode_EncodeSpecialCharacters()
|
||||
{
|
||||
string initialJson = @"{
|
||||
""?xml"": {
|
||||
""@version"": ""1.0"",
|
||||
""@standalone"": ""no""
|
||||
},
|
||||
""?xml-stylesheet"": ""href=\""classic.xsl\"" type=\""text/xml\"""",
|
||||
""span"": {
|
||||
""@class"": ""vevent"",
|
||||
""a"": {
|
||||
""@class"": ""url"",
|
||||
""@href"": ""http://www.web2con.com/"",
|
||||
""span"": [
|
||||
{
|
||||
""@class"": ""summary"",
|
||||
""#text"": ""Web 2.0 Conference"",
|
||||
""#cdata-section"": ""my escaped text""
|
||||
},
|
||||
{
|
||||
""@class"": ""location"",
|
||||
""#text"": ""Argent Hotel, San Francisco, CA""
|
||||
}
|
||||
],
|
||||
""abbr"": [
|
||||
{
|
||||
""@class"": ""dtstart"",
|
||||
""@title"": ""2005-10-05"",
|
||||
""#text"": ""October 5""
|
||||
},
|
||||
{
|
||||
""@class"": ""dtend"",
|
||||
""@title"": ""2005-10-08"",
|
||||
""#text"": ""7""
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
XmlDocument xmlNode = JsonConvert.DeserializeXmlNode(initialJson, "root", false, true);
|
||||
|
||||
StringAssert.AreEqual(@"<root>
|
||||
<_x003F_xml>
|
||||
<_x0040_version>1.0</_x0040_version>
|
||||
<_x0040_standalone>no</_x0040_standalone>
|
||||
</_x003F_xml>
|
||||
<_x003F_xml-stylesheet>href=""classic.xsl"" type=""text/xml""</_x003F_xml-stylesheet>
|
||||
<span>
|
||||
<_x0040_class>vevent</_x0040_class>
|
||||
<a>
|
||||
<_x0040_class>url</_x0040_class>
|
||||
<_x0040_href>http://www.web2con.com/</_x0040_href>
|
||||
<span>
|
||||
<_x0040_class>summary</_x0040_class>
|
||||
<_x0023_text>Web 2.0 Conference</_x0023_text>
|
||||
<_x0023_cdata-section>my escaped text</_x0023_cdata-section>
|
||||
</span>
|
||||
<span>
|
||||
<_x0040_class>location</_x0040_class>
|
||||
<_x0023_text>Argent Hotel, San Francisco, CA</_x0023_text>
|
||||
</span>
|
||||
<abbr>
|
||||
<_x0040_class>dtstart</_x0040_class>
|
||||
<_x0040_title>2005-10-05</_x0040_title>
|
||||
<_x0023_text>October 5</_x0023_text>
|
||||
</abbr>
|
||||
<abbr>
|
||||
<_x0040_class>dtend</_x0040_class>
|
||||
<_x0040_title>2005-10-08</_x0040_title>
|
||||
<_x0023_text>7</_x0023_text>
|
||||
</abbr>
|
||||
</a>
|
||||
</span>
|
||||
</root>", IndentXml(xmlNode.OuterXml));
|
||||
|
||||
string json = JsonConvert.SerializeXmlNode(xmlNode, Formatting.Indented, true);
|
||||
|
||||
Assert.AreEqual(initialJson, json);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !NET20
|
||||
@@ -184,6 +294,146 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[Test]
|
||||
public void XNode_Roundtrip_PropertyNameWithColon()
|
||||
{
|
||||
const string initialJson = @"{""Be:fore:After!"":""Value!""}";
|
||||
|
||||
XDocument xmlNode = JsonConvert.DeserializeXNode(initialJson, null, false, true);
|
||||
|
||||
Assert.AreEqual("<Be_x003A_fore_x003A_After_x0021_>Value!</Be_x003A_fore_x003A_After_x0021_>", xmlNode.ToString());
|
||||
|
||||
string json = JsonConvert.SerializeXNode(xmlNode);
|
||||
|
||||
Assert.AreEqual(initialJson, json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void XNode_Roundtrip_PropertyNameWithEscapedValue()
|
||||
{
|
||||
const string initialJson = @"{""BeforeAfter!"":""Value!""}";
|
||||
|
||||
XDocument xmlNode = JsonConvert.DeserializeXNode(initialJson);
|
||||
|
||||
Assert.AreEqual("<BeforeAfter_x0021_>Value!</BeforeAfter_x0021_>", xmlNode.ToString());
|
||||
|
||||
string json = JsonConvert.SerializeXNode(xmlNode);
|
||||
|
||||
Assert.AreEqual(initialJson, json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void XNode_EncodeSpecialCharacters()
|
||||
{
|
||||
string initialJson = @"{
|
||||
""?xml"": {
|
||||
""@version"": ""1.0"",
|
||||
""@standalone"": ""no""
|
||||
},
|
||||
""?xml-stylesheet"": ""href=\""classic.xsl\"" type=\""text/xml\"""",
|
||||
""span"": {
|
||||
""@class"": ""vevent"",
|
||||
""a"": {
|
||||
""@class"": ""url"",
|
||||
""@href"": ""http://www.web2con.com/"",
|
||||
""span"": [
|
||||
{
|
||||
""@class"": ""summary"",
|
||||
""#text"": ""Web 2.0 Conference"",
|
||||
""#cdata-section"": ""my escaped text""
|
||||
},
|
||||
{
|
||||
""@class"": ""location"",
|
||||
""#text"": ""Argent Hotel, San Francisco, CA""
|
||||
}
|
||||
],
|
||||
""abbr"": [
|
||||
{
|
||||
""@class"": ""dtstart"",
|
||||
""@title"": ""2005-10-05"",
|
||||
""#text"": ""October 5""
|
||||
},
|
||||
{
|
||||
""@class"": ""dtend"",
|
||||
""@title"": ""2005-10-08"",
|
||||
""#text"": ""7""
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
XDocument xmlNode = JsonConvert.DeserializeXNode(initialJson, "root", false, true);
|
||||
|
||||
StringAssert.AreEqual(@"<root>
|
||||
<_x003F_xml>
|
||||
<_x0040_version>1.0</_x0040_version>
|
||||
<_x0040_standalone>no</_x0040_standalone>
|
||||
</_x003F_xml>
|
||||
<_x003F_xml-stylesheet>href=""classic.xsl"" type=""text/xml""</_x003F_xml-stylesheet>
|
||||
<span>
|
||||
<_x0040_class>vevent</_x0040_class>
|
||||
<a>
|
||||
<_x0040_class>url</_x0040_class>
|
||||
<_x0040_href>http://www.web2con.com/</_x0040_href>
|
||||
<span>
|
||||
<_x0040_class>summary</_x0040_class>
|
||||
<_x0023_text>Web 2.0 Conference</_x0023_text>
|
||||
<_x0023_cdata-section>my escaped text</_x0023_cdata-section>
|
||||
</span>
|
||||
<span>
|
||||
<_x0040_class>location</_x0040_class>
|
||||
<_x0023_text>Argent Hotel, San Francisco, CA</_x0023_text>
|
||||
</span>
|
||||
<abbr>
|
||||
<_x0040_class>dtstart</_x0040_class>
|
||||
<_x0040_title>2005-10-05</_x0040_title>
|
||||
<_x0023_text>October 5</_x0023_text>
|
||||
</abbr>
|
||||
<abbr>
|
||||
<_x0040_class>dtend</_x0040_class>
|
||||
<_x0040_title>2005-10-08</_x0040_title>
|
||||
<_x0023_text>7</_x0023_text>
|
||||
</abbr>
|
||||
</a>
|
||||
</span>
|
||||
</root>", xmlNode.ToString());
|
||||
|
||||
string json = JsonConvert.SerializeXNode(xmlNode, Formatting.Indented, true);
|
||||
|
||||
Assert.AreEqual(initialJson, json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void XNode_MetadataArray_EncodeSpecialCharacters()
|
||||
{
|
||||
string initialJson = @"{
|
||||
""$id"": ""1"",
|
||||
""$values"": [
|
||||
""1"",
|
||||
""2"",
|
||||
""3"",
|
||||
""4"",
|
||||
""5""
|
||||
]
|
||||
}";
|
||||
|
||||
XDocument xmlNode = JsonConvert.DeserializeXNode(initialJson, "root", false, true);
|
||||
|
||||
StringAssert.AreEqual(@"<root>
|
||||
<_x0024_id>1</_x0024_id>
|
||||
<_x0024_values>1</_x0024_values>
|
||||
<_x0024_values>2</_x0024_values>
|
||||
<_x0024_values>3</_x0024_values>
|
||||
<_x0024_values>4</_x0024_values>
|
||||
<_x0024_values>5</_x0024_values>
|
||||
</root>", xmlNode.ToString());
|
||||
|
||||
string json = JsonConvert.SerializeXNode(xmlNode, Formatting.Indented, true);
|
||||
|
||||
Assert.AreEqual(initialJson, json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeDollarProperty()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user