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
I played around with the model a bit more and found a solution. Here is the matching model:
You have an error in the model classes.
AppList
class has an object, not a list. These are proper classes for deserialization.Example response from the doc: