public static void Presence_of_concreteType_token_does_not_interfere_with_normal_deserialization() { // Arrange var expectedId = "year-field"; var expectedDecimalPlaces = 5; var expectedTitle = "my-title"; var year = new YearField(expectedId) { NumberOfDecimalPlaces = expectedDecimalPlaces, Title = expectedTitle, }; var serializer = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Field>)); var jsonWithConcreteType = serializer.SerializeToString(year); var settings = new NullJsonConfiguration().BuildJsonSerializerSettings(SerializationDirection.Deserialize); settings.Converters = new JsonConverter[0]; // Act var actual = JsonConvert.DeserializeObject <YearField>(jsonWithConcreteType, settings); // Assert typeof(YearField).IsAssignableTo(typeof(Field)).Should().BeTrue(); jsonWithConcreteType.Should().Contain("$concreteType"); actual.Id.Should().Be(expectedId); actual.NumberOfDecimalPlaces.Should().Be(expectedDecimalPlaces); actual.Title.Should().Be(expectedTitle); }
public static void NaosJsonSerializer___With_type_Compact___Uses_compact() { // Arrange var property1 = A.Dummy <string>(); var property2 = A.Dummy <string>(); var property3 = A.Dummy <string>(); var expected = "{" + Invariant($"\"property1\":\"{property1}\",") + Invariant($"\"property2\":\"{property2}\",") + Invariant($"\"property3\":\"{property3}\",") + "\"property4\":null" + "}"; var test = new TestObject { Property1 = property1, Property2 = property2, Property3 = property3, }; var serializer = new NaosJsonSerializer(unregisteredTypeEncounteredStrategy: UnregisteredTypeEncounteredStrategy.Attempt, formattingKind: JsonFormattingKind.Compact); // Act var actual = serializer.SerializeToString(test); // Assert actual.Should().Be(expected); }
public static void NaosJsonSerializer___With_type_Default___Uses_default() { // Arrange var property1 = A.Dummy <string>(); var property2 = A.Dummy <string>(); var property3 = A.Dummy <string>(); var expected = "{" + Environment.NewLine + Invariant($" \"property1\": \"{property1}\",") + Environment.NewLine + Invariant($" \"property2\": \"{property2}\",") + Environment.NewLine + Invariant($" \"property3\": \"{property3}\",") + Environment.NewLine + " \"property4\": null" + Environment.NewLine + "}"; var test = new TestObject { Property1 = property1, Property2 = property2, Property3 = property3, }; var serializer = new NaosJsonSerializer(unregisteredTypeEncounteredStrategy: UnregisteredTypeEncounteredStrategy.Attempt); // Act var actual = serializer.SerializeToString(test); // Assert actual.Should().Be(expected); }
public static void Serializer_deserialize_partial_InheritedTypes() { var serializedValue = "[" + Environment.NewLine + " {" + Environment.NewLine + " \"diet\": {\r\n \"maxCalories\": 50000,\r\n \"$concreteType\": \"Naos.Serialization.Test.JsonConfigurationTest+LowCalorie, Naos.Serialization.Test\"\r\n }," + Environment.NewLine + " \"int32\": 5" + Environment.NewLine + " }," + Environment.NewLine + " {" + Environment.NewLine + " \"int32\": 55," + Environment.NewLine + " \"float\": 3.56" + Environment.NewLine + " }" + Environment.NewLine + "]"; var result = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <IBaseInterface>), UnregisteredTypeEncounteredStrategy.Attempt).Deserialize <IBaseInterface[]>(serializedValue); result.Length.Should().Be(2); result[0].Diet.Should().BeOfType <LowCalorie>(); ((LowCalorie)result[0].Diet).MaxCalories.Should().Be(50000); (result[0] as InheritedType3).Should().NotBeNull(); (result[0] as InheritedType3).Int32.Should().Be(5); (result[0] as InheritedType3).Float.Should().Be(default(float)); result[1].Diet.Should().BeNull(); (result[1] as InheritedType3).Should().NotBeNull(); (result[1] as InheritedType3).Int32.Should().Be(55); Math.Round((result[1] as InheritedType3).Float, 2).Should().Be(Math.Round(3.56, 2)); }
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}"); }
public static void TestBase() { var serializer = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <TestBase>)); var tasks = Enumerable.Range(1, 100).Select(_ => A.Dummy <TestBase>()) .Select(_ => new Task(() => serializer.SerializeToString(_))).ToArray(); Parallel.ForEach(tasks, _ => _.Start()); Task.WaitAll(tasks); }
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 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); }
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); }
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 Serializer_deserialize_camel_cased_properties() { var serializedValue = "{" + Environment.NewLine + " \"testName\": \"there\"" + Environment.NewLine + "}"; var result = new NaosJsonSerializer(unregisteredTypeEncounteredStrategy: UnregisteredTypeEncounteredStrategy.Attempt).Deserialize <CamelCasedPropertyTest>(serializedValue); result.TestName.Should().Be("there"); }
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); }
public static void Serializer_deserialize_camel_cased_enumerations() { var serializedValue = "{" + Environment.NewLine + " \"value\": \"secondOption\"" + Environment.NewLine + "}"; var result = new NaosJsonSerializer(unregisteredTypeEncounteredStrategy: UnregisteredTypeEncounteredStrategy.Attempt).Deserialize <CamelCasedEnumTest>(serializedValue); result.Value.Should().Be(TestEnum.SecondOption); }
public static void RootFirstWorks() { var middleSerializer = new NaosJsonSerializer(typeof(ConfigMiddle)); middleSerializer.SerializeToString(null); var topSerializer = new NaosJsonSerializer(typeof(ConfigTop)); topSerializer.SerializeToString(null); }
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); }
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(); }
public static void Serializer_serializes_object_where_constructor_parameter_is_different_type_than_corresponding_property_but_is_assignable_from_that_property_type() { var family = new Family(new List <string> { "joe", "jane", "jackie" }); var expectedFamilyJson = "{\r\n \"firstNames\": [\r\n \"joe\",\r\n \"jane\",\r\n \"jackie\"\r\n ]\r\n}"; var actualFamilyJson = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Family>)).SerializeToString(family); expectedFamilyJson.Should().Be(actualFamilyJson); }
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); }
public static void DeserializeObject_without_type_deserialize_json_into_JObject_using_DefaultSerializerSettings() { var dogJson = "{\"name\":\"Barney\",\"furColor\":\"brindle\",\"age\":10}"; var dog = new NaosJsonSerializer(unregisteredTypeEncounteredStrategy: UnregisteredTypeEncounteredStrategy.Attempt).Deserialize <dynamic>(dogJson) as JObject; dog.Properties().Count().Should().Be(3); dog["name"].ToString().Should().Be("Barney"); dog["age"].ToString().Should().Be("10"); dog["furColor"].ToString().Should().Be("brindle"); }
public static void DeserializeObject_without_type_deserialize_json_into_JObject_using_CompactSerializerSettings() { var dogJson = "{\"name\":\"Barney\",\"furColor\":\"brindle\",\"age\":10}"; var dog = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Animal>), UnregisteredTypeEncounteredStrategy.Attempt, JsonFormattingKind.Compact).Deserialize <dynamic>(dogJson) as JObject; dog.Properties().Count().Should().Be(3); dog["name"].ToString().Should().Be("Barney"); dog["age"].ToString().Should().Be("10"); dog["furColor"].ToString().Should().Be("brindle"); }
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); }
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"); }
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); }
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 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"); }
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"); }
public static void Serializer_throws_JsonSerializationException_when_a_constructor_parameter_is_missing_in_json_and_that_parameter_is_a_property_on_the_child_type() { var catJson1 = "{\"numberOfLives\":9,\"name\":\"Cleo\"}"; var catJson2 = "{\"numberOfLives\":9,\"age\":3}"; var jsonSerializer = new NaosJsonSerializer(typeof(GenericDiscoveryJsonConfiguration <Animal>)); var ex1 = Assert.Throws <JsonSerializationException>(() => jsonSerializer.Deserialize <Animal>(catJson1)); var ex2 = Assert.Throws <JsonSerializationException>(() => jsonSerializer.Deserialize <Animal>(catJson2)); ex1.Message.Should().StartWith("The json payload could not be deserialized into any of the candidate types."); ex2.Message.Should().StartWith("The json payload could not be deserialized into any of the candidate types."); }
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); }