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);
        }
Beispiel #2
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);
        }
Beispiel #3
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);
        }