Пример #1
0
        public void testHashtable()
        {
            Hashtable dic = new Hashtable();

            dic.Add("name", "sunweb");
            dic.Add("age", 99);
            dic.Add("gender", "male");

            string str = Json.ToString(dic, false);

            JsonObject obj = Json.ParseJson(str);

            Assert.AreEqual("sunweb", obj.Get("name"));
            Assert.AreEqual("male", obj.Get("gender"));
            Assert.AreEqual(99, obj.Get <int>("age"));

            //没有顺序
            //Assert.AreEqual( "{ \"name\":\"sunweb\", \"age\":99, \"gender\":\"male\" }", str );

            MyPhone phone = new MyPhone();

            phone.Name  = "新闻大事690501468";
            phone.Owner = new PhoneOwner {
                Id = 2
            };
            dic.Add("phone", phone);

            str = Json.ToString(dic, false);
            Console.WriteLine(str);
        }
Пример #2
0
        public void testDictionary()
        {
            Dictionary <string, object> dic = new Dictionary <string, object>();

            dic.Add("name", "sunweb");
            dic.Add("age", 99);
            dic.Add("gender", "male");

            string str = Json.ToString(dic, false);

            Assert.AreEqual("{ \"name\":\"sunweb\", \"age\":99, \"gender\":\"male\" }", str);

            // 将对象放入dic中
            MyPhone phone = new MyPhone();

            phone.Name  = "新闻大事690501468";
            phone.Owner = new PhoneOwner {
                Id = 2
            };
            dic.Add("phone", phone);

            str = Json.ToString(dic, false);
            Console.WriteLine(str);
            Assert.AreEqual("{ \"name\":\"sunweb\", \"age\":99, \"gender\":\"male\", \"phone\":{ \"Id\":0, \"Name\":\"新闻大事690501468\", \"Weight\":0, \"Owner\":{ \"Id\":2, \"Name\":\"\", \"Age\":\"\" } } }", str);
        }
Пример #3
0
        private static MyPhone getPhoneList()
        {
            MyPhone phone = new MyPhone();

            phone.Name  = "新闻大事690501468";
            phone.Owner = new PhoneOwner {
                Id = 2
            };
            return(phone);
        }
Пример #4
0
        public void testDeserializeTypeSimple()
        {
            string str = "{Id:2,  Name:\"诺基亚n78\", Weight:300, Owner:{Id:88,Name:\"ownerName\",Age:\"999\"}}";

            MyPhone phone = Json.Deserialize <MyPhone>(str);

            Assert.IsNotNull(phone);
            Assert.AreEqual(88, phone.Owner.Id);
            Assert.AreEqual("ownerName", phone.Owner.Name);
            Assert.AreEqual("999", phone.Owner.Age);
        }
Пример #5
0
        public void StringToObject()
        {
            string str = "{Id:2,  Name:\"诺基亚n78\", Weight:300, Owner:6}";

            object obj = JSON.ToObject(str, typeof(MyPhone));

            Assert.IsNotNull(obj);
            MyPhone phone = obj as MyPhone;

            Assert.IsNotNull(phone);
            Assert.AreEqual(6, phone.Owner.Id);
        }
Пример #6
0
        public void testSimpleObject()
        {
            MyPhone phone = new MyPhone();

            phone.Name  = "新闻大事690501468";
            phone.Owner = new PhoneOwner {
                Id = 2
            };

            string strJson = Json.ToString(phone);

            Console.WriteLine(strJson);

            string result = " { \"Id\":0, \"Name\":\"新闻大事690501468\", \"Weight\":0, \"Owner\":{ \"Id\":2, \"Name\":\"\", \"Age\":\"\" } }";

            Assert.AreEqual(result.Trim(), strJson.Trim());
        }
Пример #7
0
        public void testList()
        {
            List <string> list = new List <string>();

            list.Add("123");
            list.Add("abc");
            list.Add("");
            list.Add("name");

            string str = Json.ToString(list);

            Assert.AreEqual("[ \"123\", \"abc\", \"\", \"name\" ]", str);

            string newStr = Json.ToString(list);

            Assert.AreEqual(str, newStr);

            ArrayList nlist = new ArrayList();

            nlist.Add(123);
            nlist.Add("abc");
            nlist.Add("");
            nlist.Add("name");
            string nstr = Json.ToString(nlist);

            Assert.AreEqual("[ 123, \"abc\", \"\", \"name\" ]", nstr);

            MyPhone phone = new MyPhone();

            phone.Name  = "新闻大事690501468";
            phone.Owner = new PhoneOwner {
                Id = 2
            };

            ArrayList mylist = new ArrayList();

            mylist.Add("abc");
            mylist.Add(123);
            mylist.Add(phone);

            string mystr = Json.ToString(mylist);

            Assert.AreEqual("[ \"abc\", 123, { \"Id\":0, \"Name\":\"新闻大事690501468\", \"Weight\":0, \"Owner\":{ \"Id\":2, \"Name\":\"\", \"Age\":\"\" } } ]", mystr);
        }
Пример #8
0
        public void JsonStringToList()
        {
            string         result = @"
[
	{ Id:0, Name:""新闻大事690501468"", Weight:0, Owner:""2"" },
	{ Id:1, Name:""新闻大事690501468"", Weight:0, Owner:""2"" },
	{ Id:2, Name:""新闻大事690501468"", Weight:0, Owner:""2"" },
	{ Id:3, Name:""新闻大事690501468"", Weight:0, Owner:""2"" }
]
";
            List <MyPhone> list   = JSON.ToList <MyPhone>(result);

            Assert.AreEqual(4, list.Count);

            for (int i = 0; i < list.Count; i++)
            {
                MyPhone phone = list[i] as MyPhone;
                Assert.AreEqual(i, phone.Id);
            }
        }
