Example #1
0
        public void JObject_With_JArray_Prop_Remove_Test_Should_Succeed()
        {
            var json = new
            {
                code = 1,
                msg  = "2",
                data = new List <dynamic>
                {
                    new { prop1 = 11, prop2 = "12" },
                    new { prop1 = 21, prop2 = "22" },
                },
            }.ToJson();

            var fields = new List <string> {
                "prop1"
            };

            var actual = JsonTProvider.FilterNodes(json, "data", fields, true);

            var expected = new
            {
                code = 1,
                msg  = "2",
                data = new List <dynamic>
                {
                    new { prop2 = "12" },
                    new { prop2 = "22" },
                },
            }.ToJson();

            Assert.Equal(expected, actual);
        }
        public void JObject_Remove_Test_Should_Succeed()
        {
            var json = new
            {
                prop1 = 1,
                prop2 = "2",
                prop3 = new{ sub1 = 1 },
                prop4 = new List <string> {
                    "a", "b"
                }
            }.ToJson();

            var fields = new List <string> {
                "prop1", "prop3"
            };

            var actual = JsonTProvider.FilterNodes(json, fields, true);

            var expected = new
            {
                prop2 = "2",
                prop4 = new List <string> {
                    "a", "b"
                }
            }.ToJson();

            Assert.Equal(expected, actual);
        }
Example #3
0
        public void JObject_With_JObject_Prop_Save_Test_Should_Succeed()
        {
            var json = new
            {
                code = 1,
                msg  = "2",
                data = new
                {
                    prop1 = 1,
                    prop2 = "2",
                },
            }.ToJson();

            var fields = new List <string> {
                "prop1"
            };

            var actual = JsonTProvider.FilterNodes(json, "data", fields, false);

            var expected = new
            {
                code = 1,
                msg  = "2",
                data = new
                {
                    prop1 = 1,
                },
            }.ToJson();

            Assert.Equal(expected, actual);
        }
Example #4
0
        public void JObject_With_Prop_Should_Throw_ArgumentException_When_Input_Is_Not_Object()
        {
            var json = new List <dynamic>
            {
                new { prop1 = 11, prop2 = "12" },
            }.ToJson();

            var fields = new List <string> {
                "prop1"
            };

            Assert.Throws <ArgumentException>(() => JsonTProvider.FilterNodes(json, "data", fields, false));
        }
Example #5
0
        public void JObject_With_Prop_Should_Throw_NotFoundPropertyException_When_Input_DoNot_Contain_Prop()
        {
            var json = new
            {
                code = 1,
                msg  = "2",
                data = new List <dynamic>
                {
                    new { prop1 = 11, prop2 = "12" },
                    new { prop1 = 21, prop2 = "22" },
                },
            }.ToJson();

            var fields = new List <string> {
                "prop1"
            };

            Assert.Throws <NotFoundNodeException>(() => JsonTProvider.FilterNodes(json, "obj", fields, false));
        }
        public void JArray_Remove_Test_Should_Succeed()
        {
            var json = new List <dynamic>
            {
                new
                {
                    prop1 = "a1",
                    prop2 = 12
                },
                new
                {
                    prop1 = "a2",
                    prop2 = 22
                }
            }
            .ToJson();

            var fields = new List <string> {
                "prop1"
            };

            var actual = JsonTProvider.FilterNodes(json, fields, true);

            var expected = new List <dynamic>
            {
                new
                {
                    prop2 = 12
                },
                new
                {
                    prop2 = 22
                }
            }
            .ToJson();

            Assert.Equal(expected, actual);
        }
