-Fixed directly serializing XmlElements

This commit is contained in:
James Newton-King
2014-07-04 17:28:36 +12:00
parent 30445090f2
commit 51212d92b1
4 changed files with 148 additions and 5 deletions
@@ -23,6 +23,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
#endregion #endregion
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using System.Text; using System.Text;
#if !(NETFX_CORE || PORTABLE || PORTABLE40) #if !(NETFX_CORE || PORTABLE || PORTABLE40)
using System; using System;
@@ -115,7 +120,81 @@ namespace Newtonsoft.Json.Tests.Converters
return node; return node;
} }
[Test]
public void SerializeXmlElement()
{
string xml = @"<payload>
<Country>6</Country>
<FinancialTransactionApprovalRequestUID>79</FinancialTransactionApprovalRequestUID>
<TransactionStatus>Approved</TransactionStatus>
<StatusChangeComment></StatusChangeComment>
<RequestedBy>Someone</RequestedBy>
</payload>";
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
var result = xmlDocument.FirstChild.ChildNodes.Cast<XmlNode>().ToArray();
var json = JsonConvert.SerializeObject(result, Formatting.Indented); // <--- fails here with the cast message
Assert.AreEqual(@"[
{
""Country"": ""6""
},
{
""FinancialTransactionApprovalRequestUID"": ""79""
},
{
""TransactionStatus"": ""Approved""
},
{
""StatusChangeComment"": """"
},
{
""RequestedBy"": ""Someone""
}
]", json);
}
#if !NET20 #if !NET20
[Test]
public void SerializeXElement()
{
string xml = @"<payload>
<Country>6</Country>
<FinancialTransactionApprovalRequestUID>79</FinancialTransactionApprovalRequestUID>
<TransactionStatus>Approved</TransactionStatus>
<StatusChangeComment></StatusChangeComment>
<RequestedBy>Someone</RequestedBy>
</payload>";
var xmlDocument = XDocument.Parse(xml);
var result = xmlDocument.Root.Nodes().ToArray();
var json = JsonConvert.SerializeObject(result, Formatting.Indented); // <--- fails here with the cast message
Assert.AreEqual(@"[
{
""Country"": ""6""
},
{
""FinancialTransactionApprovalRequestUID"": ""79""
},
{
""TransactionStatus"": ""Approved""
},
{
""StatusChangeComment"": """"
},
{
""RequestedBy"": ""Someone""
}
]", json);
}
public class DecimalContainer public class DecimalContainer
{ {
public decimal Number { get; set; } public decimal Number { get; set; }
@@ -48,6 +48,23 @@ namespace Newtonsoft.Json.Tests
[TestFixture] [TestFixture]
public class JsonTextReaderTest : TestFixtureBase public class JsonTextReaderTest : TestFixtureBase
{ {
[Test]
public void ReadMultilineString()
{
string json = @"""first line
second line
third line""";
JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
Assert.IsTrue(jsonTextReader.Read());
Assert.AreEqual(JsonToken.String, jsonTextReader.TokenType);
Assert.AreEqual(@"first line
second line
third line", jsonTextReader.Value);
}
#if !(NET20 || NET35 || PORTABLE40 || PORTABLE) #if !(NET20 || NET35 || PORTABLE40 || PORTABLE)
[Test] [Test]
public void ReadBigInteger() public void ReadBigInteger()
@@ -429,6 +429,23 @@ OnSerialized_Derived
OnSerialized_Derived_Derived", string.Join(Environment.NewLine, e.ToArray())); OnSerialized_Derived_Derived", string.Join(Environment.NewLine, e.ToArray()));
} }
#endif #endif
[Test]
public void NoStreamingContextParameter()
{
ExportPostData d = new ExportPostData
{
user = "user!",
contract = new Contract
{
contractName = "name!"
}
};
ExceptionAssert.Throws<JsonException>(
"Serialization Callback 'Void Deserialized()' in type 'Newtonsoft.Json.Tests.Serialization.Contract' must have a single parameter of type 'System.Runtime.Serialization.StreamingContext'.",
() => JsonConvert.SerializeObject(d, Formatting.Indented));
}
} }
public class SerializationEventOrderTestObject public class SerializationEventOrderTestObject
@@ -523,4 +540,34 @@ OnSerialized_Derived_Derived", string.Join(Environment.NewLine, e.ToArray()));
Events.Add("OnDeserialized_Derived_Derived"); Events.Add("OnDeserialized_Derived_Derived");
} }
} }
public class ExportPostData
{
public Contract contract { get; set; }
public bool includeSubItems { get; set; }
public string user { get; set; }
public string[] projects { get; set; }
}
public class Contract
{
public string _id { get; set; }
public string contractName { get; set; }
public string contractNumber { get; set; }
public string updatedBy { get; set; }
public DateTime updated_at { get; set; }
private bool _onDeserializedCalled;
public bool GetOnDeserializedCalled()
{
return _onDeserializedCalled;
}
[OnDeserialized]
internal void Deserialized()
{
_onDeserializedCalled = true;
}
}
} }
@@ -217,7 +217,7 @@ namespace Newtonsoft.Json.Converters
get { return _documentType.InternalSubset; } get { return _documentType.InternalSubset; }
} }
public new string LocalName public override string LocalName
{ {
get { return "DOCTYPE"; } get { return "DOCTYPE"; }
} }
@@ -243,7 +243,7 @@ namespace Newtonsoft.Json.Converters
get { return _node.NodeType; } get { return _node.NodeType; }
} }
public string LocalName public virtual string LocalName
{ {
get { return _node.LocalName; } get { return _node.LocalName; }
} }
@@ -261,7 +261,7 @@ namespace Newtonsoft.Json.Converters
} }
} }
private IXmlNode WrapNode(XmlNode node) internal static IXmlNode WrapNode(XmlNode node)
{ {
switch (node.NodeType) switch (node.NodeType)
{ {
@@ -445,7 +445,7 @@ namespace Newtonsoft.Json.Converters
get { return _documentType.InternalSubset; } get { return _documentType.InternalSubset; }
} }
public new string LocalName public override string LocalName
{ {
get { return "DOCTYPE"; } get { return "DOCTYPE"; }
} }
@@ -928,7 +928,7 @@ namespace Newtonsoft.Json.Converters
#endif #endif
#if !(NETFX_CORE || PORTABLE) #if !(NETFX_CORE || PORTABLE)
if (value is XmlNode) if (value is XmlNode)
return new XmlNodeWrapper((XmlNode)value); return XmlNodeWrapper.WrapNode((XmlNode)value);
#endif #endif
throw new ArgumentException("Value must be an XML object.", "value"); throw new ArgumentException("Value must be an XML object.", "value");