Skip to content

Commit

Permalink
-Fixed deserializing non-string values in some XML nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jan 18, 2017
1 parent 8956d3c commit 56e3d75
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
41 changes: 41 additions & 0 deletions Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2913,6 +2913,47 @@ public void SerializeDocumentImplicitAttributeNamespace()
var deserialized = JsonConvert.DeserializeObject<XDocument>(json);
Assert.AreEqual(@"<MyElement xmlns=""http://example.com"" />", deserialized.ToString());
}

public class Model
{
public XElement Document { get; set; }
}

[Test]
public void DeserializeDateInElementText()
{
Model model = new Model();
model.Document = new XElement("Value", new XAttribute("foo", "bar"))
{
Value = "2001-01-01T11:11:11"
};

var serializer = JsonSerializer.Create(new JsonSerializerSettings
{
Converters = new List<JsonConverter>(new[] { new XmlNodeConverter() })
});

var json = new StringBuilder(1024);

using (var stringWriter = new StringWriter(json, CultureInfo.InvariantCulture))
using (var jsonWriter = new JsonTextWriter(stringWriter))
{
jsonWriter.Formatting = Formatting.None;
serializer.Serialize(jsonWriter, model);

Assert.AreEqual(@"{""Document"":{""Value"":{""@foo"":""bar"",""#text"":""2001-01-01T11:11:11""}}}", json.ToString());
}

using (var stringReader = new StringReader(json.ToString()))
using (var jsonReader = new JsonTextReader(stringReader))
{
var document = (XDocument)serializer.Deserialize(jsonReader, typeof(XDocument));

StringAssert.AreEqual(@"<Document>
<Value foo=""bar"">2001-01-01T11:11:11</Value>
</Document>", document.ToString());
}
}
#endif
}
}
Expand Down
24 changes: 12 additions & 12 deletions Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1680,16 +1680,16 @@ private void DeserializeValue(JsonReader reader, IXmlDocument document, XmlNames
switch (propertyName)
{
case TextName:
currentNode.AppendChild(document.CreateTextNode(reader.Value.ToString()));
currentNode.AppendChild(document.CreateTextNode(ConvertTokenToXmlValue(reader)));
break;
case CDataName:
currentNode.AppendChild(document.CreateCDataSection(reader.Value.ToString()));
currentNode.AppendChild(document.CreateCDataSection(ConvertTokenToXmlValue(reader)));
break;
case WhitespaceName:
currentNode.AppendChild(document.CreateWhitespace(reader.Value.ToString()));
currentNode.AppendChild(document.CreateWhitespace(ConvertTokenToXmlValue(reader)));
break;
case SignificantWhitespaceName:
currentNode.AppendChild(document.CreateSignificantWhitespace(reader.Value.ToString()));
currentNode.AppendChild(document.CreateSignificantWhitespace(ConvertTokenToXmlValue(reader)));
break;
default:
// processing instructions and the xml declaration start with ?
Expand Down Expand Up @@ -2072,15 +2072,15 @@ private void CreateInstruction(JsonReader reader, IXmlDocument document, IXmlNod
{
case "@version":
reader.Read();
version = reader.Value.ToString();
version = ConvertTokenToXmlValue(reader);
break;
case "@encoding":
reader.Read();
encoding = reader.Value.ToString();
encoding = ConvertTokenToXmlValue(reader);
break;
case "@standalone":
reader.Read();
standalone = reader.Value.ToString();
standalone = ConvertTokenToXmlValue(reader);
break;
default:
throw JsonSerializationException.Create(reader, "Unexpected property name encountered while deserializing XmlDeclaration: " + reader.Value);
Expand All @@ -2092,7 +2092,7 @@ private void CreateInstruction(JsonReader reader, IXmlDocument document, IXmlNod
}
else
{
IXmlNode instruction = document.CreateProcessingInstruction(propertyName.Substring(1), reader.Value.ToString());
IXmlNode instruction = document.CreateProcessingInstruction(propertyName.Substring(1), ConvertTokenToXmlValue(reader));
currentNode.AppendChild(instruction);
}
}
Expand All @@ -2109,19 +2109,19 @@ private void CreateDocumentType(JsonReader reader, IXmlDocument document, IXmlNo
{
case "@name":
reader.Read();
name = reader.Value.ToString();
name = ConvertTokenToXmlValue(reader);
break;
case "@public":
reader.Read();
publicId = reader.Value.ToString();
publicId = ConvertTokenToXmlValue(reader);
break;
case "@system":
reader.Read();
systemId = reader.Value.ToString();
systemId = ConvertTokenToXmlValue(reader);
break;
case "@internalSubset":
reader.Read();
internalSubset = reader.Value.ToString();
internalSubset = ConvertTokenToXmlValue(reader);
break;
default:
throw JsonSerializationException.Create(reader, "Unexpected property name encountered while deserializing XmlDeclaration: " + reader.Value);
Expand Down

0 comments on commit 56e3d75

Please sign in to comment.