Jack
Jack

Reputation: 131

Complex data model and json.net serizalization problems

I have the following data model

class Tournament{
    int Id { get; set;}
    ICollection<League> Leagues { get; set;}
}

class League{
    int Id {get;set;};
    Tournament Parent { get; set;}
    ICollection<Player> Players { get; set;}
    ICollection<Round>  { get; set;}
}
 class Player{
    int Id  { get; set;}
     League Parent  { get; set;}
     ICollection<Game> Games { get; set;}
 }
 class Round{
    int Id  { get; set;}
    ICollection<Game> Games { get; set;}
    League Parent  { get; set;}
}

class Game{
 int Id { get; set;}
 Player playerOne { get; set;}
 Player playerTwo  { get; set;}
 Round Parent { get; set;}
}

Which I am having trouble serializing and deserializing using json.net newtsoft.

I am getting the following file https://api.myjson.com/bins/1aa4kl

Using the following JSON net settings

  JsonConvert.DefaultSettings = () => new JsonSerializerSettings
      {
        DateTimeZoneHandling = DateTimeZoneHandling.Local,
        TypeNameHandling = TypeNameHandling.Objects,
        Formatting = Formatting.Indented,
        ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
        PreserveReferencesHandling = PreserveReferencesHandling.Objects,
        };

But as you can see from the output file it is not correct, I would like so that the games store references to the players and games but it is not referencing them correctly. And on desrizaling none of the parent child relationships are being mapped except for league to tournament.

I have no data attributes on any of the classes.

Can anyone advise on what is going wrong? Do I need to change the data model?

Thanks

Upvotes: 3

Views: 94

Answers (1)

Felix D.
Felix D.

Reputation: 5123

I managed to get a working model:

public class JsonRoot
{
    [JsonProperty("$id")]
    public string Id_X { get; set; }
    [JsonProperty("$type")]
    public string Type_X { get; set; }
    public bool UploadedToDatabase { get; set; }
    public string Id { get; set; }
    public bool TournamentStarted { get; set; }
    public int MaximumNumberOfRounds { get; set; }
    public string Name { get; set; }
    public League[] Leagues { get; set; }
    public object SaveFilePath { get; set; }
    public object SavedFileName { get; set; }
    public object DefaultPairingMethod { get; set; }
    public int DefaultPairingLag { get; set; }
    public bool IsTournamentRanked { get; set; }
    public string UserId { get; set; }
    public object StreamUrl { get; set; }
    public string StartTime { get; set; }
    public string EndTime { get; set; }
    public int NumberOfPlacesForPrizes { get; set; }
    public bool isTournamentDirector { get; set; }
}

[JsonProperty("$id")] is needed here cause the $ is not allowed in propertynames !


public class League
{
    [JsonProperty("$id")]
    public string ID_X { get; set; }
    [JsonProperty("$type")]
    public string Type_X { get; set; }
    public bool UploadedToDatabase { get; set; }
    public string Id { get; set; }
    public object Name { get; set; }
    public int NumberOfRounds { get; set; }
    public object Parent { get; set; }
    public Player[] Players { get; set; }
    public Round[] Rounds { get; set; }
    public bool LeagueStarted { get; set; }
    public object Prizes { get; set; }
}

public class Round
{
    [JsonProperty("$id")]
    public string ID_X { get; set; }
    [JsonProperty("$type")]
    public string Type_X { get; set; }
    public bool UploadedToDatabase { get; set; }
    public string Id { get; set; }
    public object Parent { get; set; }
    public List<Game> Games { get; set; }
    public bool HasStarted { get; set; }
    public string TimeStarted { get; set; }
    public int RoundNumber { get; set; }
}

public class Game
{
    [JsonProperty("$id")]
    public string ID_X { get; set; }
    [JsonProperty("$type")]
    public string Type_X { get; set; }
    public object Parent { get; set; }
    public bool UploadedToDatabase { get; set; }
    public string Id { get; set; }
    public int TableNumber { get; set; }
    public string PlayerOneId { get; set; }
    public string PlayerTwoId { get; set; }
    public object PlayerOne { get; set; }
    public object PlayerTwo { get; set; }
    public int PlayerOneScore { get; set; }
    public int PlayerTwoScore { get; set; }
    public object HighestScoringWord { get; set; }
    public string ScoresSubmittedDateTime { get; set; }
}

public class Player
{
    [JsonProperty("$id")]
    public string ID_X { get; set; }
    [JsonProperty("$type")]
    public string Type_X { get; set; }
    public bool UploadedToDatabase { get; set; }
    public string Id { get; set; }
    public object Parent { get; set; }
    public List<Game> Games { get; set; }
    public int PlayerId { get; set; }
    public string FirstName { get; set; }
    public string Lastname { get; set; }
    public object Username { get; set; }
    public object Email { get; set; }
    public double Rating { get; set; }
    public int PointsFor { get; set; }
    public int PointsAgainst { get; set; }
    public double NumberOfWins { get; set; }
    public double NumberOfDraws { get; set; }
    public double NumberOfLosses { get; set; }
    public int Spread { get; set; }
    public string FullName { get; set; }
    public bool IsSelected { get; set; }      
}

I was trying to solve the 'parent' problem like this but it's not serializing with that:

public class Reference
{
    [JsonProperty("$ref")]
    public string ref_id { get; set; }
}

but I ended up using object instead...

I am sure this can be improved, but it should show you the way to go :D

You can replace string Id with Guid and stuff...

For debuggin I recommend adding some override ToString() to the classes.

Tested like that:

var test = JsonConvert.DeserializeObject<JsonRoot>(json);

Upvotes: 1

Related Questions