1+ using Newtonsoft . Json ;
2+ using Newtonsoft . Json . Linq ;
3+ using SteamWebAPI2 . Models . CSGO ;
4+ using System ;
5+ using System . Collections . Generic ;
6+ using System . Reflection ;
7+
8+ namespace SteamWebAPI2 . Utilities . JsonConverters
9+ {
10+ internal class GameMapsPlaytimeConverter : JsonConverter
11+ {
12+ public override void WriteJson ( JsonWriter writer , object value , JsonSerializer serializer )
13+ {
14+ throw new NotImplementedException ( ) ;
15+ }
16+
17+ /// <summary>
18+ /// Custom deserialization required because raw API response is awful. Instead of returning real JSON objects, the API
19+ /// returns an array of "keys" and a matrix of "rows". We need to match up the keys to the columns within the rows
20+ /// to figure out which value goes with which key.
21+ ///
22+ /// Example response:
23+ ///
24+ /// "Keys": ["Key 1", "Key 2", "Key 3"],
25+ /// "Rows": [
26+ /// ["A 1", "A 2", "A 3"],
27+ /// ["B 1", "B 2", "B 3"]
28+ /// ]
29+ /// </summary>
30+ /// <param name="reader"></param>
31+ /// <param name="objectType"></param>
32+ /// <param name="existingValue"></param>
33+ /// <param name="serializer"></param>
34+ /// <returns></returns>
35+ public override object ReadJson ( JsonReader reader , Type objectType , object existingValue , JsonSerializer serializer )
36+ {
37+ if ( reader . TokenType == JsonToken . Null )
38+ {
39+ return null ;
40+ }
41+
42+ List < GameMapsPlaytime > playtimes = new List < GameMapsPlaytime > ( ) ;
43+
44+ JObject o = JObject . Load ( reader ) ;
45+
46+ var keys = o [ "Keys" ] ;
47+ var rows = o [ "Rows" ] ;
48+
49+ foreach ( var row in rows )
50+ {
51+ int columnIndex = 0 ;
52+ GameMapsPlaytime playtime = new GameMapsPlaytime ( ) ;
53+ foreach ( var value in row )
54+ {
55+ var key = keys [ columnIndex ] ;
56+ if ( key . ToString ( ) == "IntervalStartTimeStamp" )
57+ {
58+ playtime . IntervalStartTimeStamp = ulong . Parse ( value . ToString ( ) ) ;
59+ }
60+ else if ( key . ToString ( ) == "MapName" )
61+ {
62+ playtime . MapName = value . ToString ( ) ;
63+ }
64+ else if ( key . ToString ( ) == "RelativePercentage" )
65+ {
66+ playtime . RelativePercentage = float . Parse ( value . ToString ( ) ) ;
67+ }
68+ columnIndex ++ ;
69+ }
70+ playtimes . Add ( playtime ) ;
71+ }
72+
73+ return new GameMapsPlaytimeResult ( )
74+ {
75+ Playtimes = playtimes
76+ } ;
77+ }
78+
79+ public override bool CanWrite { get { return false ; } }
80+
81+ public override bool CanConvert ( Type objectType )
82+ {
83+ return typeof ( GameMapsPlaytimeResult ) . GetTypeInfo ( ) . IsAssignableFrom ( objectType . GetTypeInfo ( ) ) ;
84+ }
85+ }
86+ }
0 commit comments