skip to Main Content

I am trying to deserialize the response from the SteamAPI in C# .NET 7. I make the REST request via RestSharp and save the content as a file. Later I load the file and want to deserialize it to search it with LINQ. But with various options of JsonSerializerOptions it does not work. The SteamAppList.AppList is always null.

For the data model I used Json2Csharp and Jetbrains Rider.

I tried different JsonSerializerOptions and did debugging via vscode and Jetbrains Rider.

Here is my model (SteamAppList.cs):

[Serializable]
    public class SteamAppList
    {
        [JsonPropertyName("applist")]
        public Applist Applist;
    }
    [Serializable]
    public class Applist
    {
        [JsonPropertyName("apps")]
        public List<App> Apps;
    }
    [Serializable]
    public class App
    {
        [JsonPropertyName("appid")]
        public int Appid;

        [JsonPropertyName("name")]
        public string Name;
    }

And my SteamApps.cs for the request and the deserialisation:

public class SteamApps
    {
        private readonly CancellationToken cancellationToken;

        private async Task<RestResponse> GetSteamApps()
        {
            var client = new RestClient();
            var request = new RestRequest("https://api.steampowered.com/ISteamApps/GetAppList/v2/");
            request.AddHeader("Accept", "*/*");
            request.AddHeader("User-Agent", "Major GSM");
            var response = await client.GetAsync(request, cancellationToken);
            return response;
        }

        public bool RetireveAndSaveSteamApps()
        {
            var path = FileModule.GetDirectoryPath(MajorGsmLibrary.ApplicationDirectory.Games);
            var fileName = "steam.library.json";
            RestResponse restResponse = GetSteamApps().Result;
            
            try
            {
                File.WriteAllText(Path.Combine(path, fileName), restResponse.Content);
            }
            catch (IOException exception)
            {
                LogModule.WriteError("Could not write steam libarary file!", exception);
                return false;
            }
            catch (ArgumentNullException exception)
            {
                LogModule.WriteError("Missing data for steam library file", exception);
                return false;
            }
            return true;
        }

        public static string CheckGameAgainstSteamLibraryWithAppId(string appId)
        {
            var path = FileModule.GetDirectoryPath(MajorGsmLibrary.ApplicationDirectory.Games);
            var fileName = "steam.library.json";
            var fileData =
                File.ReadAllText(Path.Combine(path, fileName));
            SteamAppList? steamAppList = JsonSerializer.Deserialize<SteamAppList>(fileData, new JsonSerializerOptions { MaxDepth = 64, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });

            if (steamAppList!.Applist != null)
            {
                var app = steamAppList.Applist.Apps.SingleOrDefault(x => x.Appid.ToString() == appId);
                if (app != null) return app.Name;
            }

            return "Unknown";
        }
    }

Im calling the SteamApps-class from a GTK# Application:

foreach (var game in Games)
            {
                var appName = SteamApps.CheckGameAgainstSteamLibraryWithAppId(game.AppId);
                if (appName == "Unknown") return;
                game.FoundInSteamLibrary = true;
                game.SteamLibraryName = appName;
            }

The JSON I’m receiving looks like this:

{
"applist": {
    "apps": [
        {
            "appid": 1941401,
            "name": ""
        },
        {
            "appid": 1897482,
            "name": ""
        },
        {
            "appid": 2112761,
            "name": ""
        },
        {
            "appid": 1470110,
            "name": "Who's Your Daddy Playtest"
        },
        {
            "appid": 2340170,
            "name": "Melon Knight"
        },
        {
            "appid": 1793090,
            "name": "Blockbuster Inc."
        }
    ]
}

}

2

Answers


  1. Chosen as BEST ANSWER

    I played around with the model a bit more and found a solution. Here is the matching model:

    public class SteamAppList
    {
        [JsonPropertyName("applist")]
        public Applist Applist { get; set; }
    }
    
    public class Applist
    {
        [JsonPropertyName("apps")]
        public App[] Apps { get; set; }
    }
    
    public class App
    {
        [JsonPropertyName("appid")]
        public long Appid { get; set; }
    
        [JsonPropertyName("name")]
        public string? Name { get; set; }
    }
    

  2. You have an error in the model classes. AppList class has an object, not a list. These are proper classes for deserialization.

    public class SteamAppList
    {
        public Applist applist { get; set; }
    }
    
    public class Applist
    {
        public Apps apps { get; set; }
    }
    
    public class Apps
    {
        public List<App> app { get; set; }
    }
    
    public class App
    {
        public int appid { get; set; }
        public string app_type { get; set; }
    }
    

    Example response from the doc:

    {"applist":{"apps":{"app":[{
        "appid": 500,
        "app_type": "game"
    },
    {
        "appid": 222840,
        "app_type": "tool"
    },
    {
        "appid": 222860,
        "app_type": "tool"
    }   ]}}}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search