-Added .NET 3.5 build

-Added dynamic support to LINQ to JSON
-Added dynamic support to serializer
This commit is contained in:
JamesNK
2010-09-11 08:06:53 +00:00
parent 12a91cce9e
commit 43501484fb
33 changed files with 2105 additions and 48 deletions
@@ -18,12 +18,12 @@ namespace Newtonsoft.Json.Tests.Serialization
_startingWithChar = startingWithChar;
}
protected override IList<JsonProperty> CreateProperties(JsonObjectContract contract)
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> properties = base.CreateProperties(contract);
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
// only serializer properties that start with the specified character
properties =
properties =
properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();
return properties;
@@ -0,0 +1,183 @@
#if !(NET35 || NET20 || SILVERLIGHT)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization.Formatters;
using System.Text;
using Newtonsoft.Json.Utilities;
using NUnit.Framework;
namespace Newtonsoft.Json.Tests.Serialization
{
public class DynamicTests : TestFixtureBase
{
public class DynamicChildObject
{
public string Text { get; set; }
public int Integer { get; set; }
}
public class TestDynamicObject : DynamicObject
{
private readonly Dictionary<string, object> _members;
public int Int;
public DynamicChildObject ChildObject { get; set; }
internal Dictionary<string, object> Members
{
get { return _members; }
}
public TestDynamicObject()
{
_members = new Dictionary<string, object>();
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return _members.Keys.Union(new[] { "Int", "ChildObject" });
}
public override bool TryConvert(ConvertBinder binder, out object result)
{
Type targetType = binder.Type;
if (targetType == typeof(IDictionary<string, object>) ||
targetType == typeof(IDictionary))
{
result = new Dictionary<string, object>(_members);
return true;
}
else
{
return base.TryConvert(binder, out result);
}
}
public override bool TryDeleteMember(DeleteMemberBinder binder)
{
return _members.Remove(binder.Name);
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return _members.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_members[binder.Name] = value;
return true;
}
}
public class ErrorSettingDynamicObject : DynamicObject
{
public override bool TrySetMember(SetMemberBinder binder, object value)
{
return false;
}
}
[Test]
public void SerializeDynamicObject()
{
TestDynamicObject dynamicObject = new TestDynamicObject();
dynamic d = dynamicObject;
d.Int = 1;
d.Decimal = 99.9d;
d.ChildObject = new DynamicChildObject();
Dictionary<string, object> values = new Dictionary<string, object>();
foreach (string memberName in dynamicObject.GetDynamicMemberNames())
{
object value;
dynamicObject.TryGetMember(memberName, out value);
values.Add(memberName, value);
}
Assert.AreEqual(d.Int, values["Int"]);
Assert.AreEqual(d.Decimal, values["Decimal"]);
Assert.AreEqual(d.ChildObject, values["ChildObject"]);
string json = JsonConvert.SerializeObject(dynamicObject, Formatting.Indented);
Assert.AreEqual(@"{
""Decimal"": 99.9,
""Int"": 1,
""ChildObject"": {
""Text"": null,
""Integer"": 0
}
}", json);
TestDynamicObject newDynamicObject = JsonConvert.DeserializeObject<TestDynamicObject>(json);
d = newDynamicObject;
Assert.AreEqual(99.9, d.Decimal);
Assert.AreEqual(1, d.Int);
Assert.AreEqual(dynamicObject.ChildObject.Integer, d.ChildObject.Integer);
Assert.AreEqual(dynamicObject.ChildObject.Text, d.ChildObject.Text);
}
[Test]
public void sdfsdf()
{
ErrorSettingDynamicObject d = JsonConvert.DeserializeObject<ErrorSettingDynamicObject>("{'hi':5}");
}
[Test]
public void SerializeDynamicObjectWithObjectTracking()
{
dynamic o = new ExpandoObject();
o.Text = "Text!";
o.Integer = int.MaxValue;
o.DynamicChildObject = new DynamicChildObject
{
Integer = int.MinValue,
Text = "Child text!"
};
string json = JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
Console.WriteLine(json);
string dynamicChildObjectTypeName = ReflectionUtils.GetTypeName(typeof(DynamicChildObject), FormatterAssemblyStyle.Simple);
Assert.AreEqual(@"{
""$type"": ""System.Dynamic.ExpandoObject, System.Core"",
""Text"": ""Text!"",
""Integer"": 2147483647,
""DynamicChildObject"": {
""$type"": """ + dynamicChildObjectTypeName + @""",
""Text"": ""Child text!"",
""Integer"": -2147483648
}
}", json);
dynamic n = JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
Assert.IsInstanceOfType(typeof(ExpandoObject), n);
Assert.AreEqual("Text!", n.Text);
Assert.AreEqual(int.MaxValue, n.Integer);
Assert.IsInstanceOfType(typeof(DynamicChildObject), n.DynamicChildObject);
Assert.AreEqual("Child text!", n.DynamicChildObject.Text);
Assert.AreEqual(int.MinValue, n.DynamicChildObject.Integer);
}
}
}
#endif
@@ -23,7 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if !PocketPC && !SILVERLIGHT && !NET20
#if !(NET35 || NET20 || SILVERLIGHT)
using System;
using System.Collections.Generic;
using System.Data;
@@ -27,6 +27,7 @@ using System;
using System.Collections.Generic;
#if !SILVERLIGHT && !PocketPC && !NET20
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;
using System.Web.Script.Serialization;
#endif
using System.Text;
@@ -52,6 +53,10 @@ using System.Reflection;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using System.Linq.Expressions;
#endif
#if !(NET35 || NET20 || SILVERLIGHT)
using System.Dynamic;
#endif
namespace Newtonsoft.Json.Tests.Serialization
@@ -3019,7 +3024,7 @@ keyword such as type of business.""
PosDouble p = (PosDouble)value;
if (p != null)
writer.WriteRawValue(String.Format("new PosD({0},{1})", p.X, p.Y));
writer.WriteRawValue(String.Format(CultureInfo.InvariantCulture, "new PosD({0},{1})", p.X, p.Y));
else
writer.WriteNull();
}
@@ -3573,5 +3578,45 @@ keyword such as type of business.""
""event"": ""derived""
}", json);
}
#if !(NET35 || NET20 || SILVERLIGHT)
[Test]
public void SerializeExpandoObject()
{
dynamic expando = new ExpandoObject();
expando.Int = 1;
expando.Decimal = 99.9d;
expando.Complex = new ExpandoObject();
expando.Complex.String = "I am a string";
expando.Complex.DateTime = new DateTime(2000, 12, 20, 18, 55, 0, DateTimeKind.Utc);
string json = JsonConvert.SerializeObject(expando, Formatting.Indented);
Assert.AreEqual(@"{
""Int"": 1,
""Decimal"": 99.9,
""Complex"": {
""String"": ""I am a string"",
""DateTime"": ""\/Date(977338500000)\/""
}
}", json);
IDictionary<string, object> newExpando = JsonConvert.DeserializeObject<ExpandoObject>(json);
Assert.IsInstanceOfType(typeof(long), newExpando["Int"]);
Assert.AreEqual(expando.Int, newExpando["Int"]);
Assert.IsInstanceOfType(typeof(double), newExpando["Decimal"]);
Assert.AreEqual(expando.Decimal, newExpando["Decimal"]);
Assert.IsInstanceOfType(typeof(JObject), newExpando["Complex"]);
JObject o = (JObject)newExpando["Complex"];
Assert.IsInstanceOfType(typeof(string), ((JValue)o["String"]).Value);
Assert.AreEqual(expando.Complex.String, (string)o["String"]);
Assert.IsInstanceOfType(typeof(DateTime), ((JValue)o["DateTime"]).Value);
Assert.AreEqual(expando.Complex.DateTime, (DateTime)o["DateTime"]);
}
#endif
}
}
@@ -195,7 +195,7 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(new DateTime(2000, 12, 1, 0, 0, 0, DateTimeKind.Utc), c[2]);
Assert.AreEqual(3, errors.Count);
#if !(NET20 || SILVERLIGHT)
#if !(NET20 || NET35 || SILVERLIGHT)
Assert.AreEqual("The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.", errors[0]);
#else
Assert.AreEqual("The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.", errors[0]);