public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var dateTime = (DateTime?)base.ReadJson(reader, typeof(DateTime?), existingValue, serializer); if (dateTime.HasValue) { if (objectType == typeof(EasternDateTime)) { return(EasternDateTime.Convert(dateTime.Value)); } else if (objectType == typeof(CentralDateTime)) { return(CentralDateTime.Convert(dateTime.Value)); } else if (objectType == typeof(MountainDateTime)) { return(MountainDateTime.Convert(dateTime.Value)); } else if (objectType == typeof(PacificDateTime)) { return(PacificDateTime.Convert(dateTime.Value)); } else if (objectType == typeof(AlaskaDateTime)) { return(AlaskaDateTime.Convert(dateTime.Value)); } else if (objectType == typeof(HawaiiDateTime)) { return(HawaiiDateTime.Convert(dateTime.Value)); } else if (objectType == typeof(UniversalDateTime)) { return(UniversalDateTime.Convert(dateTime.Value)); } } return(null); }
public override DateTimeBase Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.Number) { if (reader.TryGetInt64(out var unixSeconds)) { var utcDateTime = FromUnixTime(unixSeconds); if (typeToConvert == typeof(EasternDateTime)) { return(EasternDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(CentralDateTime)) { return(CentralDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(MountainDateTime)) { return(MountainDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(PacificDateTime)) { return(PacificDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(AlaskaDateTime)) { return(AlaskaDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(HawaiiDateTime)) { return(HawaiiDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(UniversalDateTime)) { return(UniversalDateTime.Convert(utcDateTime)); } } if (reader.TryGetDecimal(out var unixDecimalSeconds)) { var utcDateTime = FromUnixTime(Convert.ToInt64(unixDecimalSeconds)); if (typeToConvert == typeof(EasternDateTime)) { return(EasternDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(CentralDateTime)) { return(CentralDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(MountainDateTime)) { return(MountainDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(PacificDateTime)) { return(PacificDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(AlaskaDateTime)) { return(AlaskaDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(HawaiiDateTime)) { return(HawaiiDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(UniversalDateTime)) { return(UniversalDateTime.Convert(utcDateTime)); } } } if (reader.TokenType == JsonTokenType.String) { var value = reader.GetString(); if (typeToConvert == typeof(EasternDateTime) && EasternDateTime.TryParse(value, out var easternDateTime)) { return(easternDateTime); } else if (typeToConvert == typeof(CentralDateTime) && CentralDateTime.TryParse(value, out var centralDateTime)) { return(centralDateTime); } else if (typeToConvert == typeof(MountainDateTime) && MountainDateTime.TryParse(value, out var mountainDateTime)) { return(mountainDateTime); } else if (typeToConvert == typeof(PacificDateTime) && PacificDateTime.TryParse(value, out var pacificDateTime)) { return(pacificDateTime); } else if (typeToConvert == typeof(AlaskaDateTime) && AlaskaDateTime.TryParse(value, out var alaskaDateTime)) { return(alaskaDateTime); } else if (typeToConvert == typeof(HawaiiDateTime) && HawaiiDateTime.TryParse(value, out var hawaiiDateTime)) { return(hawaiiDateTime); } else if (typeToConvert == typeof(UniversalDateTime) && UniversalDateTime.TryParse(value, out var utcDateTime)) { return(utcDateTime); } if (long.TryParse(value, out var unixSeconds)) { var utcDateTime = FromUnixTime(unixSeconds); if (typeToConvert == typeof(EasternDateTime)) { return(EasternDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(CentralDateTime)) { return(CentralDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(MountainDateTime)) { return(MountainDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(PacificDateTime)) { return(PacificDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(AlaskaDateTime)) { return(AlaskaDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(HawaiiDateTime)) { return(HawaiiDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(UniversalDateTime)) { return(UniversalDateTime.Convert(utcDateTime)); } } if (decimal.TryParse(value, out var unixDecmialSeconds)) { var utcDateTime = FromUnixTime(Convert.ToInt64(unixDecmialSeconds)); if (typeToConvert == typeof(EasternDateTime)) { return(EasternDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(CentralDateTime)) { return(CentralDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(MountainDateTime)) { return(MountainDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(PacificDateTime)) { return(PacificDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(AlaskaDateTime)) { return(AlaskaDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(HawaiiDateTime)) { return(HawaiiDateTime.Convert(utcDateTime)); } if (typeToConvert == typeof(UniversalDateTime)) { return(UniversalDateTime.Convert(utcDateTime)); } } } return(null); }