예제 #1
0
        public static void Serializer_deserialize_empty_json_into_the_only_child_type_that_has_a_default_constructor()
        {
            var atkinsJson = "{}";

            var atkins = new ObcJsonSerializer(typeof(DietJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).Deserialize <Diet>(atkinsJson) as Atkins;

            atkins.Should().NotBeNull();
        }
        public static void SerializeObject_without_type_serializes_to_json_using_MinimalSerializerSettings()
        {
            // If Minimal is being used then the null Nickname property won't be serialized
            var dog = new Dog(5, "spud", FurColor.Brindle);

            var json = new ObcJsonSerializer(typeof(MinimalFormatJsonSerializationConfiguration <AnimalJsonSerializationConfiguration>).ToJsonSerializationConfigurationType()).SerializeToString(dog);

            json.Should().Be("{\"name\":\"spud\",\"furColor\":\"brindle\",\"dogTag\":\"my name is spud\",\"age\":5}");
        }
예제 #3
0
        public static void Serializer_deserialize_type_where_constructor_does_not_throw_exception_when_another_candidate_has_constructor_that_does_throw_exception()
        {
            var sometimesThrowsJson = "{\"triggerNumber\":123456}";

            var doesNotThrow =
                new ObcJsonSerializer(typeof(TypesToRegisterJsonSerializationConfiguration <SometimesThrows>).ToJsonSerializationConfigurationType()).Deserialize <SometimesThrows>(sometimesThrowsJson) as DoesNotThrow;

            doesNotThrow.Should().NotBeNull();
            doesNotThrow.TriggerNumber.Should().Be(123456);
        }
예제 #4
0
        public static void Serializer_serializes_TwoWay_bindable_type_that_embeds_a_OneWay_bindable_type_using_specified_serializer()
        {
            var whale = new Whale("willy", new LowCalorie(50000));

            var expectedWhaleJson = Invariant($"{{\r\n  \"name\": \"willy\",\r\n  \"diet\": {{\r\n    \"maxCalories\": 50000,\r\n    \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+LowCalorie, OBeautifulCode.Serialization.Json.Test\"\r\n  }},\r\n  \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+Whale, OBeautifulCode.Serialization.Json.Test\"\r\n}}");

            var actualWhaleJson = new ObcJsonSerializer(typeof(SeaCreatureJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).SerializeToString(whale);

            actualWhaleJson.Should().Be(expectedWhaleJson);
        }
예제 #5
0
        public static void Serializer_deserialize_type_where_constructor_parameter_is_different_type_than_corresponding_property_but_is_assignable_from_that_property_type()
        {
            var familyJson = "{\"firstNames\": [\"joe\",\"jane\",\"jackie\"]}";

            var family             = new ObcJsonSerializer(typeof(TypesToRegisterJsonSerializationConfiguration <Family>).ToJsonSerializationConfigurationType()).Deserialize <Family>(familyJson);
            var expectedFirstNames = new[] { "joe", "jane", "jackie" };

            family.Should().NotBeNull();
            expectedFirstNames.Should().BeEquivalentTo(family.FirstNames);
        }
예제 #6
0
        public static void Serializer_serializes_OneWay_bindable_type_that_embeds_a_TwoWay_bindable_type_using_specified_serializer()
        {
            var seafoodDiet = new SeafoodDiet(new Salmon(SeaCreatureSize.Medium, "red"), 345);

            var expectedSeafoodDietJson = Invariant($"{{\r\n  \"seaCreature\": {{\r\n    \"color\": \"red\",\r\n    \"size\": \"medium\",\r\n    \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+Salmon, OBeautifulCode.Serialization.Json.Test\"\r\n  }},\r\n  \"amount\": 345,\r\n  \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+SeafoodDiet, OBeautifulCode.Serialization.Json.Test\"\r\n}}");

            var actualSeafoodDietJson = new ObcJsonSerializer(typeof(SeaCreatureJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).SerializeToString(seafoodDiet);

            actualSeafoodDietJson.Should().Be(expectedSeafoodDietJson);
        }
        public static void DeserializeObject_with_type_deserialize_json_using_MinimalSerializerSettings()
        {
            // If Minimal is being used then empty JSON string will deserialize into NoLighting
            // otherwise, out-of-the-box json.net will create an anonymous object
            var lightingJson = "{}";

            var lighting = new ObcJsonSerializer(typeof(MinimalFormatJsonSerializationConfiguration <LightingJsonSerializationConfiguration>).ToJsonSerializationConfigurationType()).Deserialize(lightingJson, typeof(Lighting)) as NoLighting;

            lighting.Should().NotBeNull();
        }
예제 #8
0
        public static void Serializer_deserialize_into_concrete_type_where_multiple_inherited_types_have_the_same_properties_and_abstract_type_is_marked_TwoWay_bindable()
        {
            var salmonJson = "{\r\n  \"color\": \"brown\",\r\n  \"size\": \"medium\",\r\n  \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+Salmon, OBeautifulCode.Serialization.Json.Test\"\r\n}";

            var salmon = new ObcJsonSerializer(typeof(SeaCreatureJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).Deserialize <Salmon>(salmonJson);

            salmon.Should().NotBeNull();
            salmon.Color.Should().Be("brown");
            salmon.Size.Should().Be(SeaCreatureSize.Medium);
        }
예제 #9
0
        public static void Serializer_serializes_TwoWay_bindable_type_that_embeds_a_TwoWay_bindable_type_using_specified_serializer()
        {
            var tuna  = new Tuna(SeaCreatureSize.Medium, "black");
            var shark = new Shark("sammy", tuna);

            var expectedSharkJson = Invariant($"{{\r\n  \"name\": \"sammy\",\r\n  \"likesToEat\": {{\r\n    \"color\": \"black\",\r\n    \"size\": \"medium\",\r\n    \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+Tuna, OBeautifulCode.Serialization.Json.Test\"\r\n  }},\r\n  \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+Shark, OBeautifulCode.Serialization.Json.Test\"\r\n}}");

            var actualSharkJson = new ObcJsonSerializer(typeof(SeaCreatureJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).SerializeToString(shark);

            actualSharkJson.Should().Be(expectedSharkJson);
        }
        public static void SerializeObject_without_type_serializes_to_json_using_DefaultSerializerSettings()
        {
            // If Default is being used then there should be new lines
            var dog = new Dog(5, "spud", FurColor.Brindle);

            var json = new ObcJsonSerializer(typeof(AnimalJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).SerializeToString(dog);

            var expected = Invariant($"{{\r\n  \"name\": \"spud\",\r\n  \"furColor\": \"brindle\",\r\n  \"dogTag\": \"my name is spud\",\r\n  \"nickname\": null,\r\n  \"age\": 5,\r\n  \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.Dog, OBeautifulCode.Serialization.Json.Test\"\r\n}}");

            json.Should().Be(expected);
        }
예제 #11
0
        public static void Serializer_deserialize_inherited_types_when_null_property_is_not_included_in_json()
        {
            var json = "{\"float\":.2,\"int32\":50}";

            var inheritedType3 = new ObcJsonSerializer(typeof(BaseInterfaceJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).Deserialize <IBaseInterface>(json) as InheritedType3;

            inheritedType3.Should().NotBeNull();
            inheritedType3.Int32.Should().Be(50);
            inheritedType3.Float.Should().Be(.2f);
            inheritedType3.Diet.Should().BeNull();
        }
예제 #12
0
        public static void Serializer_deserialize_to_type_having_all_constructor_parameters_in_json_when_another_types_constructor_has_all_the_same_parameters_but_one_additional_one_which_is_not_in_the_json()
        {
            var dogJson = "{\"name\":\"Barney\",\"furColor\":\"brindle\",\"age\":10}";

            var dog = new ObcJsonSerializer(typeof(AnimalJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).Deserialize <Animal>(dogJson) as Dog;

            dog.Should().NotBeNull();
            dog.Name.Should().Be("Barney");
            dog.Age.Should().Be(10);
            dog.FurColor.Should().Be(FurColor.Brindle);
            dog.DogTag.Should().Be("my name is Barney");
        }
예제 #13
0
        public static void Serializer_serializes_and_deserialize_SecureString_types()
        {
            var serializedValue = "{" + Environment.NewLine +
                                  "  \"secure\": \"Password\"" + Environment.NewLine +
                                  "}";

            var deserialized = new ObcJsonSerializer().Deserialize <SecureStringTest>(serializedValue);

            var result = new ObcJsonSerializer().SerializeToString(deserialized);

            result.Should().Be(serializedValue);
        }
예제 #14
0
        public static void Serializer_deserialize_TwoWay_bindable_type_into_abstract_type_when_concrete_type_embeds_a_OneWay_bindable_type_using_specified_serializer()
        {
            var whaleJson = "{\r\n  \"name\": \"willy\",\r\n  \"diet\": {\r\n    \"maxCalories\": 50000\r\n  },\r\n  \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+Whale, OBeautifulCode.Serialization.Json.Test\"\r\n}";

            var whale = new ObcJsonSerializer(typeof(SeaCreatureJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).Deserialize <SeaCreature>(whaleJson) as Whale;

            whale.Should().NotBeNull();
            whale.Name.Should().Be("willy");
            whale.Diet.Should().NotBeNull();
            whale.Diet.Should().BeOfType <LowCalorie>();
            ((LowCalorie)whale.Diet).MaxCalories.Should().Be(50000);
        }
        public static void DeserializeObject_with_type_deserialize_json_using_DefaultSerializerSettings()
        {
            // If Default is being used then strict constructor matching will result in a Dog and not a Mouse
            var dogJson = "{\"name\":\"Barney\",\"furColor\":\"brindle\",\"age\":10}";

            var dog = new ObcJsonSerializer(typeof(AnimalJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).Deserialize(dogJson, typeof(Animal)) as Dog;

            dog.Should().NotBeNull();
            dog.Name.Should().Be("Barney");
            dog.Age.Should().Be(10);
            dog.FurColor.Should().Be(FurColor.Brindle);
            dog.DogTag.Should().Be("my name is Barney");
        }
예제 #16
0
        public static void Serializer_deserialize_OneWay_bindable_type_into_abstract_type_when_concrete_type_embeds_a_TwoWay_bindable_type_using_specified_serializer()
        {
            var seafoodDietJson = "{\r\n  \"seaCreature\": {\r\n    \"color\": \"red\",\r\n    \"size\": \"medium\",\r\n    \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+Salmon, OBeautifulCode.Serialization.Json.Test\"\r\n  },\r\n  \"amount\": 345\r\n}";

            var seafoodDiet = new ObcJsonSerializer(typeof(SeaCreatureJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).Deserialize <Diet>(seafoodDietJson) as SeafoodDiet;

            seafoodDiet.Should().NotBeNull();
            seafoodDiet.Amount.Should().Be(345);
            seafoodDiet.SeaCreature.Should().NotBeNull();
            seafoodDiet.SeaCreature.Should().BeOfType <Salmon>();
            ((Salmon)seafoodDiet.SeaCreature).Color.Should().Be("red");
            ((Salmon)seafoodDiet.SeaCreature).Size.Should().Be(SeaCreatureSize.Medium);
        }
예제 #17
0
        public static void Serializer_deserialize_TwoWay_bindable_type_into_abstract_type_when_concrete_type_embeds_a_TwoWay_bindable_type_using_specified_serializer()
        {
            var sharkJson = "{\r\n  \"name\": \"sammy\",\r\n  \"likesToEat\": {\r\n    \"color\": \"black\",\r\n    \"size\": \"medium\",\r\n    \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+Tuna, OBeautifulCode.Serialization.Json.Test\"\r\n  },\r\n  \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+Shark, OBeautifulCode.Serialization.Json.Test\"\r\n}";

            var shark = new ObcJsonSerializer(typeof(SeaCreatureJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).Deserialize <SeaCreature>(sharkJson) as Shark;

            shark.Should().NotBeNull();
            shark.Name.Should().Be("sammy");
            shark.LikesToEat.Should().NotBeNull();
            shark.LikesToEat.Should().BeOfType <Tuna>();
            ((Tuna)shark.LikesToEat).Size.Should().Be(SeaCreatureSize.Medium);
            ((Tuna)shark.LikesToEat).Color.Should().Be("black");
        }
예제 #18
0
        public static void Serializer_deserialize_type_with_constructor_that_has_a_property_that_is_an_inherited_type_and_is_null_in_json()
        {
            var catDietJson = "{\"cat\":{\"numberOfLives\":9,\"name\":\"Cleo\",\"age\":3}, \"diet\":null}";

            var catDiet = new ObcJsonSerializer(typeof(CatDietJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).Deserialize <CatDiet>(catDietJson);

            catDiet.Should().NotBeNull();
            catDiet.Cat.Should().NotBeNull();
            catDiet.Cat.Name.Should().Be("Cleo");
            catDiet.Cat.Age.Should().Be(3);
            catDiet.Cat.NumberOfLives.Should().Be(9);

            catDiet.Diet.Should().BeNull();
        }
예제 #19
0
        public static void Serializer_serializes_camel_cased_enumerations()
        {
            var value = new CamelCasedEnumTest()
            {
                Value = TestEnum.FirstOption,
            };

            var result = new ObcJsonSerializer().SerializeToString(value);

            var serializedValue = "{" + Environment.NewLine +
                                  "  \"value\": \"firstOption\"" + Environment.NewLine +
                                  "}";

            result.Should().Be(serializedValue);
        }
예제 #20
0
        public static void Serializer_serializes_camel_cased_properties()
        {
            var value = new CamelCasedPropertyTest()
            {
                TestName = "Hello",
            };

            var result = new ObcJsonSerializer().SerializeToString(value);

            var serializedValue = "{" + Environment.NewLine +
                                  "  \"testName\": \"Hello\"" + Environment.NewLine +
                                  "}";

            result.Should().Be(serializedValue);
        }
예제 #21
0
        public static void Serializer_deserialize_into_abstract_type_where_multiple_inherited_types_have_the_same_properties_using_type_information_written_into_json()
        {
            var salmonJson = "{\r\n  \"color\": \"brown\",\r\n  \"size\": \"medium\",\r\n  \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+Salmon, OBeautifulCode.Serialization.Json.Test\"\r\n}";

            var salmon1 = new ObcJsonSerializer(typeof(SeaCreatureJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).Deserialize <SeaCreature>(salmonJson) as Salmon;
            var salmon2 = new ObcJsonSerializer(typeof(SeaCreatureJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).Deserialize <Fish>(salmonJson) as Salmon;

            salmon1.Should().NotBeNull();
            salmon1.Color.Should().Be("brown");
            salmon1.Size.Should().Be(SeaCreatureSize.Medium);

            salmon2.Should().NotBeNull();
            salmon2.Color.Should().Be("brown");
            salmon2.Size.Should().Be(SeaCreatureSize.Medium);
        }
예제 #22
0
        public static void Serializer_deserialize_type_with_no_constructor_that_has_a_property_that_is_an_inherited_type_and_is_null_in_json()
        {
            var dogDietJson = "{\"dog\":{\"name\":\"Barney\",\"furColor\":\"brindle\",\"age\":10}, \"diet\":null}";

            var dogDiet = new ObcJsonSerializer(typeof(DogDietJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).Deserialize <DogDiet>(dogDietJson);

            dogDiet.Should().NotBeNull();

            dogDiet.Dog.Should().NotBeNull();
            dogDiet.Dog.Name.Should().Be("Barney");
            dogDiet.Dog.Age.Should().Be(10);
            dogDiet.Dog.FurColor.Should().Be(FurColor.Brindle);
            dogDiet.Dog.DogTag.Should().Be("my name is Barney");

            dogDiet.Diet.Should().BeNull();
        }
예제 #23
0
        public static void Serializer_serializes_InheritedTypes()
        {
            var value = new InheritedTypeBase[]
            {
                new InheritedType1
                {
                    Base   = "Base",
                    Child1 = "Child1",
                },
                new InheritedType2
                {
                    Base   = "my base",
                    Child2 = "my child 2",
                },
            };

            var result = new ObcJsonSerializer(typeof(InheritedTypeBaseJsonSerializationConfiguration).ToJsonSerializationConfigurationType()).SerializeToString(value);

            var serializedValue = Invariant($"[\r\n  {{\r\n    \"child1\": \"Child1\",\r\n    \"base\": \"Base\",\r\n    \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+InheritedType1, OBeautifulCode.Serialization.Json.Test\"\r\n  }},\r\n  {{\r\n    \"child2\": \"my child 2\",\r\n    \"base\": \"my base\",\r\n    \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.JsonSerializationConfigurationTest+InheritedType2, OBeautifulCode.Serialization.Json.Test\"\r\n  }}\r\n]");

            result.Should().Be(serializedValue);
        }
예제 #24
0
        public static void Serializer_roundtrips_type_with_dictionary_properties_whose_keys_are_strings_or_enums_without_manipulating_case_of_first_letter_in_string_key()
        {
            var expected = new DictionaryPropertiesTest
            {
                Names = new Dictionary <string, string>
                {
                    { "Joe", "Locks" },
                    { "sally", "fields" },
                },
                ReadOnlyNames = new ReadOnlyDictionary <string, string>(
                    new Dictionary <string, string>
                {
                    { "billy", "Bob" },
                    { "Harry", "wright" },
                }),
                ColorsByNames = new Dictionary <string, Color>
                {
                    { "billy", Color.Green },
                    { "Jean", Color.White },
                },
                NamesByColor = new Dictionary <Color, string>
                {
                    { Color.Green, "Billy" },
                    { Color.White, "jean" },
                },
            };

            var json = new ObcJsonSerializer().SerializeToString(expected);

            var actual = new ObcJsonSerializer().Deserialize <DictionaryPropertiesTest>(json);

            actual.Should().NotBeNull();
            expected.Names.Should().BeEquivalentTo(actual.Names);
            expected.ReadOnlyNames.Should().BeEquivalentTo(actual.ReadOnlyNames);
            expected.ColorsByNames.Should().BeEquivalentTo(actual.ColorsByNames);
            expected.NamesByColor.Should().BeEquivalentTo(actual.NamesByColor);
        }