-Fixed directly serializing XmlElements
This commit is contained in:
@@ -23,6 +23,11 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
using System.Linq;
|
||||
#endif
|
||||
using System.Text;
|
||||
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
using System;
|
||||
@@ -115,7 +120,81 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
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
|
||||
[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 decimal Number { get; set; }
|
||||
|
||||
@@ -48,6 +48,23 @@ namespace Newtonsoft.Json.Tests
|
||||
[TestFixture]
|
||||
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)
|
||||
[Test]
|
||||
public void ReadBigInteger()
|
||||
|
||||
@@ -429,6 +429,23 @@ OnSerialized_Derived
|
||||
OnSerialized_Derived_Derived", string.Join(Environment.NewLine, e.ToArray()));
|
||||
}
|
||||
#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
|
||||
@@ -523,4 +540,34 @@ OnSerialized_Derived_Derived", string.Join(Environment.NewLine, e.ToArray()));
|
||||
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; }
|
||||
}
|
||||
|
||||
public new string LocalName
|
||||
public override string LocalName
|
||||
{
|
||||
get { return "DOCTYPE"; }
|
||||
}
|
||||
@@ -243,7 +243,7 @@ namespace Newtonsoft.Json.Converters
|
||||
get { return _node.NodeType; }
|
||||
}
|
||||
|
||||
public string LocalName
|
||||
public virtual string 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)
|
||||
{
|
||||
@@ -445,7 +445,7 @@ namespace Newtonsoft.Json.Converters
|
||||
get { return _documentType.InternalSubset; }
|
||||
}
|
||||
|
||||
public new string LocalName
|
||||
public override string LocalName
|
||||
{
|
||||
get { return "DOCTYPE"; }
|
||||
}
|
||||
@@ -928,7 +928,7 @@ namespace Newtonsoft.Json.Converters
|
||||
#endif
|
||||
#if !(NETFX_CORE || PORTABLE)
|
||||
if (value is XmlNode)
|
||||
return new XmlNodeWrapper((XmlNode)value);
|
||||
return XmlNodeWrapper.WrapNode((XmlNode)value);
|
||||
#endif
|
||||
|
||||
throw new ArgumentException("Value must be an XML object.", "value");
|
||||
|
||||
Reference in New Issue
Block a user