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 ObcJsonSerializer(typeof(AnimalJsonSerializationConfiguration).ToJsonSerializationConfigurationType()); 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_to_only_child_type_that_strictly_matches_json_when_json_can_deserialize_into_multiple_child_types() { var noLightingJson = "{}"; var incandescentJson = "{\"watts\":60}"; var jsonSerializer = new ObcJsonSerializer(typeof(LightingJsonSerializationConfiguration).ToJsonSerializationConfigurationType()); var noLighting = jsonSerializer.Deserialize <Lighting>(noLightingJson) as NoLighting; var incandescent = jsonSerializer.Deserialize <Lighting>(incandescentJson) as Incandescent; noLighting.Should().NotBeNull(); incandescent.Should().NotBeNull(); incandescent.Watts.Should().Be(60); }
public static void RawSubject_Roundtrips() { // Arrange var expected = A.Dummy <RawSubject>(); // Act var actualBsonString = BsonSerializer.SerializeToString(expected); var actualBson = BsonSerializer.Deserialize <RawSubject>(actualBsonString); var actualJsonString = JsonSerializer.SerializeToString(expected); var actualJson = JsonSerializer.Deserialize <RawSubject>(actualJsonString); // Assert actualBson.Should().Be(expected); actualJson.Should().Be(expected); }
public static void LogConfigurationTimeSliced___Can___Deserialize() { // Arrange var input = @" { ""timeSlicePerFile"": ""00:01:00"", ""fileNamePrefix"": ""Log"", ""logFileDirectoryPath"": ""D:\\Logs"", ""createDirectoryStructureIfMissing"": true, ""logInclusionKindToOriginsMap"": { ""String"": null, ""Object"": null, ""Exception"": null, }, ""logItemPropertiesToIncludeInLogMessage"": ""default"" }"; var serializer = new ObcJsonSerializer <LoggingJsonSerializationConfiguration>(); // Act var result = serializer.Deserialize <LogWriterConfigBase>(input); // Assert var typed = (TimeSlicedFilesLogConfig)result; typed.LogFileDirectoryPath.Should().Be("D:\\Logs"); typed.TimeSlicePerFile.Should().Be(TimeSpan.FromMinutes(1)); typed.FileNamePrefix.Should().Be("Log"); typed.CreateDirectoryStructureIfMissing.Should().BeTrue(); typed.LogInclusionKindToOriginsMapFriendlyString.Should().Be("String=NULL,Object=NULL,Exception=NULL"); }
public static void Serializer_deserialize_type_where_constructor_parameter_is_different_type_than_corresponding_property_but_is_assignable_to_the_property_type() { var friendsJson = "{\"firstNames\": [\"betty\",\"bob\",\"bailey\"]}"; var serializationConfigurationType = typeof(TypesToRegisterJsonSerializationConfiguration <Friends>).ToJsonSerializationConfigurationType(); var serializer = new ObcJsonSerializer(serializationConfigurationType); var actual = serializer.Deserialize <Friends>(friendsJson); actual.Should().NotBeNull(); actual.FirstNames.Should().BeEquivalentTo("betty", "bob", "bailey"); }
public static void Roundtrip_DefaultPackageDescription() { // Arrange var expected = default(PackageDescription); var jsonSerializer = new ObcJsonSerializer <PackagingJsonSerializationConfiguration>(); // Act var actualJsonString = jsonSerializer.SerializeToString(expected); var actualFromJsonString = jsonSerializer.Deserialize <PackageDescription>(actualJsonString); // Assert actualFromJsonString.Should().Be(expected); }
public static void Deserialize___Null_type___Throws() { // Arrange var serializer = new ObcJsonSerializer(); Action action = () => serializer.Deserialize(string.Empty, null); // Act var exception = Record.Exception(action); // Assert exception.Should().NotBeNull(); exception.Should().BeOfType <ArgumentNullException>(); exception.Message.Should().Contain("type"); }
public static void Deserialize___Should_roundtrip_serialized_TestModelWithOptionalConstructorParameters___When_payload_is_missing_properties_for_constructor_parameters_with_default_values_for_parameters_not_assignable_to_null_and_parameters_assignable_to_null_that_are_defaulted_to_null() { // Arrange var payload1 = "{\r\n \"stringValue1\": \"my-string-value-1\",\r\n \"stringValue3\": \"my-string-value-3\",\r\n \"nullableIntValue2\": 28,\r\n \"nullableIntValue3\": 32,\r\n \"nullableEnumValue2\": \"third\",\r\n \"nullableEnumValue3\": \"fourth\"\r\n}"; var payload2 = "{\r\n \"stringValue1\": \"my-string-value-1\",\r\n \"stringValue3\": null,\r\n \"nullableIntValue2\": null,\r\n \"nullableIntValue3\": null,\r\n \"nullableEnumValue2\": null,\r\n \"nullableEnumValue3\": null\r\n}"; var payload3 = "{\r\n \"stringValue1\": \"my-string-value-1\",\r\n \"stringValue3\": null,\r\n \"nullableIntValue2\": 0,\r\n \"nullableIntValue3\": 0,\r\n \"nullableEnumValue2\": \"first\",\r\n \"nullableEnumValue3\": \"first\"\r\n}"; var serializer = new ObcJsonSerializer(typeof(TypesToRegisterJsonSerializationConfiguration <TestModelWithOptionalConstructorParameters>).ToJsonSerializationConfigurationType()); var expected1 = new TestModelWithOptionalConstructorParameters("my-string-value-1", stringValue3: "my-string-value-3", nullableIntValue2: 28, nullableIntValue3: 32, nullableEnumValue2: TestEnum.Third, nullableEnumValue3: TestEnum.Fourth); var expected2 = new TestModelWithOptionalConstructorParameters("my-string-value-1", stringValue3: null, nullableIntValue2: null, nullableIntValue3: null, nullableEnumValue2: null, nullableEnumValue3: null); var expected3 = new TestModelWithOptionalConstructorParameters("my-string-value-1", stringValue3: null, nullableIntValue2: 0, nullableIntValue3: 0, nullableEnumValue2: TestEnum.First, nullableEnumValue3: TestEnum.First); // Act var actual1 = serializer.Deserialize <TestModelWithOptionalConstructorParameters>(payload1); var actual2 = serializer.Deserialize <TestModelWithOptionalConstructorParameters>(payload2); var actual3 = serializer.Deserialize <TestModelWithOptionalConstructorParameters>(payload3); // Assert actual1.AsTest().Must().BeEqualTo(expected1); actual2.AsTest().Must().BeEqualTo(expected2); actual3.AsTest().Must().BeEqualTo(expected3); }
public static void Serializer_deserialize_inherited_types_with_constructors() { var dogJson = "{\"name\":\"Barney\",\"furColor\":\"brindle\",\"age\":10,\"dogTag\":\"my name is Barney\"}"; var catJson = "{\"numberOfLives\":9,\"name\":\"Cleo\",\"age\":3}"; var tigerJson = "{\"tailLength\":2,\"name\":\"Ronny\",\"numberOfTeeth\":50,\"age\":5}"; var mouseJson = "{\"tailLength\":8,\"name\":\"Missy\",\"furColor\":\"black\",\"age\":7}"; var jsonSerializer = new ObcJsonSerializer(typeof(AnimalJsonSerializationConfiguration).ToJsonSerializationConfigurationType()); var dog = jsonSerializer.Deserialize <Animal>(dogJson) as Dog; var cat = jsonSerializer.Deserialize <Animal>(catJson) as Cat; var tiger = jsonSerializer.Deserialize <Animal>(tigerJson) as Tiger; var mouse = jsonSerializer.Deserialize <Animal>(mouseJson) as Mouse; 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"); cat.Should().NotBeNull(); cat.Name.Should().Be("Cleo"); cat.Age.Should().Be(3); cat.NumberOfLives.Should().Be(9); tiger.Should().NotBeNull(); tiger.Name.Should().Be("Ronny"); tiger.Age.Should().Be(5); tiger.TailLength.Should().Be(2); tiger.NumberOfTeeth.Should().Be(50); mouse.Should().NotBeNull(); mouse.Name.Should().Be("Missy"); mouse.Age.Should().Be(7); mouse.TailLength.Should().Be(8); mouse.FurColor.Should().Be(FurColor.Black); }
public static void Deserialize___Should_roundtrip_serialized_TestModelWithOptionalConstructorParameters___When_payload_is_missing_properties_for_constructor_parameters_with_default_values() { // Arrange var payload = "{ \"stringValue1\": \"my-string-value\" }"; var serializer = new ObcJsonSerializer(typeof(TypesToRegisterJsonSerializationConfiguration <TestModelWithOptionalConstructorParameters>).ToJsonSerializationConfigurationType()); var expected = new TestModelWithOptionalConstructorParameters("my-string-value"); // Act var actual = serializer.Deserialize <TestModelWithOptionalConstructorParameters>(payload); // Assert actual.AsTest().Must().BeEqualTo(expected); }
public static void ProcessDetails_Roundtrips() { // Arrange var expected = A.Dummy <ProcessDetails>(); var bsonSerializer = new ObcBsonSerializer(typeof(DiagnosticsBsonSerializationConfiguration).ToBsonSerializationConfigurationType()); var jsonSerializer = new ObcJsonSerializer(typeof(DiagnosticsJsonSerializationConfiguration).ToJsonSerializationConfigurationType()); // Act var actualBsonString = bsonSerializer.SerializeToString(expected); var actualBson = bsonSerializer.Deserialize <ProcessDetails>(actualBsonString); var actualJsonString = jsonSerializer.SerializeToString(expected); var actualJson = jsonSerializer.Deserialize <ProcessDetails>(actualJsonString); // Assert actualBson.Should().Be(expected); actualJson.Should().Be(expected); }
public static void Roundtrip_DummyPackage() { // Arrange var expected = A.Dummy <Package>(); var bsonSerializer = new ObcBsonSerializer <PackagingBsonSerializationConfiguration>(); var jsonSerializer = new ObcJsonSerializer <PackagingJsonSerializationConfiguration>(); // Act var actualBsonString = bsonSerializer.SerializeToString(expected); var actualFromBsonString = bsonSerializer.Deserialize <Package>(actualBsonString); var actualJsonString = jsonSerializer.SerializeToString(expected); var actualFromJsonString = jsonSerializer.Deserialize <Package>(actualJsonString); // Assert actualFromBsonString.Should().Be(expected); actualFromJsonString.Should().Be(expected); }
public static void Roundtrip_DefaultPropertiesPackageRepositoryConfiguration() { // Arrange var expected = new PackageRepositoryConfiguration(); var bsonSerializer = new ObcBsonSerializer <PackagingBsonSerializationConfiguration>(); var jsonSerializer = new ObcJsonSerializer <PackagingJsonSerializationConfiguration>(); // Act var actualBsonString = bsonSerializer.SerializeToString(expected); var actualFromBsonString = bsonSerializer.Deserialize <PackageRepositoryConfiguration>(actualBsonString); var actualJsonString = jsonSerializer.SerializeToString(expected); var actualFromJsonString = jsonSerializer.Deserialize <PackageRepositoryConfiguration>(actualJsonString); // Assert actualFromBsonString.Should().Be(expected); actualFromJsonString.Should().Be(expected); }
public static void Roundtrip_DefaultScheduleBase() { // Arrange var expected = default(ScheduleBase); var bsonSerializer = new ObcBsonSerializer <CronBsonSerializationConfiguration>(); var jsonSerializer = new ObcJsonSerializer <CronJsonSerializationConfiguration>(); // Act var actualBsonString = bsonSerializer.SerializeToString(expected); var actualFromBsonString = bsonSerializer.Deserialize <ScheduleBase>(actualBsonString); var actualJsonString = jsonSerializer.SerializeToString(expected); var actualFromJsonString = jsonSerializer.Deserialize <ScheduleBase>(actualJsonString); // Assert actualFromBsonString.Should().Be(expected); actualFromJsonString.Should().Be(expected); }
public static void ToExcelWorkbook___Should_project_report_into_Excel_Workbook___When_called() { // Arrange var reportSerializedJsonFile = "FILE_PATH_HERE"; var excelWorkbookFilePath = "FILE_PATH_HERE"; var asposeLicense = @"PASTE_LICENSE_HERE"; var cultureKind = CultureKind.EnglishUnitedStates; using (var licenseStream = new MemoryStream(Encoding.ASCII.GetBytes(asposeLicense))) { new License().SetLicense(licenseStream); } var serializer = new ObcJsonSerializer <DependencyOnlyJsonSerializationConfiguration <DataStructureJsonSerializationConfiguration> >(); var report = serializer.Deserialize <Report>(File.ReadAllText(reportSerializedJsonFile)); if (report.AdditionalInfo == null) { report = report.DeepCloneWithAdditionalInfo(new AdditionalReportInfo("Copyright (c) 2021 The Company. All rights reserved.", "This file is for authorized users only. All data is private and confidential to its owner and is only viewable by the owner's authorized parties.")); } var context = new ReportToWorkbookProjectionContext { CultureKind = cultureKind, }; File.Delete(excelWorkbookFilePath); // Act var workbook = report.ToExcelWorkbook(context); // Assert workbook.Save(excelWorkbookFilePath); // Cleanup workbook.Dispose(); }