-Refactor

This commit is contained in:
James Newton-King
2014-11-24 22:01:53 +13:00
parent b5ef767774
commit 04e9048d10
9 changed files with 259 additions and 91 deletions
@@ -23,6 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System.Diagnostics;
#if !(NET35 || NET20 || PORTABLE40)
using System;
using System.Collections.Generic;
@@ -52,6 +53,54 @@ namespace Newtonsoft.Json.Tests.Linq
[TestFixture]
public class DynamicTests : TestFixtureBase
{
private void UpdateValueCount(IDictionary<string, int> counts, dynamic d)
{
string s = d.ToString();
int c;
if (!counts.TryGetValue(s, out c))
{
c = 0;
}
c++;
counts[s] = c;
}
[Test]
public void DeserializeLargeDynamic()
{
dynamic d;
using (var jsonFile = System.IO.File.OpenText("large.json"))
using (JsonTextReader jsonTextReader = new JsonTextReader(jsonFile))
{
JsonSerializer serializer = new JsonSerializer();
d = serializer.Deserialize(jsonTextReader);
}
IDictionary<string, int> counts = new Dictionary<string, int>();
Stopwatch sw = new Stopwatch();
sw.Start();
foreach (dynamic o in d)
{
foreach (dynamic friend in o.friends)
{
UpdateValueCount(counts, friend.id);
UpdateValueCount(counts, ((string) friend.name).Split(' ')[0]);
}
}
//foreach (KeyValuePair<string, int> keyValuePair in counts)
//{
// Console.WriteLine(keyValuePair.Key + ": " + keyValuePair.Value);
//}
Console.WriteLine("Time (secs): " + sw.Elapsed.TotalSeconds);
}
[Test]
public void JObjectPropertyNames()
{
@@ -26,6 +26,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using Newtonsoft.Json.Tests.TestObjects;
#if !(NET20 || NET35 || PORTABLE || ASPNETCORE50)
using System.Numerics;
@@ -792,6 +793,20 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(JTokenType.Integer, v.Type);
StringComparison e6 = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e6);
// does not support EnumMember. breaking change to add
ExceptionAssert.Throws<ArgumentException>(() =>
{
d = new JValue("value_a");
EnumA e7 = (EnumA)d;
Assert.AreEqual(EnumA.ValueA, e7);
}, "Requested value 'value_a' was not found.");
}
public enum EnumA
{
[EnumMember(Value = "value_a")]
ValueA
}
#endif
}