public static void DeserializeToDateTime___Should_deserialize_DateTime___When_called() { // Arrange // note that this will return "+00:00" if machine is on UTC time, which is what we expect var offset = DateTime.Now.ToString("%K"); var scenarios = new List <(DateTime Expected, string SerializedString)> { (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Utc), "2019-01-05T12:14:58.1920000Z"), (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Utc).AddTicks(1), "2019-01-05T12:14:58.1920001Z"), (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Utc).AddTicks(20), "2019-01-05T12:14:58.1920020Z"), (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Unspecified), "2019-01-05T12:14:58.1920000"), (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Unspecified).AddTicks(1), "2019-01-05T12:14:58.1920001"), (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Unspecified).AddTicks(20), "2019-01-05T12:14:58.1920020"), (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Local), "2019-01-05T12:14:58.1920000" + offset), (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Local).AddTicks(1), "2019-01-05T12:14:58.1920001" + offset), (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Local).AddTicks(20), "2019-01-05T12:14:58.1920020" + offset), }; // Act var actuals = scenarios.Select(_ => ObcDateTimeStringSerializer.DeserializeToDateTime(_.SerializedString)).ToList(); // Assert actuals.Must().BeEqualTo(scenarios.Select(_ => _.Expected).ToList()); }
public static void DeserializeToDateTime___Should_throw_InvalidOperationException___When_serializedString_is_malformed() { // Arrange var serializedDateTimes = new[] { "2017-05-06T02:28:46", // no precision not supported for Unspecified "2017-05-06T02:28:46+00:00", // no precision not supported for Local "2017-05-06T02:28:46.270488", // less than 7 precision not supported for Unspecified "2017-05-06T02:28:46.270488+00:00", // less than 7 precision not supported for Local "2017-05-06T02:28:46.27048838", // too much precision "2017-05-06T02:28:46.27048838+00:00", // too much precision "2017-05-06T02:28:46.27048838Z", // too much precision "2017-05-06T02:28:46.2704883K", // shouldn't end in K "some-string", // random string "2017-05-06", // only date "2017-05-06+00:00", // only date "2017-05-06Z", // only date "02:28:46.1938283", // only time "02:28:46.1938283+00:00", // only time "02:28:46.1938283Z", // only time }; // Act var actuals = serializedDateTimes.Select(_ => Record.Exception(() => ObcDateTimeStringSerializer.DeserializeToDateTime(_))).ToList(); // Assert actuals.Must().Each().BeOfType <InvalidOperationException>(); actuals.Select(_ => _.Message).Must().Each().ContainString("is malformed; it's not in a supported format and cannot be deserialized."); }
public static void DeserializeToDateTime___Should_throw_ArgumentNullException___When_parameter_serializedString_is_null() { // Arrange, Act var actual = Record.Exception(() => ObcDateTimeStringSerializer.DeserializeToDateTime(null)); // Assert actual.Must().BeOfType <ArgumentNullException>(); actual.Message.Must().ContainString("serializedString"); }
public static void DeserializeToDateTime___Should_throw_ArgumentException___When_parameter_serializedString_is_white_space() { // Arrange, Act var actual = Record.Exception(() => ObcDateTimeStringSerializer.DeserializeToDateTime(Invariant($" {Environment.NewLine} "))); // Assert actual.Must().BeOfType <ArgumentException>(); actual.Message.Must().ContainString("serializedString"); actual.Message.Must().ContainString("white space"); }
private object MakeObjectFromString( string value, Type type) { if (value == null) { return(null); } if (this.configuredTypeToSerializerMap.ContainsKey(type)) { var serializer = this.configuredTypeToSerializerMap[type]; var result = serializer.Deserialize(value, type); return(result); } else if (type.IsEnum) { var result = Enum.Parse(type, value); return(result); } else if (type.IsArray) { var arrayItemType = type.GetElementType() ?? throw new SerializationException(Invariant($"Found array type that cannot extract element type: {type}")); var asList = (IList)this.MakeObjectFromString(value, typeof(List <>).MakeGenericType(arrayItemType)); var asArrayList = new ArrayList(asList); var result = asArrayList.ToArray(arrayItemType); return(result); } else if (type == typeof(DateTime) || type == typeof(DateTime?)) { var result = ObcDateTimeStringSerializer.DeserializeToDateTime(value); return(result); } else if ((type != typeof(string)) && typeof(IEnumerable).IsAssignableFrom(type)) { var itemType = type.GenericTypeArguments.SingleOrDefault() ?? throw new SerializationException(Invariant($"Found {typeof(IEnumerable)} type that cannot extract element type: {type}")); var stringValues = value.FromCsv(this.dictionaryStringSerializer.NullValueEncoding); var result = (IList)typeof(List <>).MakeGenericType(itemType).Construct(); foreach (var stringValue in stringValues) { var itemValue = stringValue == null ? null : this.MakeObjectFromString(stringValue, itemType); result.Add(itemValue); } return(result); } else { var typeToSearchForParse = type; if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>)) { typeToSearchForParse = type.GenericTypeArguments.Single(); } var parseMethod = typeToSearchForParse .GetMethodsFiltered(MemberRelationships.DeclaredOrInherited, MemberOwners.All, MemberAccessModifiers.Public) .SingleOrDefault(_ => { var parameters = _.GetParameters(); return((_.Name == "Parse") && (parameters.Length == 1) && (parameters.Single().ParameterType == typeof(string))); }); var result = parseMethod == null ? value : parseMethod.Invoke(null, new object[] { value }); return(result); } }