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);
        }
        public void Translate_Values_For_JObject_Should_Succeed(int code, string msg)
        {
            var json = new
            {
                code,
                msg,
            }.ToJson();

            var valMap = new Dictionary <string, Dictionary <object, object> >
            {
                { "code", new Dictionary <object, object> {
                      { 100, 0 }, { 200, -1 }
                  } },
                { "msg", new Dictionary <object, object> {
                      { "ok", "yes" }, { "error", "no" }
                  } }
            };

            var res = JsonTProvider.TranslateValues(json, valMap);

            var expected = new
            {
                code = code == 100 ? 0 : -1,
                msg  = msg == "ok" ? "yes" : "no",
            }.ToJson();

            Assert.Equal(expected, res);
        }
示例#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);
        }
示例#4
0
        public void Add_Nodes_With_Node_Name_Should_Succeed_When_JSON_Is_Object()
        {
            var json = new
            {
                prop1 = 1
            }.ToJson();

            var addNodes = new Dictionary <string, object>
            {
                { "prop2", 2 },
                { "prop3", new{ sub = 1 } },
                { "prop4", new List <string> {
                      "a"
                  } }
            };

            var res = JsonTProvider.AddNodes(json, addNodes, "nodeName");

            var expected = new
            {
                nodeName = new
                {
                    prop1 = 1
                },
                prop2 = 2,
                prop3 = new { sub = 1 },
                prop4 = new List <string> {
                    "a"
                }
            }.ToJson();

            Assert.Equal(expected, res);
        }
示例#5
0
        public void Add_Exist_Nodes_Should_Not_Effect()
        {
            var json = new
            {
                prop1 = 1
            }.ToJson();

            var addNodes = new Dictionary <string, object>
            {
                { "prop1", 2 },
                { "prop3", new{ sub = 1 } },
                { "prop4", new List <string> {
                      "a"
                  } }
            };

            var res = JsonTProvider.AddNodes(json, addNodes);

            var expected = new
            {
                prop1 = 1, //the value should be 1, not 2
                prop3 = new { sub = 1 },
                prop4 = new List <string> {
                    "a"
                }
            }.ToJson();

            Assert.Equal(expected, res);
        }
示例#6
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);
        }
示例#7
0
        public void Rename_Nodes_For_JObject_Should_Succeed()
        {
            var json = new
            {
                code    = 100,
                message = "msg",
                data    = true
            }.ToJson();

            var nameMap = new Dictionary <string, string>
            {
                { "code", "myCode" },
                { "message", "msg" }
            };

            var res = JsonTProvider.RenameNodes(json, nameMap);

            var expected = new
            {
                data   = true,
                myCode = 100,
                msg    = "msg",
            }.ToJson();

            Assert.Equal(expected, res);
        }
        public void Translate_Values_With_Node_Should_Throw_NotFoundPropertyException_When_Input_DoNot_Contain_Prop()
        {
            var json = new
            {
                code = 0,
                msg  = "ok",
                data = new List <dynamic>
                {
                    new
                    {
                        prop1 = true,
                        prop2 = 99
                    },
                    new
                    {
                        prop1 = false,
                        prop2 = -99
                    }
                }
            }
            .ToJson();

            var valMap = new Dictionary <string, Dictionary <object, object> >
            {
                { "prop1", new Dictionary <object, object> {
                      { false, 0 }, { true, 1 }
                  } },
                { "prop2", new Dictionary <object, object> {
                      { -99, 99 }
                  } }
            };

            Assert.Throws <NotFoundNodeException>(() => JsonTProvider.TranslateValues(json, "obj", valMap));
        }
        public void Translate_Values_With_JArray_Node_Should_Succeed()
        {
            var json = new
            {
                code = 0,
                msg  = "ok",
                data = new List <dynamic>
                {
                    new
                    {
                        prop1 = true,
                        prop2 = 99
                    },
                    new
                    {
                        prop1 = false,
                        prop2 = -99
                    }
                }
            }
            .ToJson();

            var valMap = new Dictionary <string, Dictionary <object, object> >
            {
                { "prop1", new Dictionary <object, object> {
                      { false, 0 }, { true, 1 }
                  } },
                { "prop2", new Dictionary <object, object> {
                      { -99, 99 }
                  } }
            };

            var res = JsonTProvider.TranslateValues(json, "data", valMap);

            var expected = new
            {
                code = 0,
                msg  = "ok",
                data = new List <dynamic>
                {
                    new
                    {
                        prop1 = 1,
                        prop2 = 99
                    },
                    new
                    {
                        prop1 = 0,
                        prop2 = 99
                    }
                }
            }.ToJson();

            Assert.Equal(expected, res);
        }
示例#10
0
        public void Rename_Nodes_With_JArray_Root_Should_Succeed()
        {
            var json = new
            {
                code    = 100,
                message = "msg",
                data    = new List <dynamic>
                {
                    new
                    {
                        code    = 100,
                        message = "msg",
                        data    = true,
                    },
                    new
                    {
                        code    = 200,
                        message = "msg2",
                        data    = false,
                    }
                }
            }.ToJson();

            var nameMap = new Dictionary <string, string>
            {
                { "code", "myCode" },
                { "message", "msg" }
            };

            var res = JsonTProvider.RenameNodes(json, "data", nameMap);

            var expected = new
            {
                code    = 100,
                message = "msg",
                data    = new List <dynamic>
                {
                    new
                    {
                        data   = true,
                        myCode = 100,
                        msg    = "msg",
                    },
                    new
                    {
                        data   = false,
                        myCode = 200,
                        msg    = "msg2",
                    }
                }
            }.ToJson();

            Assert.Equal(expected, res);
        }
示例#11
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));
        }
示例#12
0
        public void Rename_Nodes_For_BasicType_Should_Not_Effect()
        {
            var json = 2.ToJson();

            var nameMap = new Dictionary <string, string>
            {
                { "code", "myCode" },
                { "message", "msg" }
            };

            var res = JsonTProvider.RenameNodes(json, nameMap);

            Assert.Equal(json, res);
        }
        public void Translate_Values_With_Node_Should_Throw_NotSpecialJsonTypeException_When_Input_Is_Not_Object()
        {
            var json = 1.ToJson();

            var valMap = new Dictionary <string, Dictionary <object, object> >
            {
                { "prop1", new Dictionary <object, object> {
                      { false, 0 }, { true, 1 }
                  } },
                { "prop2", new Dictionary <object, object> {
                      { -99, 99 }
                  } }
            };

            Assert.Throws <NotSpecialJsonTypeException>(() => JsonTProvider.TranslateValues(json, "obj", valMap));
        }
示例#14
0
        public void Add_Nodes_Should_Throw_NotSpecialJsonTypeException()
        {
            var json = new List <string> {
                "a"
            }.ToJson();

            var addNodes = new Dictionary <string, object>
            {
                { "prop2", 2 },
                { "prop3", new{ sub = 1 } },
                { "prop4", new List <string> {
                      "a"
                  } }
            };

            Assert.Throws <NotSpecialJsonTypeException>(() => JsonTProvider.AddNodes(json, addNodes));
        }
示例#15
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);
        }
示例#17
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();
        }