Example #7
0
        static void Main(string[] args)
        {
            //1. filter nodes

            var filterNode = new List <string> {
                "UserName", "age"
            };

            var filterJson1        = @"{'userName':'******','age':18,'hobbies':['write','running']}";
            var filterJsonResult11 = JsonTProvider.FilterNodes(filterJson1, filterNode, true);
            //{"hobbies": ["write, "running"]}
            var filterJsonResult12 = JsonTProvider.FilterNodes(filterJson1, filterNode, false);
            //{"userName": "******", "age": 18}

            var filterJson2        = @"[{'userName':'******','age':18,'hobbies':['write','running']},{'userName':'******','age':20,'hobbies':['music']}]";
            var filterJsonResult21 = JsonTProvider.FilterNodes(filterJson2, filterNode, true);
            //[{"hobbies":["write","running"]},{"hobbies":["music"]}]
            var filterJsonResult22 = JsonTProvider.FilterNodes(filterJson2, filterNode, false);
            //[{"userName":"******","age":18},{"userName":"******","age":20}]

            var filterJson3        = @"{'code':0,'msg':'ok','data':{'userName':'******','age':18,'hobbies':['write','running']}}";
            var filterJsonResult31 = JsonTProvider.FilterNodes(filterJson3, "data", filterNode, true);
            //{"code":0,"msg":"ok","data":{"hobbies":["write","running"]}}
            var filterJsonResult32 = JsonTProvider.FilterNodes(filterJson3, "data", filterNode, false);
            //{"code":0,"msg":"ok","data":{"userName":"******","age":18}}

            var filterJson4        = @"{'code':0,'msg':'ok','data':[{'userName':'******','age':18,'hobbies':['write','running']},{'userName':'******','age':20,'hobbies':['music']}]}";
            var filterJsonResult41 = JsonTProvider.FilterNodes(filterJson4, "data", filterNode, true);
            //{"code":0,"msg":"ok","data":[{"hobbies":["write","running"]},{"hobbies":["music"]}]}
            var filterJsonResult42 = JsonTProvider.FilterNodes(filterJson4, "data", filterNode, false);
            //{"code":0,"msg":"ok","data":[{"userName":"******","age":18},{"userName":"******","age":20}]}

            //2. add nodes
            var addNodesDict = new Dictionary <string, object>
            {
                { "age", 18 },
                { "subObj", new{ prop1 = "123" } },
                { "subArray", new List <string> {
                      "a", "b"
                  } }
            };

            var addNodesJson    = @"{'userName':'******'}";
            var addNodesResult1 = JsonTProvider.AddNodes(addNodesJson, addNodesDict);
            //{"userName":"******","age":18,"subObj":{"prop1":"123"},"subArray":["a","b"]}
            var addNodesResult2 = JsonTProvider.AddNodes(addNodesJson, addNodesDict, "basic");
            //{"basic":{"userName":"******"},"age":18,"subObj":{"prop1":"123"},"subArray":["a","b"]}

            //3. rename nodes
            var renameDict = new Dictionary <string, string>
            {
                { "name", "userName" }, { "nl", "age" }
            };

            var renameJson1       = @"{'name':'catcherwong','nl':18}";
            var renameJsonResult1 = JsonTProvider.RenameNodes(renameJson1, renameDict);
            //{"userName":"******","age":18}

            var renameJson2       = @"{'code':0,'msg':'ok','data':{'name':'catcherwong','nl':18}}";
            var renameJsonResult2 = JsonTProvider.RenameNodes(renameJson2, "data", renameDict);
            //{"code":0,"msg":"ok","data":{"userName":"******","age":18}}


            //4. translate values
            var translateValueDict = new Dictionary <string, Dictionary <object, object> >
            {
                { "Code", new Dictionary <object, object> {
                      { -1, 0 }, { -2, 1 }
                  } },
                { "messAge", new Dictionary <object, object> {
                      { "yes", "Success" }, { "no", "Error" }
                  } }
            };

            var translateJson1   = @"{'code':-1,'message':'yes'}";
            var translateResult1 = JsonTProvider.TranslateValues(translateJson1, translateValueDict);
            //{"code":0,"message":"Success"}

            var translateJson2   = @"[{'code':-1,'message':'yes'},{'code':-2,'message':'no'}]";
            var translateResult2 = JsonTProvider.TranslateValues(translateJson2, translateValueDict);
            //[{"code":0,"message":"Success"},{"code":1,"message":"Error"}]

            var translateJson3   = @"{'myCode':-1,'myMessage':'yes','data':{'code':-2,'message':'no'}}";
            var translateResult3 = JsonTProvider.TranslateValues(translateJson3, "data", translateValueDict);
            //{"myCode":-1,"myMessage":"yes","data":{"code":1,"message":"Error"}}

            var translateJson4   = @"{'myCode':-1,'myMessage':'yes','data':[{'code':-1,'message':'yes'},{'code':-2,'message':'no'}]}";
            var translateResult4 = JsonTProvider.TranslateValues(translateJson4, "data", translateValueDict);

            //{"myCode":-1,"myMessage":"yes","data":[{"code":0,"message":"Success"},{"code":1,"message":"Error"}]}

            Console.ReadKey();
        }