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

            var atkins = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Diet>)).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 NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Animal>), formattingKind: JsonFormattingKind.Minimal).SerializeToString(dog);

            json.Should().Be("{\"name\":\"spud\",\"furColor\":\"brindle\",\"dogTag\":\"my name is spud\",\"age\":5}");
        }
        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 NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Animal>)).SerializeToString(dog);

            json.Should().Be("{\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\": \"Naos.Serialization.Test.Dog, Naos.Serialization.Test\"\r\n}");
        }
Example #4
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 = "{\r\n  \"seaCreature\": {\r\n    \"color\": \"red\",\r\n    \"size\": \"medium\",\r\n    \"$concreteType\": \"Naos.Serialization.Test.JsonConfigurationTest+Salmon, Naos.Serialization.Test\"\r\n  },\r\n  \"amount\": 345,\r\n  \"$concreteType\": \"Naos.Serialization.Test.JsonConfigurationTest+SeafoodDiet, Naos.Serialization.Test\"\r\n}";

            var actualSeafoodDietJson = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <SeaCreature>)).SerializeToString(seafoodDiet);

            actualSeafoodDietJson.Should().Be(expectedSeafoodDietJson);
        }
Example #5
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 = "{\r\n  \"name\": \"willy\",\r\n  \"diet\": {\r\n    \"maxCalories\": 50000,\r\n    \"$concreteType\": \"Naos.Serialization.Test.JsonConfigurationTest+LowCalorie, Naos.Serialization.Test\"\r\n  },\r\n  \"$concreteType\": \"Naos.Serialization.Test.JsonConfigurationTest+Whale, Naos.Serialization.Test\"\r\n}";

            var actualWhaleJson = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <SeaCreature, Diet>)).SerializeToString(whale);

            actualWhaleJson.Should().Be(expectedWhaleJson);
        }
Example #6
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\": \"Naos.Serialization.Test.JsonConfigurationTest+Salmon, Naos.Serialization.Test\"\r\n}";

            var salmon = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <SeaCreature>)).Deserialize <Salmon>(salmonJson);

            salmon.Should().NotBeNull();
            salmon.Color.Should().Be("brown");
            salmon.Size.Should().Be(SeaCreatureSize.Medium);
        }
Example #7
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 NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <SometimesThrows>)).Deserialize <SometimesThrows>(sometimesThrowsJson) as DoesNotThrow;

            doesNotThrow.Should().NotBeNull();
            doesNotThrow.TriggerNumber.Should().Be(123456);
        }
Example #8
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 NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Family>)).Deserialize <Family>(familyJson);
            var expectedFirstNames = new[] { "joe", "jane", "jackie" };

            family.Should().NotBeNull();
            expectedFirstNames.Should().BeEquivalentTo(family.FirstNames);
        }
        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 NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Lighting>), formattingKind: JsonFormattingKind.Minimal).Deserialize(lightingJson, typeof(Lighting)) as NoLighting;

            lighting.Should().NotBeNull();
        }
        public static void SerializeObject_without_type_serializes_to_json_using_CompactSerializerSettings()
        {
            // If Compact is being used then there should be no new lines
            var dog = new Dog(5, "spud", FurColor.Brindle);

            var json = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Animal>), formattingKind: JsonFormattingKind.Compact).SerializeToString(dog);

            var expected = "{\"name\":\"spud\",\"furColor\":\"brindle\",\"dogTag\":\"my name is spud\",\"nickname\":null,\"age\":5,\"$concreteType\":\"Naos.Serialization.Test.Dog, Naos.Serialization.Test\"}";

            json.Should().Be(expected);
        }
Example #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 NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <IBaseInterface>)).Deserialize <IBaseInterface>(json) as InheritedType3;

            inheritedType3.Should().NotBeNull();
            inheritedType3.Int32.Should().Be(50);
            inheritedType3.Float.Should().Be(.2f);
            inheritedType3.Diet.Should().BeNull();
        }