Пример #9
0
        public void testHashtable()
        {
            Hashtable dic = new Hashtable();

            dic.Add("name", "sunweb");
            dic.Add("age", 99);
            dic.Add("gender", "male");

            string str = JsonString.ConvertDictionary(dic, false);

            Assert.AreEqual("{ \"name\":\"sunweb\", \"age\":99, \"gender\":\"male\" }", str);

            MyPhone phone = new MyPhone();

            phone.Name  = "新闻大事690501468";
            phone.Owner = new PhoneOwner {
                Id = 2
            };
            dic.Add("phone", phone);

            str = JsonString.ConvertDictionary(dic, false);
            Console.WriteLine(str);
        }
Пример #10
0
 private static MyPhone getPhoneList()
 {
     MyPhone phone = new MyPhone();
     phone.Name = "新闻大事690501468";
     phone.Owner = new PhoneOwner { Id = 2 };
     return phone;
 }
Пример #11
0
        public void testSimpleObject()
        {
            MyPhone phone = new MyPhone();
            phone.Name = "新闻大事690501468";
            phone.Owner = new PhoneOwner { Id = 2 };

            string strJson = Json.ToString( phone );

            Console.WriteLine( strJson );

            string result = " { \"Id\":0, \"Name\":\"新闻大事690501468\", \"Weight\":0, \"Owner\":{ \"Id\":2, \"Name\":\"\", \"Age\":\"\" } }";

            Assert.AreEqual( result.Trim(), strJson.Trim() );
        }
Пример #12
0
        public void testList()
        {
            List<string> list = new List<string>();
            list.Add( "123" );
            list.Add( "abc" );
            list.Add( "" );
            list.Add( "name" );

            string str = Json.ToString( list );
            Assert.AreEqual( "[ \"123\", \"abc\", \"\", \"name\" ]", str );

            string newStr = Json.ToString( list );
            Assert.AreEqual( str, newStr );

            ArrayList nlist = new ArrayList();
            nlist.Add( 123 );
            nlist.Add( "abc" );
            nlist.Add( "" );
            nlist.Add( "name" );
            string nstr = Json.ToString( nlist );
            Assert.AreEqual( "[ 123, \"abc\", \"\", \"name\" ]", nstr );

            MyPhone phone = new MyPhone();
            phone.Name = "新闻大事690501468";
            phone.Owner = new PhoneOwner { Id = 2 };

            ArrayList mylist = new ArrayList();
            mylist.Add( "abc" );
            mylist.Add( 123 );
            mylist.Add( phone );

            string mystr = Json.ToString( mylist );

            Assert.AreEqual( "[ \"abc\", 123, { \"Id\":0, \"Name\":\"新闻大事690501468\", \"Weight\":0, \"Owner\":{ \"Id\":2, \"Name\":\"\", \"Age\":\"\" } } ]", mystr );
        }
Пример #13
0
        public void testHashtable()
        {
            Hashtable dic = new Hashtable();
            dic.Add( "name", "sunweb" );
            dic.Add( "age", 99 );
            dic.Add( "gender", "male" );

            string str = Json.ToString( dic, false );

            JsonObject obj = Json.ParseJson( str );
            Assert.AreEqual( "sunweb", obj.Get( "name" ) );
            Assert.AreEqual( "male", obj.Get( "gender" ) );
            Assert.AreEqual( 99, obj.Get<int>( "age" ) );

            //没有顺序
            //Assert.AreEqual( "{ \"name\":\"sunweb\", \"age\":99, \"gender\":\"male\" }", str );

            MyPhone phone = new MyPhone();
            phone.Name = "新闻大事690501468";
            phone.Owner = new PhoneOwner { Id = 2 };
            dic.Add( "phone", phone );

            str = Json.ToString( dic, false );
            Console.WriteLine( str );
        }
Пример #14
0
        public void testDictionary()
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add( "name", "sunweb" );
            dic.Add( "age", 99 );
            dic.Add( "gender", "male" );

            string str = Json.ToString( dic, false );
            Assert.AreEqual( "{ \"name\":\"sunweb\", \"age\":99, \"gender\":\"male\" }", str );

            // 将对象放入dic中
            MyPhone phone = new MyPhone();
            phone.Name = "新闻大事690501468";
            phone.Owner = new PhoneOwner { Id = 2 };
            dic.Add( "phone", phone );

            str = Json.ToString( dic, false );
            Console.WriteLine( str );
            Assert.AreEqual( "{ \"name\":\"sunweb\", \"age\":99, \"gender\":\"male\", \"phone\":{ \"Id\":0, \"Name\":\"新闻大事690501468\", \"Weight\":0, \"Owner\":{ \"Id\":2, \"Name\":\"\", \"Age\":\"\" } } }", str );
        }
Пример #15
0
        public void testHashtable()
        {
            Hashtable dic = new Hashtable();
            dic.Add( "name", "sunweb" );
            dic.Add( "age", 99 );
            dic.Add( "gender", "male" );

            string str = JsonString.ConvertDictionary( dic, false );
            Assert.AreEqual( "{ \"name\":\"sunweb\", \"age\":99, \"gender\":\"male\" }", str );

            MyPhone phone = new MyPhone();
            phone.Name = "新闻大事690501468";
            phone.Owner = new PhoneOwner { Id = 2 };
            dic.Add( "phone", phone );

            str = JsonString.ConvertDictionary( dic, false );
            Console.WriteLine( str );
        }