0

Using RestSharp to retrieve JSON data, the JSON that is coming in looks like this...

{"data":{"NHL":[{"team_id":1,"team":"Colorado Avalanche","regular_season":{},"postseason":{}}]}}

The C# Objects I'm trying to serialize to looks like this.

public class NHLTeamStats
{
    [DeserializeAs(Name = "data")]
    public NHLTeamStatsData Data { get; set; }
}

public partial class NHLTeamStatsData
{
    [DeserializeAs(Name = "NHL")]
    public List<NHLTeamStatsNHL> NHL { get; set; }
}

public class NHLTeamStatsNHL
{
    public NHLTeamStatsNHL()
    {
        RegularSeason = new NHLTeamStatsSeason();
        Postseason = new NHLTeamStatsSeason();
    }

    [DeserializeAs(Name = "team_id")]
    public int TeamId { get; set; }

    [DeserializeAs(Name = "team")]
    public string Team { get; set; }

    [DeserializeAs(Name = "regular_season")]
    public NHLTeamStatsSeason RegularSeason { get; set; }

    [DeserializeAs(Name = "postseason")]
    public NHLTeamStatsSeason Postseason { get; set; }
}

public class NHLTeamStatsSeason
{

    [DeserializeAs(Name = "hits")]
    public int? Hits { get; set; }
    [DeserializeAs(Name = "goals")]
    public int? Goals { get; set; }
    [DeserializeAs(Name = "saves")]
    public int? Saves { get; set; }
    [DeserializeAs(Name = "assists")]
    public int? Assists { get; set; }
    [DeserializeAs(Name = "power_plays")]
    public int? PowerPlays { get; set; }
    [DeserializeAs(Name = "faceoffs_won")]
    public int? FaceoffsWon { get; set; }
    [DeserializeAs(Name = "games_played")]
    public int? GamesPlayed { get; set; }
    [DeserializeAs(Name = "faceoffs_lost")]
    public int? FaceoffsLost { get; set; }
    [DeserializeAs(Name = "shots_on_goal")]
    public int? ShotsOnGoal { get; set; }
    [DeserializeAs(Name = "penalty_minutes")]
    public int? PenaltyMinutes { get; set; }
    [DeserializeAs(Name = "power_plays_converted")]
    public int? PowerPlaysConverted { get; set; }
}

When I do the RestSharp Execute call the response is empty as you can see in this screenshot. If I do the Execute method without the type specified response comes back fine. It only occurs when the properties regular_season, postseason are set to empty objects "regular_season":{},"postseason":{}

When they have data it works and serializes fine.

Any thoughts?

Colburn
  • 1
  • 2
  • Possible duplicate of [RestSharp serialization to JSON, object is not using SerializeAs attribute as expected](https://stackoverflow.com/q/21633649/3744182) and [RestSharp Serialize/Deserialize Naming Conversion](https://stackoverflow.com/q/30037387/3744182). Agree? – dbc Sep 29 '18 at 12:04
  • No dont think so. The [DeserializeAs] attribute handles the name differences fine. It is when the object comes in empty { } instead of null or with data that the issue occurs. Example somthing like this does fine {"data":{"NHL":[{"team_id":1,"team":"Colorado Avalanche","regular_season":{ "hits": 25 },"postseason":{ "hits": 25 }}]}} but this does not and errors. {"data":{"NHL":[{"team_id":1,"team":"Colorado Avalanche","regular_season":{},"postseason":{}}]}} – Colburn Oct 02 '18 at 14:32

0 Answers0