Exemple #1
0
        /// <summary>
        /// 绑定类型,在程序一运行的最早就应执行。
        /// </summary>
        public static void Bind()
        {
            if (isBind)
            {
                return;
            }
            isBind = true;

            //设置自定义的绑定类型
            JsonTypeRegister.BindType(typeof(Vector3), new xuexueJsonClass("x", "y", "z")
            {
                defaultFieldConstraint = false, defaultPropertyConstraint = false
            });
            JsonTypeRegister.BindType(typeof(Quaternion), new xuexueJsonClass("x", "y", "z", "w")
            {
                defaultFieldConstraint = false, defaultPropertyConstraint = false
            });
            JsonTypeRegister.BindType(typeof(Camera), new xuexueJsonClass("transform", "worldToCameraMatrix", "projectionMatrix")
            {
                defaultFieldConstraint = false, defaultPropertyConstraint = false
            });
            JsonTypeRegister.BindType(typeof(Transform), new xuexueJsonClass("name", "position", "rotation")
            {
                defaultFieldConstraint = false, defaultPropertyConstraint = false
            });

            //设置Json忽略类型
            JsonTypeRegister.AddIgnoreClass(typeof(GameObject));
            //JsonTypeRegister.AddIgnoreClass(typeof(Transform));
            JsonTypeRegister.AddIgnoreClass(typeof(Sprite));
            JsonTypeRegister.AddIgnoreClass(typeof(Texture2D));
        }
        public void TestMethod_UnityTest()
        {
            JsonTypeRegister.BindType(typeof(Vector3), new xuexueJsonClass("x", "y", "z")
            {
                defaultFieldConstraint = false
            });
            JsonTypeRegister.AddIgnoreClass(typeof(GameObject));

            TestLitJson4 tlj4 = new TestLitJson4()
            {
                a = 12
            };
            string       str4   = JsonMapper.ToJson(tlj4);
            TestLitJson4 tlj4_1 = JsonMapper.ToObject <TestLitJson4>(str4);

            Assert.IsTrue(tlj4.a == tlj4_1.a);

            float        t1b = 2;
            TestLitJson3 t1  = new TestLitJson3()
            {
                a = 1, b = t1b, c = 3, dt = DateTime.Now, testpriority2 = 5
            };
            TestLitJson3 t2 = new TestLitJson3()
            {
                a = 5, b = 6, c = 7
            };

            string       str   = JsonMapper.ToJson(t1);
            TestLitJson3 rest1 = JsonMapper.ToObject <TestLitJson3>(str);

            Assert.IsTrue(rest1.testpriority2 == 5);

            JsonMapper.OverrideObject(str, t2);
            Assert.IsTrue(t2.a == t1.a);
            Assert.IsTrue(t2.b == t1.b);
            Assert.IsTrue(t2.c == 7);
            Assert.IsTrue(t2.dt.ToShortDateString() == t1.dt.ToShortDateString());

            Vector3 v3   = new Vector3(1, 2, 3);
            Vector3 v31  = new Vector3(2, 2, 2);
            Vector3 v3_2 = JsonMapper.ToObject <Vector3>(JsonMapper.ToJson(v3));

            Assert.IsTrue(v3.x == v3_2.x);
            Assert.IsTrue(v3.y == v3_2.y);
            Assert.IsTrue(v3.z == v3_2.z);

            //string str = JsonMapper.ToJson(v3);
        }