-Added missing casts to JToken

-Upgraded to Immutable Collections RC
-Tests
This commit is contained in:
James Newton-King
2013-09-13 20:15:30 +12:00
parent 4922c4eedc
commit 5512e2623c
27 changed files with 4688 additions and 12022 deletions
+14 -5
View File
@@ -8,12 +8,13 @@
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.EntityClient;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;
[assembly: EdmSchemaAttribute()]
#region EDM Relationship Metadata
@@ -101,6 +102,7 @@ namespace Newtonsoft.Json.Tests
private ObjectSet<Folder> _Folder;
#endregion
#region AddTo Methods
/// <summary>
@@ -120,11 +122,11 @@ namespace Newtonsoft.Json.Tests
}
#endregion
}
#endregion
#region Entities
/// <summary>
@@ -155,6 +157,7 @@ namespace Newtonsoft.Json.Tests
}
#endregion
#region Primitive Properties
/// <summary>
@@ -257,6 +260,7 @@ namespace Newtonsoft.Json.Tests
partial void OnCreatedDateChanged();
#endregion
#region Navigation Properties
@@ -299,6 +303,7 @@ namespace Newtonsoft.Json.Tests
}
#endregion
}
/// <summary>
@@ -329,6 +334,7 @@ namespace Newtonsoft.Json.Tests
}
#endregion
#region Primitive Properties
/// <summary>
@@ -431,6 +437,7 @@ namespace Newtonsoft.Json.Tests
partial void OnCreatedDateChanged();
#endregion
#region Navigation Properties
@@ -517,8 +524,10 @@ namespace Newtonsoft.Json.Tests
}
#endregion
}
#endregion
}
@@ -24,6 +24,7 @@
#endregion
#if !NETFX_CORE
using System;
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
@@ -56,5 +57,14 @@ namespace Newtonsoft.Json.Tests.Linq
CustomAssert.IsInstanceOfType(typeof(JRaw), r2);
}
[Test]
public void RawToObject()
{
JRaw r1 = new JRaw("1");
int i = r1.ToObject<int>();
Assert.AreEqual(1, i);
}
}
}
@@ -286,6 +286,8 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(5f, (float?)(new JValue(5m)));
Assert.AreEqual(5, (byte)(new JValue(5)));
Assert.AreEqual(null, (sbyte?)new JValue((object)null));
Assert.AreEqual("1", (string)(new JValue(1)));
Assert.AreEqual("1", (string)(new JValue(1.0)));
Assert.AreEqual("1.0", (string)(new JValue(1.0m)));
@@ -322,6 +324,7 @@ namespace Newtonsoft.Json.Tests.Linq
CollectionAssert.AreEquivalent((byte[])Encoding.UTF8.GetBytes("hi"), (byte[])(new JValue(Convert.ToBase64String(Encoding.UTF8.GetBytes("hi")))));
Assert.AreEqual(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"), (Guid)(new JValue(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC").ToByteArray())));
Assert.AreEqual(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"), (Guid?)(new JValue(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC").ToByteArray())));
Assert.AreEqual(1, (sbyte?)(new JValue((short?)1)));
Assert.AreEqual(null, (Uri)(JValue)null);
Assert.AreEqual(null, (int?)(JValue)null);
@@ -437,6 +440,9 @@ namespace Newtonsoft.Json.Tests.Linq
ExceptionAssert.Throws<ArgumentException>("Can not convert Guid to BigInteger.", () => { var i = (new JValue(Guid.NewGuid())).ToObject<BigInteger>(); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Guid to BigInteger.", () => { var i = (new JValue(Guid.NewGuid())).ToObject<BigInteger?>(); });
#endif
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); });
}
[Test]
@@ -516,6 +522,9 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(JToken.DeepEquals(new JValue((decimal?)null), (JValue)(decimal?)null));
Assert.IsTrue(JToken.DeepEquals(new JValue((ulong?)null), (JValue)(ulong?)null));
Assert.IsTrue(JToken.DeepEquals(new JValue((sbyte?)null), (JValue)(sbyte?)null));
Assert.IsTrue(JToken.DeepEquals(new JValue((sbyte)1), (JValue)(sbyte)1));
Assert.IsTrue(JToken.DeepEquals(new JValue((byte?)null), (JValue)(byte?)null));
Assert.IsTrue(JToken.DeepEquals(new JValue((byte)1), (JValue)(byte)1));
Assert.IsTrue(JToken.DeepEquals(new JValue((ushort?)null), (JValue)(ushort?)null));
Assert.IsTrue(JToken.DeepEquals(new JValue(short.MaxValue), (JValue)short.MaxValue));
Assert.IsTrue(JToken.DeepEquals(new JValue(ushort.MaxValue), (JValue)ushort.MaxValue));
@@ -72,8 +72,9 @@
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable">
<HintPath>..\packages\Microsoft.Bcl.Immutable.1.0.12-beta\lib\portable-net45+win8+wp8\System.Collections.Immutable.dll</HintPath>
<Reference Include="System.Collections.Immutable, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.Bcl.Immutable.1.0.23-rc\lib\portable-net45+win8+wp8\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
@@ -23,7 +23,7 @@
<OldToolsVersion>3.5</OldToolsVersion>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<SignAssembly>false</SignAssembly>
<AssemblyOriginatorKeyFile>
</AssemblyOriginatorKeyFile>
@@ -35,9 +35,9 @@
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Reference Include="System.Collections.Immutable, Version=1.0.23.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.Bcl.Immutable.1.0.12-beta\lib\portable-net45+win8+wp8\System.Collections.Immutable.dll</HintPath>
<HintPath>..\packages\Microsoft.Bcl.Immutable.1.0.23-rc\lib\portable-net45+win8+wp8\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
@@ -25,6 +25,7 @@
#if !(SILVERLIGHT || NET20 || NET35 || NETFX_CORE || PORTABLE)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Diagnostics;
@@ -75,4 +75,4 @@ using System.Security;
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("5.0.6.16320")]
[assembly: AssemblyFileVersion("5.0.6.16513")]
@@ -48,7 +48,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void SerializeList()
{
ImmutableList<string> l = ImmutableList.From(new List<string>
ImmutableList<string> l = ImmutableList.CreateRange(new List<string>
{
"One",
"II",
@@ -98,48 +98,48 @@ namespace Newtonsoft.Json.Tests.Serialization
}
#endregion
#region Array
[Test]
public void SerializeArray()
{
ImmutableArray<string> l = ImmutableArray.From(new List<string>
{
"One",
"II",
"3"
});
// #region Array
// [Test]
// public void SerializeArray()
// {
// ImmutableArray<string> l = ImmutableArray.From(new List<string>
// {
// "One",
// "II",
// "3"
// });
string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Assert.AreEqual(@"[
""One"",
""II"",
""3""
]", json);
}
// string json = JsonConvert.SerializeObject(l, Formatting.Indented);
// Assert.AreEqual(@"[
// ""One"",
// ""II"",
// ""3""
//]", json);
// }
[Test]
public void DeserializeArray()
{
string json = @"[
""One"",
""II"",
""3""
]";
// [Test]
// public void DeserializeArray()
// {
// string json = @"[
// ""One"",
// ""II"",
// ""3""
//]";
ImmutableArray<string> l = JsonConvert.DeserializeObject<ImmutableArray<string>>(json);
// ImmutableArray<string> l = JsonConvert.DeserializeObject<ImmutableArray<string>>(json);
Assert.AreEqual(3, l.Length);
Assert.AreEqual("One", l[0]);
Assert.AreEqual("II", l[1]);
Assert.AreEqual("3", l[2]);
}
#endregion
// Assert.AreEqual(3, l.Length);
// Assert.AreEqual("One", l[0]);
// Assert.AreEqual("II", l[1]);
// Assert.AreEqual("3", l[2]);
// }
// #endregion
#region Queue
[Test]
public void SerializeQueue()
{
ImmutableQueue<string> l = ImmutableQueue.From(new List<string>
ImmutableQueue<string> l = ImmutableQueue.CreateRange(new List<string>
{
"One",
"II",
@@ -193,7 +193,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void SerializeStack()
{
ImmutableStack<string> l = ImmutableStack.From(new List<string>
ImmutableStack<string> l = ImmutableStack.CreateRange(new List<string>
{
"One",
"II",
@@ -247,7 +247,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void SerializeHashSet()
{
ImmutableHashSet<string> l = ImmutableHashSet.From(new List<string>
ImmutableHashSet<string> l = ImmutableHashSet.CreateRange(new List<string>
{
"One",
"II",
@@ -302,7 +302,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void SerializeSortedSet()
{
ImmutableSortedSet<string> l = ImmutableSortedSet.From(new List<string>
ImmutableSortedSet<string> l = ImmutableSortedSet.CreateRange(new List<string>
{
"One",
"II",
@@ -339,7 +339,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void SerializeDictionary()
{
ImmutableDictionary<int, string> l = ImmutableDictionary.Create(new Dictionary<int, string>
ImmutableDictionary<int, string> l = ImmutableDictionary.CreateRange(new Dictionary<int, string>
{
{1, "One"},
{2, "II"},
@@ -393,7 +393,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void SerializeSortedDictionary()
{
ImmutableSortedDictionary<int, string> l = ImmutableSortedDictionary.Create(new SortedDictionary<int, string>
ImmutableSortedDictionary<int, string> l = ImmutableSortedDictionary.CreateRange(new SortedDictionary<int, string>
{
{1, "One"},
{2, "II"},
@@ -8581,6 +8581,39 @@ Parameter name: value",
}", json);
}
public class NoConstructorReadOnlyCollection<T> : ReadOnlyCollection<T>
{
public NoConstructorReadOnlyCollection() : base(new List<T>())
{
}
}
[Test]
public void NoConstructorReadOnlyCollectionTest()
{
ExceptionAssert.Throws<JsonSerializationException>(
"Cannot deserialize readonly or fixed size list: Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+NoConstructorReadOnlyCollection`1[System.Int32]. Path '', line 1, position 1.",
() => JsonConvert.DeserializeObject<NoConstructorReadOnlyCollection<int>>("[1]"));
}
#if !(NET40 || NET35 || NET20 || PORTABLE40)
public class NoConstructorReadOnlyDictionary<TKey, TValue> : ReadOnlyDictionary<TKey, TValue>
{
public NoConstructorReadOnlyDictionary()
: base(new Dictionary<TKey, TValue>())
{
}
}
[Test]
public void NoConstructorReadOnlyDictionaryTest()
{
ExceptionAssert.Throws<JsonSerializationException>(
"Cannot deserialize readonly or fixed size dictionary: Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+NoConstructorReadOnlyDictionary`2[System.Int32,System.Int32]. Path '1', line 1, position 5.",
() => JsonConvert.DeserializeObject<NoConstructorReadOnlyDictionary<int, int>>("{'1':1}"));
}
#endif
#if !(PORTABLE || NET35 || NET20 || SILVERLIGHT || PORTABLE40)
[Test]
public void ReadTooLargeInteger()
@@ -9324,6 +9357,42 @@ Parameter name: value",
Assert.AreEqual("ConsoleWriter", restoredDict2.First().Value.PrintTest());
}
#endif
[Test]
public void Main()
{
ParticipantEntity product = new ParticipantEntity();
product.Properties = new Dictionary<string, string> { { "s", "d" } };
string json = JsonConvert.SerializeObject(product);
Console.WriteLine(json);
ParticipantEntity deserializedProduct = JsonConvert.DeserializeObject<ParticipantEntity>(json);
}
internal class ParticipantEntity
{
private Dictionary<string, string> _properties;
[JsonConstructor]
public ParticipantEntity()
{
}
/// <summary>
/// Gets or sets the date and time that the participant was created in the CU.
/// </summary>
[JsonProperty(PropertyName = "pa_created", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore)]
public DateTimeOffset CreationDate { get; internal set; }
/// <summary>
/// Gets the properties of the participant.
/// </summary>
[JsonProperty(PropertyName = "pa_info")]
public Dictionary<string, string> Properties
{
get { return _properties ?? (_properties = new Dictionary<string, string>()); }
set { _properties = value; }
}
}
}
public class PersonReference
+1 -1
View File
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl.Immutable" version="1.0.12-beta" targetFramework="net45" />
<package id="Microsoft.Bcl.Immutable" version="1.0.23-rc" targetFramework="net45" />
<package id="NUnit" version="2.6.2" targetFramework="net45" />
</packages>