public void SerializationSpeedTest() { for (var i = 0; i < 5; i++) { var stopWatch = new Stopwatch(); stopWatch.Start(); for (var j = 0; j < 10000; j++) { var obj1 = new GeneralDTO { Title = null, ABoolean = true, AGuid = Guid.NewGuid(), AnInt = 1, Pi = 3.14, Nester = new GeneralDTO { Title = "Bob", AnInt = 42 } }; var obj1Bytes = BsonSerializer.Serialize(obj1); BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes); } stopWatch.Stop(); Console.WriteLine(stopWatch.ElapsedMilliseconds); } }
public void SerializationOfBytesIsNotLossy() { var bin = new List <byte>(); for (int i = 1; i < 1000; i++) { bin.AddRange(BitConverter.GetBytes(i)); } var obj1 = new GeneralDTO { Bytes = bin.ToArray() }; var obj2 = new GeneralDTO { Bytes = null }; var obj1Bytes = BsonSerializer.Serialize(obj1); var obj2Bytes = BsonSerializer.Serialize(obj2); var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes); var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes); Assert.True(obj1.Bytes.SequenceEqual(hydratedObj1.Bytes)); Assert.Equal(null, hydratedObj2.Bytes); }
public void SerializesAndDeserializesAFloat() { var o = new GeneralDTO { AFloat = 1.4f }; Assert.Equal(1.4f, BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(o)).AFloat); }
public void SerializingPocoGeneratesBytes() { var dummy = new GeneralDTO { Title = "Testing" }; Assert.NotEmpty(BsonSerializer.Serialize(dummy)); }
public void DoesntSerializeIgnoredProperties() { var o = new GeneralDTO { IgnoredProperty = 4 }; Assert.Equal(0, BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(o)).IgnoredProperty); }
public void SerializesAndDeserializesAComplexObject() { var obj1 = new GeneralDTO { Flags64 = Flags64.FlagOff, Flags32 = Flags32.FlagOn, Pi = 2d, AnInt = 3, Title = "telti", ABoolean = false, Strings = new[] { "a", "bb", "abc" }, Bytes = new byte[] { 1, 2, 3 }, AGuid = Guid.NewGuid(), ADateTime = new DateTime(2001, 4, 8, 10, 43, 23, 104), ARex = new Regex("it's over (9000)", RegexOptions.IgnoreCase) }; var nested = new GeneralDTO { Pi = 43d, Title = "little", ARex = new Regex("^over (9000)$") }; obj1.Nester = nested; var obj2 = new GeneralDTO(); var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(obj1)); var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(obj2)); Assert.Equal(obj1.Pi, hydratedObj1.Pi); Assert.Equal(obj1.AnInt, hydratedObj1.AnInt); Assert.Equal(obj1.Title, hydratedObj1.Title); Assert.Equal(obj1.ABoolean, hydratedObj1.ABoolean); Assert.Equal(obj1.Bytes, hydratedObj1.Bytes); Assert.Equal(obj1.AGuid, hydratedObj1.AGuid); Assert.Equal(obj1.ADateTime.Value.ToUniversalTime().Ticks, hydratedObj1.ADateTime.Value.ToUniversalTime().Ticks); Assert.Equal(obj1.Strings, hydratedObj1.Strings); Assert.Equal(obj1.Flags32, hydratedObj1.Flags32); Assert.Equal(obj1.Flags64, hydratedObj1.Flags64); Assert.Equal(obj1.Nester.Title, hydratedObj1.Nester.Title); Assert.Equal(obj1.Nester.Pi, hydratedObj1.Nester.Pi); Assert.Equal(obj1.ARex.ToString(), hydratedObj1.ARex.ToString()); Assert.Equal(obj1.ARex.Options, hydratedObj1.ARex.Options); Assert.Equal(obj2.Pi, hydratedObj2.Pi); Assert.Equal(obj2.AnInt, hydratedObj2.AnInt); Assert.Equal(obj2.Title, hydratedObj2.Title); Assert.Equal(obj2.ABoolean, hydratedObj2.ABoolean); Assert.Equal(obj2.Bytes, hydratedObj2.Bytes); Assert.Equal(obj2.AGuid, hydratedObj2.AGuid); Assert.Equal(obj2.ADateTime, hydratedObj2.ADateTime); Assert.Equal(obj2.Strings, hydratedObj2.Strings); Assert.Equal(obj2.Flags32, hydratedObj2.Flags32); Assert.Equal(obj2.Flags64, hydratedObj2.Flags64); Assert.Equal(obj2.Nester, hydratedObj2.Nester); Assert.Equal(obj2.ARex, hydratedObj2.ARex); }
public void RecursiveNestedTypesDontCauseInfiniteLoop() { var obj1 = new GeneralDTO { Title = "Hello World", Nester = new GeneralDTO { Title = "Bob", AnInt = 42 } }; var obj1Bytes = BsonSerializer.Serialize(obj1); BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes); }
public void SerializationOfIEnumerableTIsNotLossy() { var gto = new GeneralDTO { AnIEnumerable = new List <Person>() { new Person(), new Person() } }; var bytes = BsonSerializer.Serialize(gto); var gto2 = BsonDeserializer.Deserialize <GeneralDTO>(bytes); Assert.Equal(2, gto2.AnIEnumerable.Count()); }
public void SerializationOfEnumIsNotLossy() { var obj1 = new GeneralDTO { Flags32 = Flags32.FlagOn, Flags64 = Flags64.FlagOff }; var obj2 = new GeneralDTO(); var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(obj1)); var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(obj2)); Assert.Equal(obj1.Flags32, hydratedObj1.Flags32); Assert.Equal(null, hydratedObj2.Flags32); Assert.Equal(obj1.Flags64, hydratedObj1.Flags64); Assert.Equal(null, hydratedObj2.Flags64); }
public void SerializationOfNestedObjectsIsNotLossy() { var obj1 = new GeneralDTO { Title = "Hello World", Nester = new GeneralDTO { Title = "Bob", AnInt = 42 } }; var obj1Bytes = BsonSerializer.Serialize(obj1); var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes); Assert.Equal(obj1.Title, hydratedObj1.Title); Assert.Equal(obj1.Nester.Title, hydratedObj1.Nester.Title); Assert.Equal(obj1.Nester.AnInt, hydratedObj1.Nester.AnInt); }
public void SerializationOfScopedCodeIsNotLossy() { var obj1 = new GeneralDTO { Code = new ScopedCode { CodeString = "function(){return 'hello world!'}" } }; var scope = new Expando(); scope["$ns"] = "root"; obj1.Code.Scope = scope; var obj2 = BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(obj1)); Assert.Equal(obj1.Code.CodeString, obj2.Code.CodeString); Assert.Equal(((Expando)obj1.Code.Scope)["$ns"], ((Expando)obj2.Code.Scope)["$ns"]); }
public void SerializationOfBooleansAreNotLossy() { var obj1 = new GeneralDTO { ABoolean = true }; var obj2 = new GeneralDTO { ABoolean = null }; var obj1Bytes = BsonSerializer.Serialize(obj1); var obj2Bytes = BsonSerializer.Serialize(obj2); var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes); var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes); Assert.Equal(obj1.ABoolean, hydratedObj1.ABoolean); Assert.Equal(null, hydratedObj2.ABoolean); }
public void SerializationOfDoublesAreNotLossy() { var obj1 = new GeneralDTO { Pi = 3.1415927d }; var obj2 = new GeneralDTO { Pi = null }; var obj1Bytes = BsonSerializer.Serialize(obj1); var obj2Bytes = BsonSerializer.Serialize(obj2); var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes); var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes); Assert.Equal(obj1.Pi, hydratedObj1.Pi); Assert.Equal(null, hydratedObj2.Pi); }
public void SerializationOfGuidIsNotLossy() { var obj1 = new GeneralDTO { AGuid = Guid.NewGuid() }; var obj2 = new GeneralDTO { AGuid = null }; var obj1Bytes = BsonSerializer.Serialize(obj1); var obj2Bytes = BsonSerializer.Serialize(obj2); var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes); var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes); Assert.Equal(obj1.AGuid, hydratedObj1.AGuid); Assert.Equal(null, hydratedObj2.AGuid); }
public void SerializationOfStringsAreNotLossy() { var obj1 = new GeneralDTO { Title = null }; var obj2 = new GeneralDTO { Title = "Hello World" }; var obj1Bytes = BsonSerializer.Serialize(obj1); var obj2Bytes = BsonSerializer.Serialize(obj2); var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes); var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes); Assert.Equal(null, hydratedObj1.Title); Assert.Equal(obj2.Title, hydratedObj2.Title); }
public void SerializationOfIntsAreNotLossy() { var obj1 = new GeneralDTO { AnInt = 100 }; var obj2 = new GeneralDTO { AnInt = null }; var obj1Bytes = BsonSerializer.Serialize(obj1); var obj2Bytes = BsonSerializer.Serialize(obj2); var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes); var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes); Assert.Equal(obj1.AnInt, hydratedObj1.AnInt); Assert.Equal(null, hydratedObj2.AnInt); }
public void SerializationOfRegexIsNotLossy() { var obj1 = new GeneralDTO { ARex = new Regex("[0-9]{5}", RegexOptions.Multiline) }; var obj2 = new GeneralDTO { ARex = null }; var obj1Bytes = BsonSerializer.Serialize(obj1); var obj2Bytes = BsonSerializer.Serialize(obj2); var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes); var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes); Assert.Equal(obj1.ARex.ToString(), hydratedObj1.ARex.ToString()); Assert.Equal(obj1.ARex.Options, hydratedObj1.ARex.Options); Assert.Equal(null, hydratedObj2.ARex); //more tests would be useful for all the options. }
public void SerializationOfDatesHasMillisecondPrecision() { var obj1 = new GeneralDTO { ADateTime = null }; var obj2 = new GeneralDTO { ADateTime = DateTime.Now }; var obj1Bytes = BsonSerializer.Serialize(obj1); var obj2Bytes = BsonSerializer.Serialize(obj2); var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes); var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes); Assert.Equal(null, hydratedObj1.ADateTime); //Mongo stores dates as long, therefore, we have to use double->long rounding. Assert.Equal((long)((obj2.ADateTime.Value.ToUniversalTime() - DateTime.MinValue)).TotalMilliseconds, (long)(hydratedObj2.ADateTime.Value - DateTime.MinValue).TotalMilliseconds); }