-Added ToObject to JToken for deserializing LINQ to JSON objects to a .NET object

-Added support for Guid, TimeSpan and Uri to LINQ to JSON
This commit is contained in:
JamesNK
2011-08-05 08:38:47 +00:00
parent d8b32dfdc4
commit 7d3ce867da
15 changed files with 570 additions and 8 deletions
@@ -31,6 +31,7 @@ using System.Text;
using NUnit.Framework;
using Newtonsoft.Json.Bson;
using System.IO;
using Newtonsoft.Json.Tests.Serialization;
using Newtonsoft.Json.Utilities;
using Newtonsoft.Json.Linq;
@@ -1164,5 +1165,54 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
public void UriGuidTimeSpanTestClassEmptyTest()
{
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass();
var memoryStream = new MemoryStream();
var bsonWriter = new BsonWriter(memoryStream);
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(bsonWriter, c1);
bsonWriter.Flush();
memoryStream.Position = 0;
var bsonReader = new BsonReader(memoryStream);
UriGuidTimeSpanTestClass c2 = serializer.Deserialize<UriGuidTimeSpanTestClass>(bsonReader);
Assert.AreEqual(c1.Guid, c2.Guid);
Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
Assert.AreEqual(c1.Uri, c2.Uri);
}
public void UriGuidTimeSpanTestClassValuesTest()
{
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass
{
Guid = new Guid("1924129C-F7E0-40F3-9607-9939C531395A"),
NullableGuid = new Guid("9E9F3ADF-E017-4F72-91E0-617EBE85967D"),
TimeSpan = TimeSpan.FromDays(1),
NullableTimeSpan = TimeSpan.FromHours(1),
Uri = new Uri("http://testuri.com")
};
var memoryStream = new MemoryStream();
var bsonWriter = new BsonWriter(memoryStream);
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(bsonWriter, c1);
bsonWriter.Flush();
memoryStream.Position = 0;
var bsonReader = new BsonReader(memoryStream);
UriGuidTimeSpanTestClass c2 = serializer.Deserialize<UriGuidTimeSpanTestClass>(bsonReader);
Assert.AreEqual(c1.Guid, c2.Guid);
Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
Assert.AreEqual(c1.Uri, c2.Uri);
}
}
}
+44 -3
View File
@@ -118,7 +118,10 @@ namespace Newtonsoft.Json.Tests.Linq
new JProperty("DateTime", new JValue(new DateTime(2000, 12, 29, 23, 51, 10, DateTimeKind.Utc))),
new JProperty("Boolean", new JValue(true)),
new JProperty("String", new JValue("A string lol!")),
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!")))
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!"))),
new JProperty("Uri", new Uri("http://json.codeplex.com/")),
new JProperty("Guid", new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF")),
new JProperty("TimeSpan", TimeSpan.FromDays(1))
);
dynamic d = o;
@@ -166,6 +169,27 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(d.Bytes == d.Bytes);
Assert.IsTrue(d.Bytes == Encoding.UTF8.GetBytes("A string lol!"));
Assert.IsTrue(d.Bytes == new JValue(Encoding.UTF8.GetBytes("A string lol!")));
Assert.IsTrue(d.Uri == d.Uri);
Assert.IsTrue(d.Uri == new Uri("http://json.codeplex.com/"));
Assert.IsTrue(d.Uri > new Uri("http://abc.org/"));
Assert.IsTrue(d.Uri >= new Uri("http://abc.com/"));
Assert.IsTrue(d.Uri > null);
Assert.IsTrue(d.Uri >= null);
Assert.IsTrue(d.Guid == d.Guid);
Assert.IsTrue(d.Guid == new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF"));
Assert.IsTrue(d.Guid > new Guid("AAAAAAAA-0D80-44F2-BF34-4654156FA7AF"));
Assert.IsTrue(d.Guid >= new Guid("AAAAAAAA-0D80-44F2-BF34-4654156FA7AF"));
Assert.IsTrue(d.Guid > null);
Assert.IsTrue(d.Guid >= null);
Assert.IsTrue(d.TimeSpan == d.TimeSpan);
Assert.IsTrue(d.TimeSpan == TimeSpan.FromDays(1));
Assert.IsTrue(d.TimeSpan > TimeSpan.FromHours(1));
Assert.IsTrue(d.TimeSpan >= TimeSpan.FromHours(1));
Assert.IsTrue(d.TimeSpan > null);
Assert.IsTrue(d.TimeSpan >= null);
}
[Test]
@@ -179,7 +203,10 @@ namespace Newtonsoft.Json.Tests.Linq
new JProperty("DateTime", new JValue(new DateTime(2000, 12, 29, 23, 51, 10, DateTimeKind.Utc))),
new JProperty("Boolean", new JValue(true)),
new JProperty("String", new JValue("A string lol!")),
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!")))
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!"))),
new JProperty("Uri", new Uri("http://json.codeplex.com/")),
new JProperty("Guid", new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF")),
new JProperty("TimeSpan", TimeSpan.FromDays(1))
);
dynamic d = o;
@@ -454,7 +481,10 @@ namespace Newtonsoft.Json.Tests.Linq
new JProperty("DateTime", new JValue(new DateTime(2000, 12, 29, 23, 51, 10, DateTimeKind.Utc))),
new JProperty("Boolean", new JValue(true)),
new JProperty("String", new JValue("A string lol!")),
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!")))
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!"))),
new JProperty("Uri", new Uri("http://json.codeplex.com/")),
new JProperty("Guid", new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF")),
new JProperty("TimeSpan", TimeSpan.FromDays(1))
);
dynamic d = o;
@@ -466,6 +496,9 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual("True", d.Boolean.ToString());
Assert.AreEqual("A string lol!", d.String.ToString());
Assert.AreEqual("System.Byte[]", d.Bytes.ToString());
Assert.AreEqual("http://json.codeplex.com/", d.Uri.ToString());
Assert.AreEqual("ea27fe1d-0d80-44f2-bf34-4654156fa7af", d.Guid.ToString());
Assert.AreEqual("1.00:00:00", d.TimeSpan.ToString());
}
[Test]
@@ -539,6 +572,14 @@ namespace Newtonsoft.Json.Tests.Linq
AssertValueConverted<ushort>(ushort.MinValue);
AssertValueConverted<ushort?>(ushort.MinValue);
AssertValueConverted<ushort?>(null);
AssertValueConverted<TimeSpan>(TimeSpan.FromDays(1));
AssertValueConverted<TimeSpan?>(TimeSpan.FromDays(1));
AssertValueConverted<TimeSpan?>(null);
AssertValueConverted<Guid>(new Guid("60304274-CD13-4060-B38C-057C8557AB54"));
AssertValueConverted<Guid?>(new Guid("60304274-CD13-4060-B38C-057C8557AB54"));
AssertValueConverted<Guid?>(null);
AssertValueConverted<Uri>(new Uri("http://json.codeplex.com/"));
AssertValueConverted<Uri>(null);
}
private static void AssertValueConverted<T>(object value)
@@ -1591,5 +1591,32 @@ Parameter name: arrayIndex")]
Assert.AreEqual(false, prop4.ShouldSerializeValue(o));
}
#endif
[Test]
public void FromObjectTimeSpan()
{
JValue v = (JValue)JToken.FromObject(TimeSpan.FromDays(1));
Assert.AreEqual(v.Value, TimeSpan.FromDays(1));
Assert.AreEqual("1.00:00:00", v.ToString());
}
[Test]
public void FromObjectUri()
{
JValue v = (JValue)JToken.FromObject(new Uri("http://www.stuff.co.nz"));
Assert.AreEqual(v.Value, new Uri("http://www.stuff.co.nz"));
Assert.AreEqual("http://www.stuff.co.nz/", v.ToString());
}
[Test]
public void FromObjectGuid()
{
JValue v = (JValue)JToken.FromObject(new Guid("9065ACF3-C820-467D-BE50-8D4664BEAF35"));
Assert.AreEqual(v.Value, new Guid("9065ACF3-C820-467D-BE50-8D4664BEAF35"));
Assert.AreEqual("9065acf3-c820-467d-be50-8d4664beaf35", v.ToString());
}
}
}
@@ -71,6 +71,19 @@ namespace Newtonsoft.Json.Tests.Linq
v.Value = StringComparison.OrdinalIgnoreCase;
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, v.Value);
Assert.AreEqual(JTokenType.Integer, v.Type);
v.Value = new Uri("http://json.codeplex.com/");
Assert.AreEqual(new Uri("http://json.codeplex.com/"), v.Value);
Assert.AreEqual(JTokenType.Uri, v.Type);
v.Value = TimeSpan.FromDays(1);
Assert.AreEqual(TimeSpan.FromDays(1), v.Value);
Assert.AreEqual(JTokenType.TimeSpan, v.Type);
Guid g = Guid.NewGuid();
v.Value = g;
Assert.AreEqual(g, v.Value);
Assert.AreEqual(JTokenType.Guid, v.Type);
}
[Test]
@@ -115,6 +128,15 @@ namespace Newtonsoft.Json.Tests.Linq
v = new JValue(new DateTime(2000, 12, 12, 20, 59, 59, DateTimeKind.Utc), JTokenType.Date);
Assert.AreEqual("12/12/2000 20:59:59", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(new Uri("http://json.codeplex.com/"));
Assert.AreEqual("http://json.codeplex.com/", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(TimeSpan.FromDays(1));
Assert.AreEqual("1.00:00:00", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(new Guid("B282ADE7-C520-496C-A448-4084F6803DE5"));
Assert.AreEqual("b282ade7-c520-496c-a448-4084f6803de5", v.ToString(null, CultureInfo.InvariantCulture));
}
[Test]
@@ -33,6 +33,7 @@ using Newtonsoft.Json.Linq;
using System.Xml;
using System.IO;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Tests.Serialization;
using Newtonsoft.Json.Tests.TestObjects;
namespace Newtonsoft.Json.Tests.Linq
@@ -782,5 +783,54 @@ keyword such as type of business.""
},
o.Children()["item"].Children()["title"].Values<string>().ToArray());
}
public void UriGuidTimeSpanTestClassEmptyTest()
{
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass();
JObject o = JObject.FromObject(c1);
Assert.AreEqual(@"{
""Guid"": ""00000000-0000-0000-0000-000000000000"",
""NullableGuid"": null,
""TimeSpan"": ""00:00:00"",
""NullableTimeSpan"": null,
""Uri"": null
}", o.ToString());
UriGuidTimeSpanTestClass c2 = o.ToObject<UriGuidTimeSpanTestClass>();
Assert.AreEqual(c1.Guid, c2.Guid);
Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
Assert.AreEqual(c1.Uri, c2.Uri);
}
public void UriGuidTimeSpanTestClassValuesTest()
{
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass
{
Guid = new Guid("1924129C-F7E0-40F3-9607-9939C531395A"),
NullableGuid = new Guid("9E9F3ADF-E017-4F72-91E0-617EBE85967D"),
TimeSpan = TimeSpan.FromDays(1),
NullableTimeSpan = TimeSpan.FromHours(1),
Uri = new Uri("http://testuri.com")
};
JObject o = JObject.FromObject(c1);
Assert.AreEqual(@"{
""Guid"": ""1924129c-f7e0-40f3-9607-9939c531395a"",
""NullableGuid"": ""9e9f3adf-e017-4f72-91e0-617ebe85967d"",
""TimeSpan"": ""1.00:00:00"",
""NullableTimeSpan"": ""01:00:00"",
""Uri"": ""http://testuri.com/""
}", o.ToString());
UriGuidTimeSpanTestClass c2 = o.ToObject<UriGuidTimeSpanTestClass>();
Assert.AreEqual(c1.Guid, c2.Guid);
Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
Assert.AreEqual(c1.Uri, c2.Uri);
}
}
}
@@ -4795,5 +4795,63 @@ keyword such as type of business.""
IList list = JsonConvert.DeserializeObject<IList>("['1', 'two', 'III']");
Assert.AreEqual(3, list.Count);
}
public void UriGuidTimeSpanTestClassEmptyTest()
{
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass();
string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
Assert.AreEqual(@"{
""Guid"": ""00000000-0000-0000-0000-000000000000"",
""NullableGuid"": null,
""TimeSpan"": ""00:00:00"",
""NullableTimeSpan"": null,
""Uri"": null
}", json);
UriGuidTimeSpanTestClass c2 = JsonConvert.DeserializeObject<UriGuidTimeSpanTestClass>(json);
Assert.AreEqual(c1.Guid, c2.Guid);
Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
Assert.AreEqual(c1.Uri, c2.Uri);
}
public void UriGuidTimeSpanTestClassValuesTest()
{
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass
{
Guid = new Guid("1924129C-F7E0-40F3-9607-9939C531395A"),
NullableGuid = new Guid("9E9F3ADF-E017-4F72-91E0-617EBE85967D"),
TimeSpan = TimeSpan.FromDays(1),
NullableTimeSpan = TimeSpan.FromHours(1),
Uri = new Uri("http://testuri.com")
};
string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
Assert.AreEqual(@"{
""Guid"": ""1924129c-f7e0-40f3-9607-9939c531395a"",
""NullableGuid"": ""9e9f3adf-e017-4f72-91e0-617ebe85967d"",
""TimeSpan"": ""1.00:00:00"",
""NullableTimeSpan"": ""01:00:00"",
""Uri"": ""http://testuri.com/""
}", json);
UriGuidTimeSpanTestClass c2 = JsonConvert.DeserializeObject<UriGuidTimeSpanTestClass>(json);
Assert.AreEqual(c1.Guid, c2.Guid);
Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
Assert.AreEqual(c1.Uri, c2.Uri);
}
}
public class UriGuidTimeSpanTestClass
{
public Guid Guid { get; set; }
public Guid? NullableGuid { get; set; }
public TimeSpan TimeSpan { get; set; }
public TimeSpan? NullableTimeSpan { get; set; }
public Uri Uri { get; set; }
}
}