Added controllersupport converter

This commit is contained in:
mmuffins 2018-05-23 22:35:28 +02:00
parent 0b8ec19632
commit b2a28190dc
5 changed files with 85 additions and 99 deletions

View File

@ -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<string>(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();
}
}
}

View File

@ -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
{
}
}

View File

@ -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; }
}
}

View File

@ -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<SteamFeatured>(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<string>(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<string>(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");
}
}
}

View File

@ -43,6 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\Converters.cs" />
<Compile Include="Endpoints\AppDetails.cs" />
<Compile Include="Classes\appdetails\LinuxRequirements.cs" />
<Compile Include="Classes\appdetails\MacRequirements.cs" />