diff --git a/SteamStorefrontAPI/Classes/Converters.cs b/SteamStorefrontAPI/Classes/Converters.cs new file mode 100644 index 0000000..15589a4 --- /dev/null +++ b/SteamStorefrontAPI/Classes/Converters.cs @@ -0,0 +1,70 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SteamStorefrontAPI.Classes +{ + // Converts price strings to double, e.g. 2599 => 25.99 + public class SteamPriceStringConverter : JsonConverter + { + public override bool CanRead + { + get => true; + } + + public override bool CanWrite + { + get => false; + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + var value = reader.Value.ToString(); + return double.Parse(value.Insert(value.Length - 2, ".")); + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + throw new NotImplementedException(); + } + + public override bool CanConvert(Type objectType) + { + throw new NotImplementedException(); + } + } + + // Converts a string to a ControllerSupport enum + internal class ControllerSupportConverter : JsonConverter + { + public override bool CanRead + { + get => true; + } + + public override bool CanConvert(Type t) => t == typeof(ControllerSupport) || t == typeof(ControllerSupport?); + + public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer) + { + if (reader.TokenType == JsonToken.Null) return null; + var value = serializer.Deserialize(reader); + + ControllerSupport convertedValue; + if(Enum.TryParse(value, out convertedValue)) + { + return convertedValue; + } + + return null; + } + + public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer) + { + throw new NotImplementedException(); + } + } + +} diff --git a/SteamStorefrontAPI/Classes/appdetails/PriceOverview.cs b/SteamStorefrontAPI/Classes/appdetails/PriceOverview.cs index ed5677d..bc4c856 100644 --- a/SteamStorefrontAPI/Classes/appdetails/PriceOverview.cs +++ b/SteamStorefrontAPI/Classes/appdetails/PriceOverview.cs @@ -15,49 +15,14 @@ namespace SteamStorefrontAPI.Classes public string Currency { get; set; } [JsonProperty("initial")] - [JsonConverter(typeof(LongToDoubleConverter))] + [JsonConverter(typeof(SteamPriceStringConverter))] public double Initial { get; set; } [JsonProperty("final")] - [JsonConverter(typeof(LongToDoubleConverter))] + [JsonConverter(typeof(SteamPriceStringConverter))] public double Final { get; set; } [JsonProperty("discount_percent")] public int DiscountPercent { get; set; } } - - public class LongToDoubleConverter : JsonConverter - { - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) - { - var value = reader.Value.ToString(); - return double.Parse(value.Insert(value.Length - 2, ".")); - } - - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - throw new NotImplementedException("Unnecessary because CanRead is CanWrite. The type will skip the converter."); - } - - public override bool CanConvert(Type objectType) - { - throw new NotImplementedException(); - } - - public override bool CanRead - { - get { return true; } - } - - public override bool CanWrite - { - get { return false; } - } - } - - public class PriceOverviewSerializer : JsonSerializer - { - - } - } diff --git a/SteamStorefrontAPI/Classes/featured/FeaturedApp.cs b/SteamStorefrontAPI/Classes/featured/FeaturedApp.cs index f6ef814..faa29c4 100644 --- a/SteamStorefrontAPI/Classes/featured/FeaturedApp.cs +++ b/SteamStorefrontAPI/Classes/featured/FeaturedApp.cs @@ -9,13 +9,15 @@ using Newtonsoft.Json.Converters; namespace SteamStorefrontAPI.Classes { + public enum ControllerSupport { Full, Partial }; + public class FeaturedApp { [JsonProperty("id")] - public long Id { get; set; } + public int Id { get; set; } [JsonProperty("type")] - public long Type { get; set; } + public int Type { get; set; } [JsonProperty("name")] public string Name { get; set; } @@ -24,16 +26,18 @@ namespace SteamStorefrontAPI.Classes public bool Discounted { get; set; } [JsonProperty("discount_percent")] - public long DiscountPercent { get; set; } + public int DiscountPercent { get; set; } [JsonProperty("original_price")] - public long? OriginalPrice { get; set; } + [JsonConverter(typeof(SteamPriceStringConverter))] + public double? OriginalPrice { get; set; } [JsonProperty("final_price")] - public long FinalPrice { get; set; } + [JsonConverter(typeof(SteamPriceStringConverter))] + public double? FinalPrice { get; set; } [JsonProperty("currency")] - public Currency Currency { get; set; } + public string Currency { get; set; } [JsonProperty("large_capsule_image")] public string LargeCapsuleImage { get; set; } @@ -60,6 +64,7 @@ namespace SteamStorefrontAPI.Classes public long? DiscountExpiration { get; set; } [JsonProperty("controller_support", NullValueHandling = NullValueHandling.Ignore)] + [JsonConverter(typeof(ControllerSupportConverter))] public ControllerSupport? ControllerSupport { get; set; } } } diff --git a/SteamStorefrontAPI/Classes/featured/SteamFeatured.cs b/SteamStorefrontAPI/Classes/featured/SteamFeatured.cs index 24e112d..1547b01 100644 --- a/SteamStorefrontAPI/Classes/featured/SteamFeatured.cs +++ b/SteamStorefrontAPI/Classes/featured/SteamFeatured.cs @@ -27,15 +27,12 @@ namespace SteamStorefrontAPI.Classes public string Layout { get; set; } [JsonProperty("status")] - public long Status { get; set; } + public int Status { get; set; } public static SteamFeatured FromJson(string json) => JsonConvert.DeserializeObject(json, Converter.Settings); } - public enum ControllerSupport { Full }; - - public enum Currency { Eur }; public static class Serialize { @@ -50,61 +47,9 @@ namespace SteamStorefrontAPI.Classes DateParseHandling = DateParseHandling.None, Converters = { new ControllerSupportConverter(), - new CurrencyConverter(), new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } }, }; } - internal class ControllerSupportConverter : JsonConverter - { - public override bool CanConvert(Type t) => t == typeof(ControllerSupport) || t == typeof(ControllerSupport?); - - public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer) - { - if (reader.TokenType == JsonToken.Null) return null; - var value = serializer.Deserialize(reader); - if (value == "full") - { - return ControllerSupport.Full; - } - throw new Exception("Cannot unmarshal type ControllerSupport"); - } - - public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer) - { - var value = (ControllerSupport)untypedValue; - if (value == ControllerSupport.Full) - { - serializer.Serialize(writer, "full"); return; - } - throw new Exception("Cannot marshal type ControllerSupport"); - } - } - - internal class CurrencyConverter : JsonConverter - { - public override bool CanConvert(Type t) => t == typeof(Currency) || t == typeof(Currency?); - - public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer) - { - if (reader.TokenType == JsonToken.Null) return null; - var value = serializer.Deserialize(reader); - if (value == "EUR") - { - return Currency.Eur; - } - throw new Exception("Cannot unmarshal type Currency"); - } - - public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer) - { - var value = (Currency)untypedValue; - if (value == Currency.Eur) - { - serializer.Serialize(writer, "EUR"); return; - } - throw new Exception("Cannot marshal type Currency"); - } - } } diff --git a/SteamStorefrontAPI/SteamStorefrontAPI.csproj b/SteamStorefrontAPI/SteamStorefrontAPI.csproj index a758f77..dbe7eeb 100644 --- a/SteamStorefrontAPI/SteamStorefrontAPI.csproj +++ b/SteamStorefrontAPI/SteamStorefrontAPI.csproj @@ -43,6 +43,7 @@ +