Ejemplo n.º 1
0
        public void Basic_Successful()
        {
            var serializer = new JsonSerializer();
            var obj        = new ObjectWithBasicProps
            {
                StringProp = "stringValue",
                IntProp    = 42,
                DoubleProp = 6.0,
                BoolProp   = true,
                EnumProp   = TestEnum.BasicEnumValue,
                MappedProp = 4
            };
            JsonValue expected = new JsonObject
            {
                { "StringProp", "stringValue" },
                { "IntProp", 42 },
                { "DoubleProp", 6 },
                { "BoolProp", true },
                { "EnumProp", 1 },
                { "MapToMe", 4 }
            };
            var actual = serializer.Serialize(obj);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 2
0
        public void GreedySerialization()
        {
            JsonValue expected = new JsonObject
            {
                { "#Type", typeof(ObjectWithBasicProps).AssemblyQualifiedName },
                { "StringProp", "string" },
                { "IntProp", 5 },
                { "DoubleProp", 10 },
                { "BoolProp", true },
                { "EnumProp", 2 },
                { "MapToMe", 1 }
            };
            var obj = new ObjectWithBasicProps
            {
                BoolProp   = true,
                DoubleProp = 10,
                EnumProp   = TestEnum.EnumValueWithDescription,
                IgnoreProp = "ignore",
                IntProp    = 5,
                MappedProp = 1,
                StringProp = "string"
            };
            var serializer = new JsonSerializer {
                Options = { TypeNameSerializationBehavior = TypeNameSerializationBehavior.Auto }
            };
            var actual = serializer.Serialize <object>(obj);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 3
0
        public void NameTransformation()
        {
            var serializer = new JsonSerializer
            {
                Options =
                {
                    SerializationNameTransform = s => new string(s.Reverse().ToArray())
                }
            };
            var obj = new ObjectWithBasicProps
            {
                StringProp = "stringValue",
                IntProp    = 42,
                BoolProp   = true,
                MappedProp = 4
            };
            JsonValue expected = new JsonObject
            {
                { "porPgnirtS", "stringValue" },
                { "porPtnI", 42 },
                { "porPlooB", true },
                { "MapToMe", 4 }
            };
            var actual = serializer.Serialize(obj);

            serializer.Options = null;
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 4
0
        public void GreedySerializationWithoutTypeName()
        {
            JsonValue expected = new JsonObject
            {
                { "StringProp", "string" },
                { "IntProp", 5 },
                { "DoubleProp", 10 },
                { "BoolProp", true },
                { "EnumProp", 2 },
                { "MapToMe", 1 }
            };
            var obj = new ObjectWithBasicProps
            {
                BoolProp   = true,
                DoubleProp = 10,
                EnumProp   = TestEnum.EnumValueWithDescription,
                IgnoreProp = "ignore",
                IntProp    = 5,
                MappedProp = 1,
                StringProp = "string"
            };
            var serializer = new JsonSerializer();
            var actual     = serializer.Serialize <object>(obj);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 5
0
        public void CustomOptions_SerializesDefaultValues()
        {
            var serializer = new JsonSerializer {
                Options = { EncodeDefaultValues = true }
            };
            // DoubleProp remains default
            var obj = new ObjectWithBasicProps
            {
                StringProp = "stringValue",
                IntProp    = 42,
                BoolProp   = true
            };
            JsonValue expected = new JsonObject
            {
                { "StringProp", "stringValue" },
                { "IntProp", 42 },
                { "DoubleProp", 0 },
                { "BoolProp", true },
                { "EnumProp", 0 },
                { "FlagsEnumProp", 0 },
                { "MapToMe", 0 }
            };
            var actual = serializer.Serialize(obj);

            serializer.Options = null;
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 6
0
        public void Fields()
        {
            var serializer = new JsonSerializer {
                Options = { AutoSerializeFields = true }
            };
            var obj = new ObjectWithBasicProps
            {
                StringProp = "stringValue",
                IntProp    = 42,
                DoubleProp = 6.0,
                BoolProp   = true,
                EnumProp   = TestEnum.BasicEnumValue,
                MappedProp = 4,
                Field      = "test"
            };
            JsonValue expected = new JsonObject
            {
                { "StringProp", "stringValue" },
                { "IntProp", 42 },
                { "DoubleProp", 6 },
                { "BoolProp", true },
                { "EnumProp", 1 },
                { "MapToMe", 4 },
                { "Field", "test" }
            };
            var actual = serializer.Serialize(obj);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 7
0
        public void BasicWithNamedMultivalueFlagsEnumWithDescription_Successful()
        {
            var serializer = new JsonSerializer
            {
                Options =
                {
                    EnumSerializationFormat = EnumSerializationFormat.AsName,
                    FlagsEnumSeparator      = " | "
                }
            };
            var obj = new ObjectWithBasicProps
            {
                StringProp    = "stringValue",
                IntProp       = 42,
                DoubleProp    = 6.0,
                BoolProp      = true,
                EnumProp      = TestEnum.EnumValueWithDescription,
                FlagsEnumProp = (FlagsEnum)3,
                MappedProp    = 4
            };
            JsonValue expected = new JsonObject
            {
                { "StringProp", "stringValue" },
                { "IntProp", 42 },
                { "DoubleProp", 6 },
                { "BoolProp", true },
                { "EnumProp", "enum_value_with_description" },
                { "FlagsEnumProp", "BasicEnumValue | enum_value_with_description" },
                { "MapToMe", 4 }
            };
            var actual = serializer.Serialize(obj);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 8
0
        public void BasicWithNamedFlagsEnum_Successful()
        {
            var serializer = new JsonSerializer {
                Options = { EnumSerializationFormat = EnumSerializationFormat.AsName }
            };
            var obj = new ObjectWithBasicProps
            {
                StringProp    = "stringValue",
                IntProp       = 42,
                DoubleProp    = 6.0,
                BoolProp      = true,
                EnumProp      = TestEnum.BasicEnumValue,
                FlagsEnumProp = FlagsEnum.BasicEnumValue,
                MappedProp    = 4
            };
            JsonValue expected = new JsonObject
            {
                { "StringProp", "stringValue" },
                { "IntProp", 42 },
                { "DoubleProp", 6 },
                { "BoolProp", true },
                { "EnumProp", "BasicEnumValue" },
                { "FlagsEnumProp", "BasicEnumValue" },
                { "MapToMe", 4 }
            };
            var actual = serializer.Serialize(obj);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 9
0
        public void Basic_Successful()
        {
            var serializer = new JsonSerializer {
                Options = { EnumSerializationFormat = EnumSerializationFormat.AsInteger }
            };
            var json = new JsonObject
            {
                { "StringProp", "stringValue" },
                { "IntProp", 42 },
                { "DoubleProp", 6 },
                { "BoolProp", true },
                { "EnumProp", 1 },
                { "MapToMe", 4 }
            };
            var expected = new ObjectWithBasicProps
            {
                StringProp = "stringValue",
                IntProp    = 42,
                DoubleProp = 6.0,
                BoolProp   = true,
                EnumProp   = TestEnum.BasicEnumValue,
                MappedProp = 4
            };
            var actual = serializer.Deserialize <ObjectWithBasicProps>(json);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 10
0
        public void PrepopulatedReadonlyDictionaryAutoprop()
        {
            var serializer = new JsonSerializer
            {
                Options =
                {
                    PropertySelectionStrategy = PropertySelectionStrategy.ReadAndWrite
                }
            };
            var json = new JsonObject
            {
                ["ReadOnlyDictionaryProp"] = new JsonObject
                {
                    ["key1"] = 1,
                    ["key2"] = 2
                }
            };
            var expected = new ObjectWithBasicProps
            {
                ReadOnlyDictionaryProp =
                {
                    ["key1"] = 1,
                    ["key2"] = 2
                }
            };

            var actual = serializer.Deserialize <ObjectWithBasicProps>(json);

            Assert.IsTrue(expected.ReadOnlyDictionaryProp.SequenceEqual(actual.ReadOnlyDictionaryProp));
        }
Ejemplo n.º 11
0
        public void GreedySerializationDisabled()
        {
            var expected = JsonValue.Null;
            var obj      = new ObjectWithBasicProps
            {
                BoolProp   = true,
                DoubleProp = 10,
                EnumProp   = TestEnum.EnumValueWithDescription,
                IgnoreProp = "ignore",
                IntProp    = 5,
                MappedProp = 1,
                StringProp = "string"
            };
            var serializer = new JsonSerializer {
                Options = { OnlyExplicitProperties = true }
            };
            var actual = serializer.Serialize <object>(obj);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 12
0
        public void DefaultOptions_IgnoresDefaultValues()
        {
            var serializer = new JsonSerializer();
            // DoubleProp remains default
            var obj = new ObjectWithBasicProps
            {
                StringProp = "stringValue",
                IntProp    = 42,
                BoolProp   = true
            };
            JsonValue expected = new JsonObject
            {
                { "StringProp", "stringValue" },
                { "IntProp", 42 },
                { "BoolProp", true }
            };
            var actual = serializer.Serialize(obj);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 13
0
        public void DefaultOptions_IgnoresExtraProperties()
        {
            var serializer = new JsonSerializer();
            var json       = new JsonObject
            {
                { "StringProp", "stringValue" },
                { "IntProp", 42 },
                { "UnknownProp", "string" },
                { "DoubleProp", 6 },
                { "BoolProp", true },
                { "OtherProp", Math.PI }
            };
            var expected = new ObjectWithBasicProps
            {
                StringProp = "stringValue",
                IntProp    = 42,
                DoubleProp = 6.0,
                BoolProp   = true
            };
            var actual = serializer.Deserialize <ObjectWithBasicProps>(json);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 14
0
 public void CustomOptions_ThrowsTypeDoesNotContainPropertyException()
 {
     Assert.Throws <TypeDoesNotContainPropertyException>(() =>
     {
         var serializer = new JsonSerializer();
         try
         {
             serializer.Options = new JsonSerializerOptions
             {
                 InvalidPropertyKeyBehavior = InvalidPropertyKeyBehavior.ThrowException
             };
             var json = new JsonObject
             {
                 { "StringProp", "stringValue" },
                 { "IntProp", 42 },
                 { "UnknownProp", "string" },
                 { "DoubleProp", 6 },
                 { "BoolProp", true },
                 { "OtherProp", Math.PI }
             };
             var expected = new ObjectWithBasicProps
             {
                 StringProp = "stringValue",
                 IntProp    = 42,
                 DoubleProp = 6.0,
                 BoolProp   = true
             };
             var actual = serializer.Deserialize <ObjectWithBasicProps>(json);
             Assert.Fail("Did not throw exception.");
         }
         finally
         {
             serializer.Options = null;
         }
     });
 }