1

I would like to deserialize a json containing at least 2 different lists and those lists contains inherited objects.

Here is my json :

{
    "difficulty": 1,
    "plateformes": [
        {
            "$type" : "Immobile, Assembly-CSharp",
            "x": 19,
            "y": 4.5,
            "friable": false
        },
        {
            "$type" : "Mobile, Assembly-CSharp",
            "x": 40,
            "y": 4.5,
            "finX": 45,
            "finY": 4.5
        }],
    "items": [
        {
            "$type": "Jumpboost, Assembly-CSharp",
            "temporaire": true,
            "x": 34,
            "y": 5,
            "duree": 10
        },
        {
            "$type": "Life+1, Assembly-CSharp",
            "temporaire": false,
            "x": 74,
            "y": 5
        }]
}

Here are my classes :

public class Level{

   public int difficulte; 
   public List<Plateforme> plateformes { get; set; }
   public List<Items> items { get; set; }
   ...
}

public class Plateforme {
   public int largeur;
   public float positionX;
   public float positionY;
}

public class Mobile : Plateforme {
   public float positionFinX;
   public float positionFinY;
}

public class Immobile : Plateforme {
   public bool friable;
}

public class Items {
   private bool temporaire;
   private float x;
   private float y;
}
public class Life+1 : Items {}
public class JumpBoost : Items{}


public class LevelGeneration {
   Level level = new Level();
   void Start () {
      string json = JsonConvert.SerializeObject(level, Formatting.Indented, new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.Auto
        });
    level = JsonConvert.DeserializeObject<Level>(json, settings);
   }
}

The code of LevelGeneration doesn't work. I would like to fill my Mobile, Immobile, Life+1 and JumpBoost classes with the json

I have already seen topics about inherited list : Json.net serialize/deserialize derived types? http://gigi.nullneuron.net/gigilabs/deserializing-derived-types-with-json-net/

but never with several lists

Please help me understand how can I do ! Thanks!

Community
  • 1
  • 1
tib0
  • 11
  • 1

0 Answers0