Exemplo n.º 1
0
        public void ToJson()
        {
            var target = new Target()
            {
                Targets =
                {
                    new Target1(),
                    new Target2(),
                    new Target3()
                }
            };

            var serializer = JsonSerializer.CreateDefault(JsonNetUtility.DefaultSettings);

            var writer = new ConvertPropertyNameWriter(new Dictionary <string, string>
            {
                { "$type", "type" }
            });

            serializer.Serialize(writer, target);

            string result = writer.TextWriter.ToString();

            result = JsonNetUtility.Format(result);

            Assert.Pass(result);
        }
        public void FromJson()
        {
            var target = new Target()
            {
                Targets =
                {
                    new Target1(),
                    new Target2()
                }
            };

            var binder = new ConvertTypeNameBinder();
            JsonSerializerSettings settings = JsonNetUtility.CreateDefault();

            settings.SerializationBinder = binder;

            binder.Provider.Add <Target1>("target1");
            binder.Provider.Add <Target2>("target2");

            string result  = JsonNetUtility.ToJson(target, settings);
            var    result2 = JsonNetUtility.FromJson <Target>(result, settings);

            Assert.NotNull(result2);
            Assert.IsNotEmpty(result2.Targets);
            Assert.AreEqual(2, result2.Targets.Count);
        }
Exemplo n.º 3
0
        public void ToJsonWithBinder()
        {
            var target = new Target()
            {
                Targets =
                {
                    new Target1(),
                    new Target2(),
                    new Target3()
                }
            };

            var binder = new ConvertTypeNameBinder();
            JsonSerializerSettings settings = JsonNetUtility.CreateDefault();

            settings.SerializationBinder = binder;

            binder.Provider.Add <Target1>("target1");
            binder.Provider.Add <Target2>("target2");

            var serializer = JsonSerializer.CreateDefault(settings);

            var writer = new ConvertPropertyNameWriter(new Dictionary <string, string>
            {
                { "$type", "type" }
            });

            serializer.Serialize(writer, target);

            string result = writer.TextWriter.ToString();

            result = JsonNetUtility.Format(result);

            Assert.Pass(result);
        }
        public void ToJsonAbstract()
        {
            var target = new Target3();

            string actual   = JsonNetUtility.ToJson((object)target);
            string expected = Resources.Load <TextAsset>("data0_1").text;

            Assert.AreEqual(expected, actual);
        }
        public void ToJsonGeneric()
        {
            var target = new Target3();

            string actual   = JsonNetUtility.ToJson(target);
            string expected = Resources.Load <TextAsset>("data0").text;

            Assert.AreEqual(expected, actual);
        }
        public void Format()
        {
            string compact0  = Resources.Load <TextAsset>("data1").text;
            string readable0 = Resources.Load <TextAsset>("data2").text;

            string readable1 = JsonNetUtility.Format(compact0);
            string compact1  = JsonNetUtility.Format(readable0, false);

            Assert.AreEqual(readable0, readable1);
            Assert.AreEqual(compact0, compact1);
        }
        public void FromJson()
        {
            string text    = Resources.Load <TextAsset>("data1").text;
            var    target  = JsonNetUtility.FromJson <Target3>(text);
            var    target2 = (Target3)JsonNetUtility.FromJson(text, typeof(Target3));

            Assert.AreEqual(1515, target.IntValue);
            Assert.AreEqual(false, target.BoolValue);
            Assert.AreEqual(5.5, target.FloatValue);
            Assert.AreEqual(1515, target2.IntValue);
            Assert.AreEqual(false, target2.BoolValue);
            Assert.AreEqual(5.5, target2.FloatValue);
        }
Exemplo n.º 8
0
        public void FromJsonWithBinder()
        {
            var target = new Target()
            {
                Targets =
                {
                    new Target1(),
                    new Target2(),
                    new Target3()
                }
            };

            var binder = new ConvertTypeNameBinder();
            JsonSerializerSettings settings = JsonNetUtility.CreateDefault();

            settings.SerializationBinder = binder;

            binder.Provider.Add <Target1>("target1");
            binder.Provider.Add <Target2>("target2");

            var serializer = JsonSerializer.CreateDefault(settings);

            var writer = new ConvertPropertyNameWriter(new Dictionary <string, string>
            {
                { "$type", "type" }
            });

            serializer.Serialize(writer, target);

            string result = writer.TextWriter.ToString();

            Assert.IsNotEmpty(result);

            var reader = new ConvertPropertyNameReader(new Dictionary <string, string>
            {
                { "type", "$type" }
            }, result);

            var result2 = serializer.Deserialize <Target>(reader);

            Assert.NotNull(result2);
            Assert.IsNotEmpty(result2.Targets);
            Assert.AreEqual(3, result2.Targets.Count);
        }
        public void ToJson()
        {
            var target = new Target()
            {
                Targets =
                {
                    new Target1(),
                    new Target2()
                }
            };

            var binder = new ConvertTypeNameBinder();
            JsonSerializerSettings settings = JsonNetUtility.CreateDefault();

            settings.SerializationBinder = binder;

            binder.Provider.Add <Target1>("target1");
            binder.Provider.Add <Target2>("target2");

            string result = JsonNetUtility.ToJson(target, settings, true);

            Assert.Pass(result);
        }