diff --git a/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs b/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs index ed8fe976a..2c7a861d5 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs @@ -2913,6 +2913,47 @@ public void SerializeDocumentImplicitAttributeNamespace() var deserialized = JsonConvert.DeserializeObject(json); Assert.AreEqual(@"", 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(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(@" + 2001-01-01T11:11:11 +", document.ToString()); + } + } #endif } } diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs index 3521054e2..b8fe74000 100644 --- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs +++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs @@ -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 ? @@ -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); @@ -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); } } @@ -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);