#1 Introduces additional customdatetimeconverter
Required to parse the given datetime format
This commit is contained in:
parent
1d40a88efb
commit
a1f10da3d9
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
using JpnCardsPokemon.Sdk.Utils.JsonConverter;
|
||||||
|
|
||||||
namespace JpnCardsPokemon.Sdk.Api;
|
namespace JpnCardsPokemon.Sdk.Api;
|
||||||
|
|
||||||
|
@ -38,6 +39,7 @@ public class CardPrice
|
||||||
/// Date when the price information was updated last.
|
/// Date when the price information was updated last.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[JsonPropertyName("dateUpdated")]
|
[JsonPropertyName("dateUpdated")]
|
||||||
|
[JsonConverter(typeof(CustomDateTimeConverter))]
|
||||||
public DateTime? UpdatedDate { get; set; }
|
public DateTime? UpdatedDate { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
using System;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace JpnCardsPokemon.Sdk.Utils.JsonConverter
|
||||||
|
{
|
||||||
|
internal class CustomDateTimeConverter : JsonConverter<DateTime?>
|
||||||
|
{
|
||||||
|
private readonly string _dateTimeFormat = "MM/dd/yyyy";
|
||||||
|
|
||||||
|
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
switch (reader.TokenType)
|
||||||
|
{
|
||||||
|
case JsonTokenType.Null:
|
||||||
|
return null;
|
||||||
|
case JsonTokenType.String:
|
||||||
|
{
|
||||||
|
var dateString = reader.GetString();
|
||||||
|
if (DateTime.TryParseExact(dateString, _dateTimeFormat, null, System.Globalization.DateTimeStyles.None, out var dateTime))
|
||||||
|
{
|
||||||
|
return dateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
throw new JsonException($"Cannot convert {reader.GetString()} to DateTime.");
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new JsonException($"Cannot convert {reader.GetString()} to DateTime.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
if (value.HasValue)
|
||||||
|
{
|
||||||
|
writer.WriteStringValue(value.Value.ToString(_dateTimeFormat));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
writer.WriteNullValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue