Ejemplo n.º 1
0
        public void Deserialize_Object()
        {
            var doc = new BsonDocument()
            {
                ["x"] = 1
            };

            var result = _mapper.Deserialize(typeof(object), doc);

            Assert.Equal(typeof(Dictionary <string, object>), result.GetType());

            //! used to be empty
            var dic = (Dictionary <string, object>)result;

            Assert.Single(dic);
            Assert.Equal(1, dic["x"]);
        }
Ejemplo n.º 2
0
        public void Enum_Array_Test()
        {
            var mapper = new BsonMapper();

            mapper.EnumAsInteger = false;

            var array = new CustomerType[] { CustomerType.Potential, CustomerType.Loyal };

            var serialized1   = mapper.Serialize(array);
            var deserialized1 = mapper.Deserialize <CustomerType[]>(serialized1);

            deserialized1.Should().Equal(array);

            mapper.EnumAsInteger = true;

            var serialized2   = mapper.Serialize(array);
            var deserialized2 = mapper.Deserialize <CustomerType[]>(serialized1);

            deserialized2.Should().Equal(array);
        }
Ejemplo n.º 3
0
        public void RegisterCustomSerializers(BsonMapper mapper)
        {
            //this implementation is only for testing as the simulator doesn't save it's state
            BsonMapper defaultMapper = new BsonMapper();

            BsonValue OrderToBson(Order order)
            {
                return(defaultMapper.Serialize(typeof(IOrder), order));
            }

            Order BsonToOrder(BsonValue value)
            {
                var order = DeserializedOrders.FirstOrDefault(o => o.Id == value["_id"].AsString);

                if (order == null)
                {
                    DeserializedOrders.Add(order = defaultMapper.Deserialize <Order>(value));
                }
                return(order);
            }

            BsonValue SerializeTrade(Trade trade)
            {
                return(defaultMapper.Serialize(typeof(ITrade), trade));
            }

            Trade DeserializeTrade(BsonValue value)
            {
                var trade = DeserializedTrades.FirstOrDefault(o => o.Id == value["_id"].AsString);

                if (trade == null)
                {
                    DeserializedTrades.Add(trade = defaultMapper.Deserialize <Trade>(value));
                }
                return(trade);
            }

            mapper.RegisterType <Order>(OrderToBson, BsonToOrder);
            mapper.RegisterType <Trade>(SerializeTrade, DeserializeTrade);
        }
Ejemplo n.º 4
0
        public void ShouldDeserializeCollectionClass()
        {
            var items    = CreateCollection();
            var document = _mapper.ToDocument(items);


            var lst = (ItemCollection)_mapper.Deserialize(typeof(ItemCollection), document);

            Assert.AreEqual("MyCollection", lst.MyItemCollectionName);
            Assert.AreEqual(lst.Count, 1);
            Assert.IsInstanceOfType(lst[0], typeof(Item));
            Assert.AreEqual(lst[0].MyItemName, "MyItem");
        }
Ejemplo n.º 5
0
        public void Struct_Test()
        {
            var mapper = new BsonMapper();

            mapper.IncludeFields = true;

            var obj = new ContainerValue
            {
                Id     = Guid.NewGuid(),
                Struct = new StructValue
                {
                    Property = "PropertyValue"
                }
            };

            var doc  = mapper.ToDocument(obj);
            var nobj = mapper.Deserialize <ContainerValue>(doc);

            Assert.AreEqual(obj.Id, nobj.Id);
            Assert.AreEqual(obj.Struct.Property, nobj.Struct.Property);
        }