Ejemplo n.º 1
0
 /// <summary>Create an instance bound to a specific type</summary>
 /// <param name="classType">class to marshall</param>
 public JsonSerDeser(Type classType)
 {
     Preconditions.CheckArgument(classType != null, "null classType");
     this.classType = classType;
     this.mapper    = new ObjectMapper();
     mapper.Configure(DeserializationConfig.Feature.FailOnUnknownProperties, false);
 }
Ejemplo n.º 2
0
 /// <summary>Convert an instance to a JSON string</summary>
 /// <param name="instance">instance to convert</param>
 /// <returns>a JSON string description</returns>
 /// <exception cref="Org.Codehaus.Jackson.JsonParseException">parse problems</exception>
 /// <exception cref="Org.Codehaus.Jackson.Map.JsonMappingException">O/J mapping problems
 ///     </exception>
 /// <exception cref="System.IO.IOException"/>
 /// <exception cref="Org.Codehaus.Jackson.JsonGenerationException"/>
 public virtual string ToJson(T instance)
 {
     lock (this)
     {
         mapper.Configure(SerializationConfig.Feature.IndentOutput, true);
         return(mapper.WriteValueAsString(instance));
     }
 }
Ejemplo n.º 3
0
        public void CustomObjectMap()
        {
            ObjectMapper.Configure <CustomMapClass>(m =>
            {
                var token = SnapshotTokenizer.Tokenize(m.Value);
                return(token);
            });

            var itm = new CustomMapClass
            {
                Value = "some value",
                Index = 0
            };

            itm.MatchSnapshot();
        }
Ejemplo n.º 4
0
        public void CreateMapping()
        {
            // overwrite the configured mapping
            ObjectMapper.Configure <Map>(m =>
            {
                var snapshot = new Snapshot()
                               .Add(m.Value);
                return(snapshot);
            });

            var item = new Map {
                Value = "test"
            };

            item.MatchSnapshot();
        }
Ejemplo n.º 5
0
        public void SnapshotTokenizer_MapToToken_MergeMeta_CustomMapper()
        {
            ObjectMapper.Configure <TokenizerTestItem>(item =>
            {
                var snapshot = SnapshotTokenizer.Tokenize(new { id = item.Id });
                snapshot.SetMetadata(() => new
                {
                    Id = item.Id
                });

                return(snapshot);
            });

            var token = SnapshotTokenizer.MapToToken(new TokenizerTestItem {
                Id = 1
            });

            token.Metadata["Id"].Should().Be("1");
        }
Ejemplo n.º 6
0
        public void TestCustomCreator()
        {
            var container = new ObjectMapper();

            container.Configure <Role, RoleEntity>().CreateWith(source => new RoleEntity())
            .MapMember(target => target.LoweredRoleName, source => source.RoleName.ToLower());
            var role = new Role
            {
                RoleId      = Guid.NewGuid(),
                RoleName    = "Manager",
                Description = "Department or group manager"
            };
            var entity = container.Map <Role, RoleEntity>(role);

            Assert.Equal(entity.RoleId, role.RoleId);
            Assert.Equal(entity.RoleName, role.RoleName);
            Assert.Equal(entity.LoweredRoleName, role.RoleName.ToLower());
            Assert.Equal(entity.Description, role.Description);
        }
Ejemplo n.º 7
0
        public void TestHierarchyMap()
        {
            var container = new ObjectMapper();

            container.Configure <Order, OrderEntity>().WithOptions(MemberMapOptions.Hierarchy);
            var order = new Order
            {
                OrderId    = Guid.NewGuid(),
                CustomerId = Guid.NewGuid(),
                OrderCode  = "A001",
                Address    = "China",
                Items      =
                {
                    new OrderItem
                    {
                        OrderItemId = Guid.NewGuid(),
                        ProductId   = Guid.NewGuid(),
                        Quantity    = 2
                    }
                }
            };
            var entity = container.Map <Order, OrderEntity>(order);

            Assert.NotNull(entity);
            Assert.Equal(order.OrderId, entity.OrderId);
            Assert.Equal(order.CustomerId, entity.CustomerId);
            Assert.Equal(order.OrderCode, entity.OrderCode);
            Assert.Equal(order.Address, entity.Address);
            Assert.NotNull(entity.Items);
            Assert.Equal(1, entity.Items.Count);
            var item = entity.Items.First();

            Assert.NotNull(item);
            Assert.Equal(order.Items[0].OrderItemId, item.OrderItemId);
            Assert.Equal(order.Items[0].ProductId, item.ProductId);
            Assert.Equal(order.Items[0].Quantity, item.Quantity);
        }
Ejemplo n.º 8
0
        public void TestCustomMapper()
        {
            var container = new ObjectMapper();

            container.Configure <Role, RoleEntity>().MapWith((source, target) =>
            {
                target.RoleId          = source.RoleId;
                target.RoleName        = source.RoleName.ToUpper();
                target.LoweredRoleName = source.RoleName.ToLower();
                target.Description     = source.Description;
            });
            var role = new Role
            {
                RoleId      = Guid.NewGuid(),
                RoleName    = "Manager",
                Description = "Department or group manager"
            };
            var entity = container.Map <Role, RoleEntity>(role);

            Assert.Equal(entity.RoleId, role.RoleId);
            Assert.Equal(entity.RoleName, role.RoleName.ToUpper());
            Assert.Equal(entity.LoweredRoleName, role.RoleName.ToLower());
            Assert.Equal(entity.Description, role.Description);
        }