From 1d40a88efbbaa6e50fa39fa50bd0385a9c27b4c0 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 24 Mar 2023 17:39:15 +0100 Subject: [PATCH 1/3] #1 Updates CardPrice Json Property names and removes CardPriceConverter attribute in Card --- JpnCardsPokemon.Sdk/Api/Card.cs | 2 -- JpnCardsPokemon.Sdk/Api/CardPrice.cs | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/JpnCardsPokemon.Sdk/Api/Card.cs b/JpnCardsPokemon.Sdk/Api/Card.cs index 14f2989..19ccebf 100644 --- a/JpnCardsPokemon.Sdk/Api/Card.cs +++ b/JpnCardsPokemon.Sdk/Api/Card.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.Text.Json.Serialization; -using JpnCardsPokemon.Sdk.Utils.JsonConverter; namespace JpnCardsPokemon.Sdk.Api; @@ -64,7 +63,6 @@ public class Card /// A list of known prices for this card. /// /// May contain entries from different sellers, versions and conditions. - [JsonConverter(typeof(CardPricesJsonConverter))] public IEnumerable? Prices { get; set; } /// diff --git a/JpnCardsPokemon.Sdk/Api/CardPrice.cs b/JpnCardsPokemon.Sdk/Api/CardPrice.cs index dabcbae..5402a02 100644 --- a/JpnCardsPokemon.Sdk/Api/CardPrice.cs +++ b/JpnCardsPokemon.Sdk/Api/CardPrice.cs @@ -12,6 +12,7 @@ public class CardPrice /// Specifies the card version. Almost always will be 'Regular' but may contain other versions like 'Reverse Holo', /// etc. /// + [JsonPropertyName("variant")] public string? Version { get; set; } /// @@ -48,5 +49,6 @@ public class CardPrice /// /// Name of the seller who is listing the card. /// + [JsonPropertyName("vendor")] public string? Seller { get; set; } } \ No newline at end of file From a1f10da3d9bf9e066f5557f1ceeb9d49a40041d3 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 24 Mar 2023 18:10:23 +0100 Subject: [PATCH 2/3] #1 Introduces additional customdatetimeconverter Required to parse the given datetime format --- JpnCardsPokemon.Sdk/Api/CardPrice.cs | 2 + .../JsonConverter/CustomDateTimeConverter.cs | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 JpnCardsPokemon.Sdk/Utils/JsonConverter/CustomDateTimeConverter.cs diff --git a/JpnCardsPokemon.Sdk/Api/CardPrice.cs b/JpnCardsPokemon.Sdk/Api/CardPrice.cs index 5402a02..d398ee4 100644 --- a/JpnCardsPokemon.Sdk/Api/CardPrice.cs +++ b/JpnCardsPokemon.Sdk/Api/CardPrice.cs @@ -1,5 +1,6 @@ using System; using System.Text.Json.Serialization; +using JpnCardsPokemon.Sdk.Utils.JsonConverter; namespace JpnCardsPokemon.Sdk.Api; @@ -38,6 +39,7 @@ public class CardPrice /// Date when the price information was updated last. /// [JsonPropertyName("dateUpdated")] + [JsonConverter(typeof(CustomDateTimeConverter))] public DateTime? UpdatedDate { get; set; } /// diff --git a/JpnCardsPokemon.Sdk/Utils/JsonConverter/CustomDateTimeConverter.cs b/JpnCardsPokemon.Sdk/Utils/JsonConverter/CustomDateTimeConverter.cs new file mode 100644 index 0000000..45c47b5 --- /dev/null +++ b/JpnCardsPokemon.Sdk/Utils/JsonConverter/CustomDateTimeConverter.cs @@ -0,0 +1,46 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace JpnCardsPokemon.Sdk.Utils.JsonConverter +{ + internal class CustomDateTimeConverter : JsonConverter + { + 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(); + } + } + } +} From 8e1e668e68844c5dbb523475f83370c35c2074fb Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 24 Mar 2023 18:11:26 +0100 Subject: [PATCH 3/3] #1 Removes old unused custom price converter --- .../JsonConverter/CardPricesJsonConverter.cs | 111 ------------------ 1 file changed, 111 deletions(-) delete mode 100644 JpnCardsPokemon.Sdk/Utils/JsonConverter/CardPricesJsonConverter.cs diff --git a/JpnCardsPokemon.Sdk/Utils/JsonConverter/CardPricesJsonConverter.cs b/JpnCardsPokemon.Sdk/Utils/JsonConverter/CardPricesJsonConverter.cs deleted file mode 100644 index 9f32882..0000000 --- a/JpnCardsPokemon.Sdk/Utils/JsonConverter/CardPricesJsonConverter.cs +++ /dev/null @@ -1,111 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Text.Json; -using System.Text.Json.Serialization; -using JpnCardsPokemon.Sdk.Api; - -namespace JpnCardsPokemon.Sdk.Utils.JsonConverter; - -internal class CardPricesJsonConverter : JsonConverter> -{ - public override IEnumerable Read(ref Utf8JsonReader reader, Type typeToConvert, - JsonSerializerOptions options) - { - var resultBucket = new List(); - - // read sellers - while (reader.Read()) - { - // Finished parsing? - if (reader.TokenType == JsonTokenType.EndArray) - break; - - - if (reader.TokenType != JsonTokenType.PropertyName) continue; - var sellerName = reader.GetString(); - - // Versions - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndArray) - break; - - // Version, usually "Regular" - if (reader.TokenType != JsonTokenType.PropertyName) continue; - var cardVersion = reader.GetString(); - - // Conditions - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndObject) break; - - if (reader.TokenType != JsonTokenType.PropertyName) continue; - var condition = reader.GetString(); - - var cardPrice = new CardPrice - { - Seller = sellerName, - Version = cardVersion, - Condition = condition - }; - - var propertyName = string.Empty; - // Final price properties - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndObject) break; - - - if (reader.TokenType == JsonTokenType.PropertyName) - { - propertyName = reader.GetString(); - } - else - { - // search correct property in object - if (string.IsNullOrEmpty(propertyName)) continue; - - var property = cardPrice.GetType().GetProperty(propertyName) ?? cardPrice - .GetType().GetProperties().FirstOrDefault(p => - p.GetCustomAttributes(typeof(JsonPropertyNameAttribute), true) - .Cast() - .Any(a => a.Name.Equals(propertyName, - StringComparison.InvariantCultureIgnoreCase))); - if (property != null) - { - object? value = null; - var propertyType = property.PropertyType; - - if (propertyType == typeof(string)) - value = reader.GetString(); - else if (propertyType == typeof(decimal)) - value = reader.GetDecimal(); - else if (propertyType == typeof(DateTime?)) - if (DateTime.TryParseExact(reader.GetString(), "MM/dd/yyyy", - CultureInfo.InvariantCulture, DateTimeStyles.None, out var dt)) - value = dt; - - property.SetValue(cardPrice, value); - } - - propertyName = string.Empty; - } - } - - // add card to bucket - resultBucket.Add(cardPrice); - } - } - } - - - return resultBucket; - } - - public override void Write(Utf8JsonWriter writer, IEnumerable value, JsonSerializerOptions options) - { - throw new NotImplementedException(); - } -} \ No newline at end of file