-Improved Portable40 assembly performance by using compiled expression reflection

-Fixed converting string JValues to enums with JToken.ToObject
This commit is contained in:
James Newton-King
2014-08-24 23:33:08 +12:00
parent dce6228b8f
commit 57ccaf00bf
15 changed files with 125 additions and 21 deletions
@@ -137,8 +137,45 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.IsNull(foo2.Bar);
}
[Test]
public void MultipleNamespacesXDocument()
{
string xml = @"<result xp_0:end=""2014-08-15 13:12:11.9184"" xp_0:start=""2014-08-15 13:11:49.3140"" xp_0:time_diff=""22604.3836"" xmlns:xp_0=""Test1"" p2:end=""2014-08-15 13:13:49.5522"" p2:start=""2014-08-15 13:13:49.0268"" p2:time_diff=""525.4646"" xmlns:p2=""Test2"" />";
XDocument d = XDocument.Parse(xml);
string json = JsonConvert.SerializeObject(d, Formatting.Indented);
Console.WriteLine(json);
XDocument doc = JsonConvert.DeserializeObject<XDocument>(json);
Console.WriteLine(doc.ToString());
Assert.AreEqual(xml, doc.ToString());
}
#endif
[Test]
public void MultipleNamespacesXmlDocument()
{
string xml = @"<result xp_0:end=""2014-08-15 13:12:11.9184"" xp_0:start=""2014-08-15 13:11:49.3140"" xp_0:time_diff=""22604.3836"" xmlns:xp_0=""Test1"" p2:end=""2014-08-15 13:13:49.5522"" p2:start=""2014-08-15 13:13:49.0268"" p2:time_diff=""525.4646"" xmlns:p2=""Test2"" />";
XmlDocument d = new XmlDocument();
d.LoadXml(xml);
string json = JsonConvert.SerializeObject(d, Formatting.Indented);
Console.WriteLine(json);
XmlDocument doc = JsonConvert.DeserializeObject<XmlDocument>(json);
Console.WriteLine(doc.OuterXml);
Assert.AreEqual(xml, doc.OuterXml);
}
[Test]
public void SerializeXmlElement()
{
@@ -444,6 +444,9 @@ namespace Newtonsoft.Json.Tests.Linq
ExceptionAssert.Throws<ArgumentException>("Can not convert Date to SByte.", () => { var i = (sbyte?)new JValue(DateTime.Now); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Date to SByte.", () => { var i = (sbyte)new JValue(DateTime.Now); });
ExceptionAssert.Throws<ArgumentException>("Could not convert 'Ordinal1' to StringComparison.", () => { var i = (new JValue("Ordinal1")).ToObject<StringComparison>(); });
ExceptionAssert.Throws<ArgumentException>("Could not convert 'Ordinal1' to StringComparison.", () => { var i = (new JValue("Ordinal1")).ToObject<StringComparison?>(); });
}
[Test]
@@ -492,6 +495,9 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(new Guid("A34B2080-B5F0-488E-834D-45D44ECB9E5C"), (new JValue(new Guid("A34B2080-B5F0-488E-834D-45D44ECB9E5C")).ToObject(typeof(Guid))));
Assert.AreEqual(new Guid("A34B2080-B5F0-488E-834D-45D44ECB9E5C"), (new JValue(new Guid("A34B2080-B5F0-488E-834D-45D44ECB9E5C")).ToObject(typeof(Guid?))));
Assert.AreEqual(new Uri("http://www.google.com/"), (new JValue(new Uri("http://www.google.com/")).ToObject(typeof(Uri))));
Assert.AreEqual(StringComparison.Ordinal, (new JValue("Ordinal").ToObject(typeof(StringComparison))));
Assert.AreEqual(StringComparison.Ordinal, (new JValue("Ordinal").ToObject(typeof(StringComparison?))));
Assert.AreEqual(null, (JValue.CreateNull().ToObject(typeof(StringComparison?))));
}
[Test]
@@ -755,5 +755,47 @@ namespace Newtonsoft.Json.Tests.Linq
""name"": ""Hello World""
}", json);
}
#if !(NET20 || NET35 || PORTABLE40)
[Test]
public void EnumTests()
{
JValue v = new JValue(StringComparison.Ordinal);
Assert.AreEqual(JTokenType.Integer, v.Type);
string s = v.ToString();
Assert.AreEqual("Ordinal", s);
StringComparison e = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.Ordinal, e);
dynamic d = new JValue(StringComparison.CurrentCultureIgnoreCase);
StringComparison e2 = (StringComparison)d;
Assert.AreEqual(StringComparison.CurrentCultureIgnoreCase, e2);
string s1 = d.ToString();
Assert.AreEqual("CurrentCultureIgnoreCase", s1);
string s2 = (string)d;
Assert.AreEqual("CurrentCultureIgnoreCase", s2);
d = new JValue("OrdinalIgnoreCase");
StringComparison e3 = (StringComparison)d;
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e3);
v = new JValue("ORDINAL");
d = v;
StringComparison e4 = (StringComparison)d;
Assert.AreEqual(StringComparison.Ordinal, e4);
StringComparison e5 = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.Ordinal, e5);
v = new JValue((int)StringComparison.InvariantCultureIgnoreCase);
Assert.AreEqual(JTokenType.Integer, v.Type);
StringComparison e6 = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.InvariantCultureIgnoreCase, e6);
}
#endif
}
}
@@ -75,4 +75,4 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.4.17603")]
[assembly: AssemblyFileVersion("6.0.4.17624")]
@@ -23,7 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if !(PORTABLE40 || NET20 || NET35)
#if !(NET20 || NET35)
using System;
using System.Diagnostics;
using System.Reflection;
@@ -299,5 +299,4 @@ namespace Newtonsoft.Json.Tests.Utilities
}
}
}
#endif