Browse Source
Consolidated collection serializers in CollectionSerializers.cs and CollectionSerializersGeneric.cs (and similarly for the unit tests). Added unit tests for more generic collection types.
pull/28/head
Consolidated collection serializers in CollectionSerializers.cs and CollectionSerializersGeneric.cs (and similarly for the unit tests). Added unit tests for more generic collection types.
pull/28/head

10 changed files with 279 additions and 623 deletions
-
6Bson/Bson.csproj
-
117Bson/DefaultSerializer/Serializers/CollectionSerializers.cs
-
96Bson/DefaultSerializer/Serializers/CollectionSerializersGeneric.cs
-
103Bson/DefaultSerializer/Serializers/EnumerableSerializer.cs
-
96Bson/DefaultSerializer/Serializers/EnumerableSerializerGeneric.cs
-
6BsonUnitTests/BsonUnitTests.csproj
-
65BsonUnitTests/DefaultSerializer/Serializers/CollectionSerializerGenericTests.cs
-
87BsonUnitTests/DefaultSerializer/Serializers/CollectionSerializerTests.cs
-
163BsonUnitTests/DefaultSerializer/Serializers/QueueSerializerTests.cs
-
163BsonUnitTests/DefaultSerializer/Serializers/StackSerializerTests.cs
@ -1,103 +0,0 @@ |
|||
/* Copyright 2010 10gen Inc. |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.IO; |
|||
|
|||
using MongoDB.Bson.IO; |
|||
using MongoDB.Bson.Serialization; |
|||
|
|||
namespace MongoDB.Bson.DefaultSerializer { |
|||
public class EnumerableSerializer : BsonBaseSerializer { |
|||
#region private static fields
|
|||
private static EnumerableSerializer singleton = new EnumerableSerializer(); |
|||
#endregion
|
|||
|
|||
#region constructors
|
|||
private EnumerableSerializer() { |
|||
} |
|||
#endregion
|
|||
|
|||
#region public static properties
|
|||
public static EnumerableSerializer Singleton { |
|||
get { return singleton; } |
|||
} |
|||
#endregion
|
|||
|
|||
#region public static methods
|
|||
public static void RegisterSerializers() { |
|||
BsonSerializer.RegisterSerializer(typeof(ArrayList), singleton); |
|||
BsonSerializer.RegisterSerializer(typeof(ICollection), singleton); |
|||
BsonSerializer.RegisterSerializer(typeof(IEnumerable), singleton); |
|||
BsonSerializer.RegisterSerializer(typeof(IList), singleton); |
|||
} |
|||
#endregion
|
|||
|
|||
#region public methods
|
|||
public override object DeserializeElement( |
|||
BsonReader bsonReader, |
|||
Type nominalType, |
|||
out string name |
|||
) { |
|||
var bsonType = bsonReader.PeekBsonType(); |
|||
if (bsonType == BsonType.Null) { |
|||
bsonReader.ReadNull(out name); |
|||
return null; |
|||
} else if (bsonType == BsonType.Array) { |
|||
bsonReader.ReadArrayName(out name); |
|||
bsonReader.ReadStartDocument(); |
|||
var list = new ArrayList(); |
|||
while (bsonReader.HasElement()) { |
|||
var elementType = BsonClassMapSerializer.GetActualElementType(bsonReader, typeof(object)); |
|||
var serializer = BsonSerializer.LookupSerializer(elementType); |
|||
string elementName; // elementNames are ignored on input
|
|||
var element = serializer.DeserializeElement(bsonReader, typeof(object), out elementName); |
|||
list.Add(element); |
|||
} |
|||
bsonReader.ReadEndDocument(); |
|||
return list; |
|||
} else { |
|||
var message = string.Format("Can't deserialize a {0} from BsonType {1}", nominalType.FullName, bsonType); |
|||
throw new FileFormatException(message); |
|||
} |
|||
} |
|||
|
|||
public override void SerializeElement( |
|||
BsonWriter bsonWriter, |
|||
Type nominalType, |
|||
string name, |
|||
object value |
|||
) { |
|||
if (value == null) { |
|||
bsonWriter.WriteNull(name); |
|||
} else { |
|||
bsonWriter.WriteArrayName(name); |
|||
bsonWriter.WriteStartDocument(); |
|||
int index = 0; |
|||
foreach (var element in (IEnumerable) value) { |
|||
var elementName = index.ToString(); |
|||
BsonSerializer.SerializeElement(bsonWriter, typeof(object), elementName, element); |
|||
index++; |
|||
} |
|||
bsonWriter.WriteEndDocument(); |
|||
} |
|||
} |
|||
#endregion
|
|||
} |
|||
} |
@ -1,96 +0,0 @@ |
|||
/* Copyright 2010 10gen Inc. |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.IO; |
|||
|
|||
using MongoDB.Bson.IO; |
|||
using MongoDB.Bson.Serialization; |
|||
|
|||
namespace MongoDB.Bson.DefaultSerializer { |
|||
public static class EnumerableSerializerRegistration { |
|||
#region public static methods
|
|||
public static void RegisterGenericSerializerDefinitions() { |
|||
BsonSerializer.RegisterGenericSerializerDefinition(typeof(HashSet<>), typeof(EnumerableSerializer<>)); |
|||
BsonSerializer.RegisterGenericSerializerDefinition(typeof(List<>), typeof(EnumerableSerializer<>)); |
|||
BsonSerializer.RegisterGenericSerializerDefinition(typeof(ICollection<>), typeof(EnumerableSerializer<>)); |
|||
BsonSerializer.RegisterGenericSerializerDefinition(typeof(IEnumerable<>), typeof(EnumerableSerializer<>)); |
|||
BsonSerializer.RegisterGenericSerializerDefinition(typeof(IList<>), typeof(EnumerableSerializer<>)); |
|||
} |
|||
#endregion
|
|||
} |
|||
|
|||
public class EnumerableSerializer<T> : BsonBaseSerializer { |
|||
#region constructors
|
|||
public EnumerableSerializer() { |
|||
} |
|||
#endregion
|
|||
|
|||
#region public methods
|
|||
public override object DeserializeElement( |
|||
BsonReader bsonReader, |
|||
Type nominalType, |
|||
out string name |
|||
) { |
|||
var bsonType = bsonReader.PeekBsonType(); |
|||
if (bsonType == BsonType.Null) { |
|||
bsonReader.ReadNull(out name); |
|||
return null; |
|||
} else if (bsonType == BsonType.Array) { |
|||
bsonReader.ReadArrayName(out name); |
|||
bsonReader.ReadStartDocument(); |
|||
var list = (nominalType.IsInterface) ? new List<T>() : (ICollection<T>) Activator.CreateInstance(nominalType); |
|||
while (bsonReader.HasElement()) { |
|||
var elementType = BsonClassMapSerializer.GetActualElementType(bsonReader, typeof(T)); |
|||
var serializer = BsonSerializer.LookupSerializer(elementType); |
|||
string elementName; // elementNames are ignored on input
|
|||
var element = (T) serializer.DeserializeElement(bsonReader, typeof(T), out elementName); |
|||
list.Add(element); |
|||
} |
|||
bsonReader.ReadEndDocument(); |
|||
return list; |
|||
} else { |
|||
var message = string.Format("Can't deserialize a {0} from BsonType {1}", nominalType.FullName, bsonType); |
|||
throw new FileFormatException(message); |
|||
} |
|||
} |
|||
|
|||
public override void SerializeElement( |
|||
BsonWriter bsonWriter, |
|||
Type nominalType, |
|||
string name, |
|||
object value |
|||
) { |
|||
if (value == null) { |
|||
bsonWriter.WriteNull(name); |
|||
} else { |
|||
bsonWriter.WriteArrayName(name); |
|||
bsonWriter.WriteStartDocument(); |
|||
int index = 0; |
|||
foreach (var element in (IEnumerable<T>) value) { |
|||
var elementName = index.ToString(); |
|||
BsonSerializer.SerializeElement(bsonWriter, typeof(T), elementName, element); |
|||
index++; |
|||
} |
|||
bsonWriter.WriteEndDocument(); |
|||
} |
|||
} |
|||
#endregion
|
|||
} |
|||
} |
@ -1,163 +0,0 @@ |
|||
/* Copyright 2010 10gen Inc. |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.Collections; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using NUnit.Framework; |
|||
|
|||
using MongoDB.Bson; |
|||
using MongoDB.Bson.DefaultSerializer; |
|||
using MongoDB.Bson.Serialization; |
|||
|
|||
namespace MongoDB.BsonUnitTests.DefaultSerializer.QueueSerializer { |
|||
[BsonDiscriminator("QueueSerializer.C")] // "C" is an ambiguous discriminator when nominalType is System.Object
|
|||
public class C { |
|||
public string P { get; set; } |
|||
} |
|||
|
|||
[TestFixture] |
|||
public class QueueSerializerTests { |
|||
public class T { |
|||
public Queue Q { get; set; } |
|||
} |
|||
|
|||
[Test] |
|||
public void TestNull() { |
|||
var obj = new T { Q = null }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'Q' : null }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsNull(rehydrated.Q); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestEmpty() { |
|||
var obj = new T { Q = new Queue() }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'Q' : [] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Queue>(rehydrated.Q); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneC() { |
|||
var obj = new T { Q = new Queue(new[] { new C { P = "x" } }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'Q' : [{ '_t' : 'QueueSerializer.C', 'P' : 'x' }] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Queue>(rehydrated.Q); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneInt() { |
|||
var obj = new T { Q = new Queue(new[] { 1 }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'Q' : [1] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Queue>(rehydrated.Q); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneString() { |
|||
var obj = new T { Q = new Queue(new[] { "x" }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'Q' : ['x'] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Queue>(rehydrated.Q); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoCs() { |
|||
var obj = new T { Q = new Queue(new[] { new C { P = "x" }, new C { P = "y" } }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'Q' : [{ '_t' : 'QueueSerializer.C', 'P' : 'x' }, { '_t' : 'QueueSerializer.C', 'P' : 'y' }] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Queue>(rehydrated.Q); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoInts() { |
|||
var obj = new T { Q = new Queue(new[] { 1, 2 }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'Q' : [1, 2] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Queue>(rehydrated.Q); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoStrings() { |
|||
var obj = new T { Q = new Queue(new[] { "x", "y" }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'Q' : ['x', 'y'] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Queue>(rehydrated.Q); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestMixedPrimitiveTypes() { |
|||
var dateTime = DateTime.SpecifyKind(new DateTime(2010, 1, 1, 11, 22, 33), DateTimeKind.Utc); |
|||
var millis = (long) ((dateTime - BsonConstants.UnixEpoch).TotalMilliseconds); |
|||
var guid = Guid.Empty; |
|||
var objectId = ObjectId.Empty; |
|||
var obj = new T { Q = new Queue(new object[] { true, dateTime, 1.5, 1, 2L, guid, objectId, "x" }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'Q' : [true, #Date, 1.5, 1, 2, #Guid, #ObjectId, 'x'] }"; |
|||
expected = expected.Replace("#Date", "{ '$date' : #ms }".Replace("#ms", millis.ToString())); |
|||
expected = expected.Replace("#Guid", "{ '$binary' : 'AAAAAAAAAAAAAAAAAAAAAA==', '$type' : '03' }"); |
|||
expected = expected.Replace("#ObjectId", "{ '$oid' : '000000000000000000000000' }"); |
|||
expected = expected.Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Queue>(rehydrated.Q); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
} |
|||
} |
@ -1,163 +0,0 @@ |
|||
/* Copyright 2010 10gen Inc. |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.Collections; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using NUnit.Framework; |
|||
|
|||
using MongoDB.Bson; |
|||
using MongoDB.Bson.DefaultSerializer; |
|||
using MongoDB.Bson.Serialization; |
|||
|
|||
namespace MongoDB.BsonUnitTests.DefaultSerializer.StackSerializer { |
|||
[BsonDiscriminator("StackSerializer.C")] // "C" is an ambiguous discriminator when nominalType is System.Object
|
|||
public class C { |
|||
public string P { get; set; } |
|||
} |
|||
|
|||
[TestFixture] |
|||
public class StackSerializerTests { |
|||
public class T { |
|||
public Stack S { get; set; } |
|||
} |
|||
|
|||
[Test] |
|||
public void TestNull() { |
|||
var obj = new T { S = null }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'S' : null }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsNull(rehydrated.S); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestEmpty() { |
|||
var obj = new T { S = new Stack() }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'S' : [] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Stack>(rehydrated.S); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneC() { |
|||
var obj = new T { S = new Stack(new[] { new C { P = "x" } }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'S' : [{ '_t' : 'StackSerializer.C', 'P' : 'x' }] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Stack>(rehydrated.S); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneInt() { |
|||
var obj = new T { S = new Stack(new[] { 1 }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'S' : [1] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Stack>(rehydrated.S); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneString() { |
|||
var obj = new T { S = new Stack(new[] { "x" }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'S' : ['x'] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Stack>(rehydrated.S); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoCs() { |
|||
var obj = new T { S = new Stack(new[] { new C { P = "x" }, new C { P = "y" } }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'S' : [{ '_t' : 'StackSerializer.C', 'P' : 'x' }, { '_t' : 'StackSerializer.C', 'P' : 'y' }] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Stack>(rehydrated.S); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoInts() { |
|||
var obj = new T { S = new Stack(new[] { 1, 2 }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'S' : [1, 2] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Stack>(rehydrated.S); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoStrings() { |
|||
var obj = new T { S = new Stack(new[] { "x", "y" }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'S' : ['x', 'y'] }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Stack>(rehydrated.S); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestMixedPrimitiveTypes() { |
|||
var dateTime = DateTime.SpecifyKind(new DateTime(2010, 1, 1, 11, 22, 33), DateTimeKind.Utc); |
|||
var millis = (long) ((dateTime - BsonConstants.UnixEpoch).TotalMilliseconds); |
|||
var guid = Guid.Empty; |
|||
var objectId = ObjectId.Empty; |
|||
var obj = new T { S = new Stack(new object[] { true, dateTime, 1.5, 1, 2L, guid, objectId, "x" }) }; |
|||
var json = obj.ToJson(); |
|||
var expected = "{ 'S' : [true, #Date, 1.5, 1, 2, #Guid, #ObjectId, 'x'] }"; |
|||
expected = expected.Replace("#Date", "{ '$date' : #ms }".Replace("#ms", millis.ToString())); |
|||
expected = expected.Replace("#Guid", "{ '$binary' : 'AAAAAAAAAAAAAAAAAAAAAA==', '$type' : '03' }"); |
|||
expected = expected.Replace("#ObjectId", "{ '$oid' : '000000000000000000000000' }"); |
|||
expected = expected.Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
|
|||
var bson = obj.ToBson(); |
|||
var rehydrated = BsonSerializer.DeserializeDocument<T>(bson); |
|||
Assert.IsInstanceOf<Stack>(rehydrated.S); |
|||
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue