Example #1
0
        public override object Deserialize(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            VoucherEntity data = new VoucherEntity();

            data.Name  = dictionary.GetValue("name", string.Empty);
            data.Items = JSONSerializerExecute.Deserialize <VoucherItemCollection>(dictionary["items"]);

            return(data);
        }
Example #2
0
        public override IDictionary <string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            Dictionary <string, object> dictionary = new Dictionary <string, object>();

            VoucherEntity data = (VoucherEntity)obj;

            dictionary.AddNonDefaultValue("name", data.Name);
            dictionary["items"] = data.Items;

            return(dictionary);
        }
Example #3
0
        public void VoucherWithoutItemCollectionConverterJsonTest()
        {
            VoucherEntity source = VoucherEntity.PrepareData();

            JSONSerializerExecute.RegisterConverter(typeof(VoucherConverter));

            string json = JSONSerializerExecute.SerializeWithType(source, true);

            Console.WriteLine(json);

            VoucherEntity deserialized = JSONSerializerExecute.DeserializeString <VoucherEntity>(json);

            //不校验CollectionName
            AssertVoucherEntity(source, deserialized, false);
        }
Example #4
0
        public void VoucherItemCollectionWithConverterJsonTest()
        {
            JSONSerializerExecute.RegisterConverter(typeof(VoucherConverter));
            JSONSerializerExecute.RegisterConverter(typeof(VoucherItemCollectionConverter));

            VoucherEntity source = VoucherEntity.PrepareData();

            string json = JSONSerializerExecute.SerializeWithType(source.Items, true);

            Console.WriteLine(json);

            JavaScriptSerializer serializer = JSONSerializerFactory.GetJavaScriptSerializer(typeof(VoucherItemCollection));

            VoucherItemCollection deserialized = JSONSerializerExecute.DeserializeString <VoucherItemCollection>(json);

            AssertVoucherItemCollection(source.Items, deserialized);
        }
Example #5
0
        public static VoucherEntity PrepareData()
        {
            VoucherEntity result = new VoucherEntity()
            {
                Name = "Voucher"
            };

            result.Items.Add(new VoucherItem()
            {
                Code = "1001", VoucherCode = "Voucher 1001", CreateTime = new DateTime(1972, 4, 26, 12, 40, 0, DateTimeKind.Local)
            });
            result.Items.Add(new VoucherItem()
            {
                Code = "1002", VoucherCode = "Voucher 1002", CreateTime = new DateTime(1972, 4, 26, 13, 00, 0, DateTimeKind.Local)
            });

            result.Items.CollectioName = "TestCollection";

            return(result);
        }
Example #6
0
        private static void AssertVoucherEntity(VoucherEntity source, VoucherEntity dest, bool withCollectionName = true)
        {
            Assert.AreEqual(source.Name, dest.Name);

            AssertVoucherItemCollection(source.Items, dest.Items, withCollectionName);
        }