Example #12
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 = "{\r\n  \"name\": \"sammy\",\r\n  \"likesToEat\": {\r\n    \"color\": \"black\",\r\n    \"size\": \"medium\",\r\n    \"$concreteType\": \"Naos.Serialization.Test.JsonConfigurationTest+Tuna, Naos.Serialization.Test\"\r\n  },\r\n  \"$concreteType\": \"Naos.Serialization.Test.JsonConfigurationTest+Shark, Naos.Serialization.Test\"\r\n}";

            var actualSharkJson = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <SeaCreature>)).SerializeToString(shark);

            actualSharkJson.Should().Be(expectedSharkJson);
        }
Example #13
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\": \"Naos.Serialization.Test.JsonConfigurationTest+Whale, Naos.Serialization.Test\"\r\n}";

            var whale = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <SeaCreature, Diet>)).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);
        }
Example #14
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 NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Animal>)).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");
        }
Example #15
0
        public static void Serializer_serializes_and_deserialize_SecureString_types()
        {
            var serializedValue = "{" + Environment.NewLine +
                                  "  \"secure\": \"Password\"" + Environment.NewLine +
                                  "}";

            var deserialized = new NaosJsonSerializer(unregisteredTypeEncounteredStrategy: UnregisteredTypeEncounteredStrategy.Attempt).Deserialize <SecureStringTest>(serializedValue);

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

            result.Should().Be(serializedValue);
        }
        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 NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Animal>)).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");
        }
Example #17
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\": \"Naos.Serialization.Test.JsonConfigurationTest+Salmon, Naos.Serialization.Test\"\r\n  },\r\n  \"amount\": 345\r\n}";

            var seafoodDiet = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <SeaCreature, Diet>)).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);
        }
Example #18
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\": \"Naos.Serialization.Test.JsonConfigurationTest+Tuna, Naos.Serialization.Test\"\r\n  },\r\n  \"$concreteType\": \"Naos.Serialization.Test.JsonConfigurationTest+Shark, Naos.Serialization.Test\"\r\n}";

            var shark = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <SeaCreature>)).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");
        }
Example #19
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 NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Diet>), unregisteredTypeEncounteredStrategy: UnregisteredTypeEncounteredStrategy.Attempt).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();
        }
Example #20
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\": \"Naos.Serialization.Test.JsonConfigurationTest+Salmon, Naos.Serialization.Test\"\r\n}";

            var salmon1 = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <SeaCreature>)).Deserialize <SeaCreature>(salmonJson) as Salmon;
            var salmon2 = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <SeaCreature>)).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);
        }
Example #21
0
        public static void Serializer_serializes_camel_cased_enumerations()
        {
            var value = new CamelCasedEnumTest()
            {
                Value = TestEnum.FirstOption,
            };

            var result = new NaosJsonSerializer(unregisteredTypeEncounteredStrategy: UnregisteredTypeEncounteredStrategy.Attempt).SerializeToString(value);

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

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

            var result = new NaosJsonSerializer(unregisteredTypeEncounteredStrategy: UnregisteredTypeEncounteredStrategy.Attempt).SerializeToString(value);

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

            result.Should().Be(serializedValue);
        }
Example #23
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 NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Diet>), UnregisteredTypeEncounteredStrategy.Attempt).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();
        }
Example #24
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 NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <InheritedTypeBase>), unregisteredTypeEncounteredStrategy: UnregisteredTypeEncounteredStrategy.Attempt).SerializeToString(value);

            var serializedValue = "[\r\n  {\r\n    \"child1\": \"Child1\",\r\n    \"base\": \"Base\",\r\n    \"$concreteType\": \"Naos.Serialization.Test.JsonConfigurationTest+InheritedType1, Naos.Serialization.Test\"\r\n  },\r\n  {\r\n    \"child2\": \"my child 2\",\r\n    \"base\": \"my base\",\r\n    \"$concreteType\": \"Naos.Serialization.Test.JsonConfigurationTest+InheritedType2, Naos.Serialization.Test\"\r\n  }\r\n]";

            result.Should().Be(serializedValue);
        }
Example #25
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 NaosJsonSerializer(unregisteredTypeEncounteredStrategy: UnregisteredTypeEncounteredStrategy.Attempt).SerializeToString(expected);

            var actual = new NaosJsonSerializer().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);
        }