Ejemplo n.º 1
0
        public void Missing_Discriminator_Breaks_Child_Deserialization()
        {
            var mappings = new FluentMappings();
            var sett     = new JsonSerializerSettings {
                ContractResolver = mappings.ContractResolver, Binder = mappings.Binder
            };

            mappings.MapClass <Parent>(cm => cm.SetDiscriminator("Parent"));
            mappings.MapClass <Child>(cm => cm.SetDiscriminator("Child"));
            var json = JsonConvert.SerializeObject(new Container()
            {
                Object = new Child()
                {
                    ChildString = "Test"
                }
            }, sett);

            // kill the child map
            mappings = new FluentMappings();
            sett     = new JsonSerializerSettings {
                ContractResolver = mappings.ContractResolver, Binder = mappings.Binder
            };
            mappings.MapClass <Parent>(cm => cm.SetDiscriminator("Parent"));

            Assert.Throws <JsonSerializationException>(() => JsonConvert.DeserializeObject <Container>(json, sett));
        }
Ejemplo n.º 2
0
        public void Discriminator_Should_Be_Written_For_Array()
        {
            var mappings = new FluentMappings();
            var sett     = new JsonSerializerSettings {
                ContractResolver = mappings.ContractResolver, Binder = mappings.Binder
            };

            mappings.MapClass <Parent>(cm => cm.SetDiscriminator("Parent"));
            mappings.MapClass <Child>(cm => cm.SetDiscriminator("Child"));
            var json = JsonConvert.SerializeObject(new Parent[] { new Child(), new Child() }, sett);

            Console.WriteLine(json);
            var linq = JsonConvert.DeserializeObject <JArray>(json);

            Assert.Equal("Child", linq[0]["$type"]);
        }
Ejemplo n.º 3
0
        public void Discriminator_Should_Be_Written_For_Child_When_Container_Is_Mapped()
        {
            var mappings = new FluentMappings();
            var sett     = new JsonSerializerSettings {
                ContractResolver = mappings.ContractResolver, Binder = mappings.Binder
            };

            mappings.MapClass <Container>(cm => { });
            mappings.MapClass <Parent>(cm => cm.SetDiscriminator("Parent"));
            mappings.MapClass <Child>(cm => cm.SetDiscriminator("Child"));
            var json = JsonConvert.SerializeObject(new Container()
            {
                Object = new Child()
            }, sett);
            var linq = JsonConvert.DeserializeObject <JObject>(json);

            Assert.Equal("Child", linq["Object"]["$type"]);
        }
Ejemplo n.º 4
0
        public void Can_Find_Member_When_Parent_Renames()
        {
            var wrapped  = new CamelCasePropertyNamesContractResolver();
            var mappings = new FluentMappings(wrapped);

            mappings.MapClass <Obj>(cm => cm.MapMember(x => x.String, x => x.SetConverter(null)));
            var contract = (JsonObjectContract)mappings.ContractResolver.ResolveContract(typeof(Obj));

            Assert.Equal(1, contract.Properties.Count); // ensures that we didn't make our own second property by accident
        }
Ejemplo n.º 5
0
        public void Discriminator_Enables_Child_Deserialization_Into_Parent_Reference()
        {
            var mappings = new FluentMappings();
            var sett     = new JsonSerializerSettings {
                ContractResolver = mappings.ContractResolver, Binder = mappings.Binder
            };

            mappings.MapClass <Parent>(cm => cm.SetDiscriminator("Parent"));
            mappings.MapClass <Child>(cm => cm.SetDiscriminator("Child"));
            var json = JsonConvert.SerializeObject(new Container()
            {
                Object = new Child()
                {
                    ChildString = "Test"
                }
            }, sett);
            var rt = JsonConvert.DeserializeObject <Container>(json, sett);

            Assert.IsType <Child>(rt.Object);
            Assert.Equal("Test", ((Child)rt.Object).ChildString);
        }
Ejemplo n.º 6
0
        public void Custom_Converter_Should_Read_Single_Property()
        {
            var mappings = new FluentMappings();

            mappings.MapClass <Obj>(cm => cm.MapMember(mm => mm.String, mm => mm.SetConverter(new TestConverter())));
            var sett = new JsonSerializerSettings {
                ContractResolver = mappings.ContractResolver, Binder = mappings.Binder
            };

            var json = "{ \"String\": \"Hello World\" }";
            var obj  = JsonConvert.DeserializeObject <Obj>(json, sett);

            Assert.Equal(TestConverter.ReadRepl, obj.String);
        }
