Пример #1
0
        public void testDeserializeType()
        {
            TJEntity x = new TJEntity();

            x.Id      = 2;
            x.Name    = "x02";
            x.TMoney  = 2.92M;
            x.Area    = 22222222222222222L;
            x.TDouble = 22222.2222222299;
            x.IsBlack = true;
            x.Created = DateTime.Now.AddDays(-2);

            x.TPhone = new TPhone {
                Id = 1, Name = "p01", Weight = 11
            };
            x.JsonObject = new JsonObject();
            x.JsonObject.Add("key1", "v1");
            x.JsonObject.Add("key2", "v2");

            x.IntArray = new int[] { 2, 6, 8 };
            x.Id_List  = new List <int> {
                3, 4, 5
            };
            x.Name_List = new List <string> {
                "x03", "x04", "x05"
            };
            x.TMoney_List = new List <decimal> {
                3.93M, 4.94M, 5.95M
            };
            x.Area_List = new List <long> {
                3333333333333333333L, 444444444444444444L, 555555555555555555L
            };
            x.TValue_List = new List <double> {
                33333.2222222299, 44444.2222222299, 55555.2222222299
            };
            x.IsBlack_List = new List <bool> {
                false, true, false
            };
            x.Created_List = new List <DateTime> {
                DateTime.Now.AddDays(-3), DateTime.Now.AddDays(-4), DateTime.Now.AddDays(-5)
            };

            x.TPhone_List = new List <TPhone> {
                new TPhone {
                    Id = 2, Name = "p02", Weight = 22
                },
                new TPhone {
                    Id = 3, Name = "p03", Weight = 33
                },
                new TPhone {
                    Id = 4, Name = "p04", Weight = 44
                }
            };

            x.TPhone_Dic = new Dictionary <string, TPhone>();
            x.TPhone_Dic.Add("phone5", new TPhone {
                Id = 5, Name = "p05", Weight = 55
            });
            x.TPhone_Dic.Add("phone6", new TPhone {
                Id = 6, Name = "p06", Weight = 66
            });
            x.TPhone_Dic.Add("phone7", new TPhone {
                Id = 7, Name = "p07", Weight = 77
            });

            x.TPhone_Array = new TPhone[] {
                new TPhone {
                    Id = 7, Name = "p07", Weight = 77
                },
                new TPhone {
                    Id = 8, Name = "p08", Weight = 88
                },
                new TPhone {
                    Id = 9, Name = "p09", Weight = 99
                }
            };


            String jsonString = Json.ToString(x);

            Console.WriteLine(jsonString);

            // 反序列化测试
            TJEntity k = Json.Deserialize <TJEntity>(jsonString);

            Assert.IsNotNull(k);

            // 基础类型的属性
            Assert.AreEqual(x.Id, k.Id);
            Assert.AreEqual(x.Name, k.Name);
            Assert.AreEqual(x.Area, k.Area);

            Assert.AreEqual(x.TMoney, k.TMoney);
            Assert.AreEqual(x.TDouble, k.TDouble);
            Assert.AreEqual(x.IsBlack, k.IsBlack);
            Assert.IsTrue(isTimeEqual(x.Created, k.Created));

            // 其他对象类型
            Assert.IsNotNull(k.TPhone);
            Assert.AreEqual(x.TPhone.Id, k.TPhone.Id);
            Assert.AreEqual(x.TPhone.Name, k.TPhone.Name);
            Assert.AreEqual(x.TPhone.Weight, k.TPhone.Weight);

            // 原始 JsonObject 对象
            Assert.IsNotNull(k.JsonObject);
            Assert.AreEqual("v1", k.JsonObject.Get("key1"));
            Assert.AreEqual("v2", k.JsonObject.Get("key2"));

            // List列表属性
            Assert.AreEqual(x.Id_List.Count, k.Id_List.Count);
            Assert.AreEqual(x.Id_List[0], k.Id_List[0]);
            Assert.AreEqual(x.Id_List[1], k.Id_List[1]);
            Assert.AreEqual(x.Id_List[2], k.Id_List[2]);

            Assert.AreEqual(x.IntArray.Length, k.IntArray.Length);
            Assert.AreEqual(x.IntArray[0], k.IntArray[0]);
            Assert.AreEqual(x.IntArray[1], k.IntArray[1]);
            Assert.AreEqual(x.IntArray[2], k.IntArray[2]);

            Assert.AreEqual(x.Name_List.Count, k.Name_List.Count);
            Assert.AreEqual(x.Name_List[0], k.Name_List[0]);
            Assert.AreEqual(x.Name_List[1], k.Name_List[1]);
            Assert.AreEqual(x.Name_List[2], k.Name_List[2]);

            Assert.AreEqual(x.Area_List.Count, k.Area_List.Count);
            Assert.AreEqual(x.Area_List[0], k.Area_List[0]);
            Assert.AreEqual(x.Area_List[1], k.Area_List[1]);
            Assert.AreEqual(x.Area_List[2], k.Area_List[2]);

            Assert.AreEqual(x.TMoney_List.Count, k.TMoney_List.Count);
            Assert.AreEqual(x.TMoney_List[0], k.TMoney_List[0]);
            Assert.AreEqual(x.TMoney_List[1], k.TMoney_List[1]);
            Assert.AreEqual(x.TMoney_List[2], k.TMoney_List[2]);

            Assert.AreEqual(x.TValue_List.Count, k.TValue_List.Count);
            Assert.AreEqual(x.TValue_List[0], k.TValue_List[0]);
            Assert.AreEqual(x.TValue_List[1], k.TValue_List[1]);
            Assert.AreEqual(x.TValue_List[2], k.TValue_List[2]);

            Assert.AreEqual(x.IsBlack_List.Count, k.IsBlack_List.Count);
            Assert.AreEqual(x.IsBlack_List[0], k.IsBlack_List[0]);
            Assert.AreEqual(x.IsBlack_List[1], k.IsBlack_List[1]);
            Assert.AreEqual(x.IsBlack_List[2], k.IsBlack_List[2]);

            Assert.AreEqual(x.Created_List.Count, k.Created_List.Count);
            Assert.IsTrue(isTimeEqual(x.Created_List[0], k.Created_List[0]));
            Assert.IsTrue(isTimeEqual(x.Created_List[1], k.Created_List[1]));
            Assert.IsTrue(isTimeEqual(x.Created_List[2], k.Created_List[2]));

            // List<TypedObject>属性
            Assert.AreEqual(x.TPhone_List.Count, k.TPhone_List.Count);
            Assert.AreEqual(x.TPhone_List[0].Id, k.TPhone_List[0].Id);
            Assert.AreEqual(x.TPhone_List[0].Name, k.TPhone_List[0].Name);
            Assert.AreEqual(x.TPhone_List[0].Weight, k.TPhone_List[0].Weight);

            // Array<TypedObject>属性
            Assert.AreEqual(x.TPhone_Array.Length, k.TPhone_Array.Length);
            Assert.AreEqual(x.TPhone_Array[0].Id, k.TPhone_Array[0].Id);
            Assert.AreEqual(x.TPhone_Array[0].Name, k.TPhone_Array[0].Name);
            Assert.AreEqual(x.TPhone_Array[0].Weight, k.TPhone_Array[0].Weight);

            // Dictionary<String, TypedObject>属性
            Assert.AreEqual(x.TPhone_Dic.Count, k.TPhone_Dic.Count);
            foreach (KeyValuePair <String, TPhone> kv in x.TPhone_Dic)
            {
                Assert.AreEqual(kv.Value.Id, k.TPhone_Dic[kv.Key].Id);
                Assert.AreEqual(kv.Value.Name, k.TPhone_Dic[kv.Key].Name);
                Assert.AreEqual(kv.Value.Weight, k.TPhone_Dic[kv.Key].Weight);
            }
        }
        public void testDeserializeType()
        {
            TJEntity x = new TJEntity();
            x.Id = 2;
            x.Name = "x02";
            x.TMoney = 2.92M;
            x.Area = 22222222222222222L;
            x.TDouble = 22222.2222222299;
            x.IsBlack = true;
            x.Created = DateTime.Now.AddDays( -2 );

            x.TPhone = new TPhone { Id = 1, Name = "p01", Weight = 11 };
            x.JsonObject = new JsonObject();
            x.JsonObject.Add( "key1", "v1" );
            x.JsonObject.Add( "key2", "v2" );

            x.IntArray = new int[] { 2, 6, 8 };
            x.Id_List = new List<int> { 3, 4, 5 };
            x.Name_List = new List<string> { "x03", "x04", "x05" };
            x.TMoney_List = new List<decimal> { 3.93M, 4.94M, 5.95M };
            x.Area_List = new List<long> { 3333333333333333333L, 444444444444444444L, 555555555555555555L };
            x.TValue_List = new List<double> { 33333.2222222299, 44444.2222222299, 55555.2222222299 };
            x.IsBlack_List = new List<bool> { false, true, false };
            x.Created_List = new List<DateTime> { DateTime.Now.AddDays( -3 ), DateTime.Now.AddDays( -4 ), DateTime.Now.AddDays( -5 ) };

            x.TPhone_List = new List<TPhone> {
                new TPhone { Id=2, Name = "p02", Weight=22 },
                new TPhone { Id=3, Name = "p03", Weight=33 },
                new TPhone { Id=4, Name = "p04", Weight=44 }
            };

            x.TPhone_Dic = new Dictionary<string, TPhone>();
            x.TPhone_Dic.Add( "phone5", new TPhone { Id = 5, Name = "p05", Weight = 55 } );
            x.TPhone_Dic.Add( "phone6", new TPhone { Id = 6, Name = "p06", Weight = 66 } );
            x.TPhone_Dic.Add( "phone7", new TPhone { Id = 7, Name = "p07", Weight = 77 } );

            x.TPhone_Array = new TPhone[] {
                new TPhone { Id=7, Name = "p07", Weight=77 },
                new TPhone { Id=8, Name = "p08", Weight=88 },
                new TPhone { Id=9, Name = "p09", Weight=99 }
            };

            String jsonString = Json.ToString( x );
            Console.WriteLine( jsonString );

            // 反序列化测试
            TJEntity k = Json.Deserialize<TJEntity>( jsonString );
            Assert.IsNotNull( k );

            // 基础类型的属性
            Assert.AreEqual( x.Id, k.Id );
            Assert.AreEqual( x.Name, k.Name );
            Assert.AreEqual( x.Area, k.Area );

            Assert.AreEqual( x.TMoney, k.TMoney );
            Assert.AreEqual( x.TDouble, k.TDouble );
            Assert.AreEqual( x.IsBlack, k.IsBlack );
            Assert.IsTrue( isTimeEqual( x.Created, k.Created ) );

            // 其他对象类型
            Assert.IsNotNull( k.TPhone );
            Assert.AreEqual( x.TPhone.Id, k.TPhone.Id );
            Assert.AreEqual( x.TPhone.Name, k.TPhone.Name );
            Assert.AreEqual( x.TPhone.Weight, k.TPhone.Weight );

            // 原始 JsonObject 对象
            Assert.IsNotNull( k.JsonObject );
            Assert.AreEqual( "v1", k.JsonObject.Get( "key1" ) );
            Assert.AreEqual( "v2", k.JsonObject.Get( "key2" ) );

            // List列表属性
            Assert.AreEqual( x.Id_List.Count, k.Id_List.Count );
            Assert.AreEqual( x.Id_List[0], k.Id_List[0] );
            Assert.AreEqual( x.Id_List[1], k.Id_List[1] );
            Assert.AreEqual( x.Id_List[2], k.Id_List[2] );

            Assert.AreEqual( x.IntArray.Length, k.IntArray.Length );
            Assert.AreEqual( x.IntArray[0], k.IntArray[0] );
            Assert.AreEqual( x.IntArray[1], k.IntArray[1] );
            Assert.AreEqual( x.IntArray[2], k.IntArray[2] );

            Assert.AreEqual( x.Name_List.Count, k.Name_List.Count );
            Assert.AreEqual( x.Name_List[0], k.Name_List[0] );
            Assert.AreEqual( x.Name_List[1], k.Name_List[1] );
            Assert.AreEqual( x.Name_List[2], k.Name_List[2] );

            Assert.AreEqual( x.Area_List.Count, k.Area_List.Count );
            Assert.AreEqual( x.Area_List[0], k.Area_List[0] );
            Assert.AreEqual( x.Area_List[1], k.Area_List[1] );
            Assert.AreEqual( x.Area_List[2], k.Area_List[2] );

            Assert.AreEqual( x.TMoney_List.Count, k.TMoney_List.Count );
            Assert.AreEqual( x.TMoney_List[0], k.TMoney_List[0] );
            Assert.AreEqual( x.TMoney_List[1], k.TMoney_List[1] );
            Assert.AreEqual( x.TMoney_List[2], k.TMoney_List[2] );

            Assert.AreEqual( x.TValue_List.Count, k.TValue_List.Count );
            Assert.AreEqual( x.TValue_List[0], k.TValue_List[0] );
            Assert.AreEqual( x.TValue_List[1], k.TValue_List[1] );
            Assert.AreEqual( x.TValue_List[2], k.TValue_List[2] );

            Assert.AreEqual( x.IsBlack_List.Count, k.IsBlack_List.Count );
            Assert.AreEqual( x.IsBlack_List[0], k.IsBlack_List[0] );
            Assert.AreEqual( x.IsBlack_List[1], k.IsBlack_List[1] );
            Assert.AreEqual( x.IsBlack_List[2], k.IsBlack_List[2] );

            Assert.AreEqual( x.Created_List.Count, k.Created_List.Count );
            Assert.IsTrue( isTimeEqual( x.Created_List[0], k.Created_List[0] ) );
            Assert.IsTrue( isTimeEqual( x.Created_List[1], k.Created_List[1] ) );
            Assert.IsTrue( isTimeEqual( x.Created_List[2], k.Created_List[2] ) );

            // List<TypedObject>属性
            Assert.AreEqual( x.TPhone_List.Count, k.TPhone_List.Count );
            Assert.AreEqual( x.TPhone_List[0].Id, k.TPhone_List[0].Id );
            Assert.AreEqual( x.TPhone_List[0].Name, k.TPhone_List[0].Name );
            Assert.AreEqual( x.TPhone_List[0].Weight, k.TPhone_List[0].Weight );

            // Array<TypedObject>属性
            Assert.AreEqual( x.TPhone_Array.Length, k.TPhone_Array.Length );
            Assert.AreEqual( x.TPhone_Array[0].Id, k.TPhone_Array[0].Id );
            Assert.AreEqual( x.TPhone_Array[0].Name, k.TPhone_Array[0].Name );
            Assert.AreEqual( x.TPhone_Array[0].Weight, k.TPhone_Array[0].Weight );

            // Dictionary<String, TypedObject>属性
            Assert.AreEqual( x.TPhone_Dic.Count, k.TPhone_Dic.Count );
            foreach (KeyValuePair<String, TPhone> kv in x.TPhone_Dic) {
                Assert.AreEqual( kv.Value.Id, k.TPhone_Dic[kv.Key].Id );
                Assert.AreEqual( kv.Value.Name, k.TPhone_Dic[kv.Key].Name );
                Assert.AreEqual( kv.Value.Weight, k.TPhone_Dic[kv.Key].Weight );
            }
        }