Example #1
0
        public void Does_call_RawSerializeFn_for_toplevel_types()
        {
            JsConfig <ICar> .RawSerializeFn = SerializeCar;

            var luxaryParent = new Parent()
            {
                Car = new LuxaryCar()
                {
                    Sunroof = "Big"
                }
            };
            var cheapParent = new Parent()
            {
                Car = new CheapCar()
                {
                    HasCupHolder = true
                }
            };

            // Works when ICar is a child
            var luxaryParentJson = luxaryParent.ToJson();
            var cheapParentJson  = cheapParent.ToJson();

            Assert.That(luxaryParentJson, Is.Not.StringContaining("__type"));
            Assert.That(cheapParentJson, Is.Not.StringContaining("__type"));

            ICar luxary = new LuxaryCar()
            {
                Sunroof = "Big"
            };
            ICar cheap = new CheapCar()
            {
                HasCupHolder = true
            };

            // ToJson() loses runtime cast of interface type, to keep it we need to specify it on call-site
            var luxaryJson = JsonSerializer.SerializeToString(luxary, typeof(ICar));
            var cheapJson  = JsonSerializer.SerializeToString(cheap, typeof(ICar));

            Assert.That(luxaryJson, Is.Not.StringContaining("__type"));
            Assert.That(cheapJson, Is.Not.StringContaining("__type"));

            JsConfig.Reset();
        }
        public void Does_call_RawSerializeFn_for_toplevel_types()
        {
            JsConfig<ICar>.RawSerializeFn = SerializeCar;

            var luxaryParent = new Parent() { Car = new LuxaryCar() { Sunroof = "Big" } };
            var cheapParent = new Parent() { Car = new CheapCar() { HasCupHolder = true } };

            // Works when ICar is a child
            var luxaryParentJson = luxaryParent.ToJson();
            var cheapParentJson = cheapParent.ToJson();

            Assert.That(luxaryParentJson, Is.Not.StringContaining("__type"));
            Assert.That(cheapParentJson, Is.Not.StringContaining("__type"));

            ICar luxary = new LuxaryCar() { Sunroof = "Big" };
            ICar cheap = new CheapCar() { HasCupHolder = true };

            // ToJson() loses runtime cast of interface type, to keep it we need to specify it on call-site
            var luxaryJson = JsonSerializer.SerializeToString(luxary, typeof(ICar));
            var cheapJson = JsonSerializer.SerializeToString(cheap, typeof(ICar));

            Assert.That(luxaryJson, Is.Not.StringContaining("__type"));
            Assert.That(cheapJson, Is.Not.StringContaining("__type"));

            JsConfig.Reset();
        }