コード例 #1
0
ファイル: Json_Unittest.cs プロジェクト: yanyitec/Yanyitec
        public void ParseToJToken()
        {
            var obj = new JObject();
            obj["Id"] = Guid.Empty;
            obj["Username"] = "******";

            var profile = new JObject();
            profile["Birthday"] = new DateTime(2015, 1, 1);
            profile["Age"] = 12;

            var favors = new JArray();
            favors.Push(12);
            favors.Push("Sweet Milk");
            obj["Profile"] = profile;
            profile["Favors"] = favors;

            obj["Actived"] = true;

            var jsonString = obj.ToJson();

            var convert = new Json.Parser();
            var json = convert.Parse(jsonString) as JObject;
            System.Text.RegularExpressions.Regex JsonDateRegex = new System.Text.RegularExpressions.Regex("^(\\d{4})\\-(10|11|12|0?\\d)\\-([012]\\d|30|31)T([01]\\d|2[0-3]):([0-5]?\\d):([0-5]?\\d)$", System.Text.RegularExpressions.RegexOptions.Compiled);
            var datestr = "2015-01-01T10:3:3";
            var match = JsonDateRegex.Match(datestr);
            Assert.Equal(Guid.Empty.ToString(), json["Id"].ToString());
            Assert.Equal("yiy", (string)json["Username"]);
            Assert.Equal(true, (bool)json["Actived"]);
            profile = json["Profile"] as JObject;
            Assert.Equal(new DateTime(2015, 1, 1), (DateTime)profile["Birthday"]);
            Assert.Equal(12, (int)profile["Age"]);
            favors = profile["Favors"] as JArray;
            Assert.Equal(12, (int)favors[0]);
            Assert.Equal("Sweet Milk", (string)favors[1]);

        }
コード例 #2
0
ファイル: Json_Unittest.cs プロジェクト: yanyitec/Yanyitec
        public void ParseToTokenWithFunction()
        {
            var jsonstring = @"{no:1,onstart : ";
            var func = @"function(evt,bk){
    var s = 'hello';
    var err={bk:s};
    for(var n in err){
        s += " + "\"\\\"\" + n + \"\\\"\" ':' + err[n];"
    + @"}
    var fn = function(s){alert(s);}
    fn(s);
}";
            jsonstring += func + "}";

            var convert = new Json.Parser();
            var obj = convert.Parse(jsonstring) as JObject;
            Assert.Equal(1, (int)obj["no"]);
            Assert.InstanceOf(obj["onstart"], typeof(JFunction));
            Assert.Equal(func, obj["onstart"].ToString().Trim());

        }