-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);
}
}
}