コード例 #1
0
        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));
        }
コード例 #2
0
        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 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);
        }
コード例 #4
0
        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));
        }
コード例 #5
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();
        }