Ejemplo n.º 1
0
        public void CanConvertEnum()
        {
            var obj = new ObjectWithEnum
            {
                Enums = Enums.Test2
            };

            var str         = _serializer.Serialize(obj);
            var strWithType = _serializer.Serialize(obj, new SerializerSettings()
            {
                SerializationTypeHandle = SerializationTypeHandle.All
            });

            Console.WriteLine(str);
            Console.WriteLine(strWithType);
            Assert.AreEqual("{\"Enums\":\"Test2\"}", str);
            Assert.AreEqual("{\"$type\":\"SFJsonTests.ObjectWithEnum, SFJsonTests\",\"Enums\":\"Test2\"}", strWithType);

            var strDeserialized = _deserializer.Deserialize <ObjectWithEnum>(str);

            Assert.IsTrue(strDeserialized != null);
            Assert.IsInstanceOf <Enums>(strDeserialized.Enums);
            Assert.AreEqual(obj.Enums, strDeserialized.Enums);

            var strWithTypeDeserialized = _deserializer.Deserialize <ObjectWithEnum>(strWithType);

            Assert.IsTrue(strWithTypeDeserialized != null);
            Assert.IsInstanceOf <Enums>(strWithTypeDeserialized.Enums);
            Assert.AreEqual(obj.Enums, strWithTypeDeserialized.Enums);
        }
Ejemplo n.º 2
0
        public void CanConvertEnumWithoutQuotes()
        {
            var obj = new ObjectWithEnum
            {
                Enums = Enums.Test2
            };

            var str         = "{\"Enums\":Test2}";
            var strWithType = "{\"$type\":\"SFJsonTests.ObjectWithEnum, SFJsonTests\",\"Enums\":Test2}";

            Console.WriteLine(str);
            Console.WriteLine(strWithType);
            var strDeserialized = _deserializer.Deserialize <ObjectWithEnum>(str);

            Assert.IsTrue(strDeserialized != null);
            Assert.IsInstanceOf <Enums>(strDeserialized.Enums);
            Assert.AreEqual(obj.Enums, strDeserialized.Enums);

            var strWithTypeDeserialized = _deserializer.Deserialize <ObjectWithEnum>(strWithType);

            Assert.IsTrue(strWithTypeDeserialized != null);
            Assert.IsInstanceOf <Enums>(strWithTypeDeserialized.Enums);
            Assert.AreEqual(obj.Enums, strWithTypeDeserialized.Enums);
        }