Ejemplo n.º 7
0
        public void Class_Constructs_With_Custom_Constructor()
        {
            var mappings = new FluentMappings();

            mappings.MapClass <Custom>(x => x.MapCreator(t => new Custom(t.String)));

            var sett = new JsonSerializerSettings {
                ContractResolver = mappings.ContractResolver, Binder = mappings.Binder
            };
            var inst = new Custom(5);
            var json = JsonConvert.SerializeObject(inst, sett);

            Console.WriteLine(json);
            var newinst = JsonConvert.DeserializeObject <Custom>(json, sett);

            Assert.Equal(inst.String, newinst.String);
        }
Ejemplo n.º 8
0
        public void Can_Ignore_Property()
        {
            var mappings = new FluentMappings();

            mappings.MapClass <Obj>(cm => cm.UnmapMember(x => x.String));
            var sett = new JsonSerializerSettings {
                ContractResolver = mappings.ContractResolver, Binder = mappings.Binder
            };

            var obj = new Obj {
                String = "Test"
            };
            var json = JsonConvert.SerializeObject(obj, sett);
            var jobj = JsonConvert.DeserializeObject <JObject>(json);

            Assert.Empty(jobj.Properties());
        }
Ejemplo n.º 9
0
        public void Custom_Converter_Should_Write_Entire_Type()
        {
            var mappings = new FluentMappings();

            mappings.MapClass <Obj>(cm => cm.SetConverter(new TestConverter()));
            var sett = new JsonSerializerSettings {
                ContractResolver = mappings.ContractResolver, Binder = mappings.Binder
            };

            var obj = new Obj {
                String = "Hello World"
            };
            var json = JsonConvert.SerializeObject(obj, sett);
            var str  = JsonConvert.DeserializeObject <string>(json); // no sett

            Assert.Equal(TestConverter.WriteRepl, str);
        }
Ejemplo n.º 10
0
        public void Custom_Converter_Should_Write_Single_Property()
        {
            var mappings = new FluentMappings();

            mappings.MapClass <Obj>(cm => cm.MapMember(mm => mm.String, mm => mm.SetConverter(new TestConverter())));
            var sett = new JsonSerializerSettings {
                ContractResolver = mappings.ContractResolver, Binder = mappings.Binder
            };

            var obj = new Obj {
                String = "Hello World"
            };
            var json = JsonConvert.SerializeObject(obj, sett);

            Console.WriteLine(json);
            Assert.Contains(TestConverter.WriteRepl, json);
            Assert.DoesNotContain("Hello World", json);
        }
Ejemplo n.º 11
0
        public void Can_Roundtrip_Public_Property()
        {
            var wrapped  = new CamelCasePropertyNamesContractResolver();
            var mappings = new FluentMappings(wrapped);

            mappings.MapClass <Obj>(cm =>
            {
                cm.UnmapAll();
                cm.MapMember(x => x.String, x => x.SetName("string"));
            });

            var obj = new Obj()
            {
                String = "testing 1-2-3"
            };
            var sett = new JsonSerializerSettings {
                ContractResolver = mappings.ContractResolver
            };

            var json  = JsonConvert.SerializeObject(obj, sett);
            var deser = JsonConvert.DeserializeObject <Obj>(json, sett);

            Assert.Equal(obj.String, deser.String);
        }
Ejemplo n.º 12
0
        public void Can_Roundtrip_Private_Field()
        {
            var wrapped  = new CamelCasePropertyNamesContractResolver();
            var mappings = new FluentMappings(wrapped);

            mappings.MapClass <Properties>(cm =>
            {
                cm.UnmapAll();
                cm.MapMember(x => x._string, x => x.SetName("String"));
            });
            var contract = (JsonObjectContract)mappings.ContractResolver.ResolveContract(typeof(Properties));

            Assert.Equal(1, contract.Properties.Count); // ensures that we didn't make our own second property by accident
            Assert.Equal(nameof(_string), contract.Properties[0].UnderlyingName);

            _string = "Testing 1-2-3";
            var sett = new JsonSerializerSettings {
                ContractResolver = mappings.ContractResolver
            };
            var json  = JsonConvert.SerializeObject(this, sett);
            var deser = JsonConvert.DeserializeObject <Properties>(json, sett);

            Assert.Equal(_string, deser._string);
        }