Added controllersupport converter
This commit is contained in:
parent
0b8ec19632
commit
b2a28190dc
70
SteamStorefrontAPI/Classes/Converters.cs
Normal file
70
SteamStorefrontAPI/Classes/Converters.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -15,49 +15,14 @@ namespace SteamStorefrontAPI.Classes
|
|||||||
public string Currency { get; set; }
|
public string Currency { get; set; }
|
||||||
|
|
||||||
[JsonProperty("initial")]
|
[JsonProperty("initial")]
|
||||||
[JsonConverter(typeof(LongToDoubleConverter))]
|
[JsonConverter(typeof(SteamPriceStringConverter))]
|
||||||
public double Initial { get; set; }
|
public double Initial { get; set; }
|
||||||
|
|
||||||
[JsonProperty("final")]
|
[JsonProperty("final")]
|
||||||
[JsonConverter(typeof(LongToDoubleConverter))]
|
[JsonConverter(typeof(SteamPriceStringConverter))]
|
||||||
public double Final { get; set; }
|
public double Final { get; set; }
|
||||||
|
|
||||||
[JsonProperty("discount_percent")]
|
[JsonProperty("discount_percent")]
|
||||||
public int DiscountPercent { get; set; }
|
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
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,13 +9,15 @@ using Newtonsoft.Json.Converters;
|
|||||||
|
|
||||||
namespace SteamStorefrontAPI.Classes
|
namespace SteamStorefrontAPI.Classes
|
||||||
{
|
{
|
||||||
|
public enum ControllerSupport { Full, Partial };
|
||||||
|
|
||||||
public class FeaturedApp
|
public class FeaturedApp
|
||||||
{
|
{
|
||||||
[JsonProperty("id")]
|
[JsonProperty("id")]
|
||||||
public long Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
[JsonProperty("type")]
|
[JsonProperty("type")]
|
||||||
public long Type { get; set; }
|
public int Type { get; set; }
|
||||||
|
|
||||||
[JsonProperty("name")]
|
[JsonProperty("name")]
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
@ -24,16 +26,18 @@ namespace SteamStorefrontAPI.Classes
|
|||||||
public bool Discounted { get; set; }
|
public bool Discounted { get; set; }
|
||||||
|
|
||||||
[JsonProperty("discount_percent")]
|
[JsonProperty("discount_percent")]
|
||||||
public long DiscountPercent { get; set; }
|
public int DiscountPercent { get; set; }
|
||||||
|
|
||||||
[JsonProperty("original_price")]
|
[JsonProperty("original_price")]
|
||||||
public long? OriginalPrice { get; set; }
|
[JsonConverter(typeof(SteamPriceStringConverter))]
|
||||||
|
public double? OriginalPrice { get; set; }
|
||||||
|
|
||||||
[JsonProperty("final_price")]
|
[JsonProperty("final_price")]
|
||||||
public long FinalPrice { get; set; }
|
[JsonConverter(typeof(SteamPriceStringConverter))]
|
||||||
|
public double? FinalPrice { get; set; }
|
||||||
|
|
||||||
[JsonProperty("currency")]
|
[JsonProperty("currency")]
|
||||||
public Currency Currency { get; set; }
|
public string Currency { get; set; }
|
||||||
|
|
||||||
[JsonProperty("large_capsule_image")]
|
[JsonProperty("large_capsule_image")]
|
||||||
public string LargeCapsuleImage { get; set; }
|
public string LargeCapsuleImage { get; set; }
|
||||||
@ -60,6 +64,7 @@ namespace SteamStorefrontAPI.Classes
|
|||||||
public long? DiscountExpiration { get; set; }
|
public long? DiscountExpiration { get; set; }
|
||||||
|
|
||||||
[JsonProperty("controller_support", NullValueHandling = NullValueHandling.Ignore)]
|
[JsonProperty("controller_support", NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
[JsonConverter(typeof(ControllerSupportConverter))]
|
||||||
public ControllerSupport? ControllerSupport { get; set; }
|
public ControllerSupport? ControllerSupport { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,15 +27,12 @@ namespace SteamStorefrontAPI.Classes
|
|||||||
public string Layout { get; set; }
|
public string Layout { get; set; }
|
||||||
|
|
||||||
[JsonProperty("status")]
|
[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 static SteamFeatured FromJson(string json) => JsonConvert.DeserializeObject<SteamFeatured>(json, Converter.Settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public enum ControllerSupport { Full };
|
|
||||||
|
|
||||||
public enum Currency { Eur };
|
|
||||||
|
|
||||||
public static class Serialize
|
public static class Serialize
|
||||||
{
|
{
|
||||||
@ -50,61 +47,9 @@ namespace SteamStorefrontAPI.Classes
|
|||||||
DateParseHandling = DateParseHandling.None,
|
DateParseHandling = DateParseHandling.None,
|
||||||
Converters = {
|
Converters = {
|
||||||
new ControllerSupportConverter(),
|
new ControllerSupportConverter(),
|
||||||
new CurrencyConverter(),
|
|
||||||
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
|
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,7 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="Classes\Converters.cs" />
|
||||||
<Compile Include="Endpoints\AppDetails.cs" />
|
<Compile Include="Endpoints\AppDetails.cs" />
|
||||||
<Compile Include="Classes\appdetails\LinuxRequirements.cs" />
|
<Compile Include="Classes\appdetails\LinuxRequirements.cs" />
|
||||||
<Compile Include="Classes\appdetails\MacRequirements.cs" />
|
<Compile Include="Classes\appdetails\MacRequirements.cs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user