I've been working on a music game and decided to add conversion of other games' levels. One of the games I decided to convert from uses JSON to store it's levels and so I'm using Newtonsoft.Json for deserializing level data. Level have can 2 object types that are stored in a single array/list with one shared property and one individual property. Keeping that in mind I made level class with it's properties, one base and two inherited classes:
class Chart
{
//Some unimportant properties
//...
public Note[] note;
class Note
{
public int[] beat;
}
class NoteSingle : Note
{
public int x;
}
class NoteRain : Note
{
public int[] endbeat;
}
}
However, when I try deserialize level, note only contains base objects. I tried creating JsonSerializerSettings with TypeNameHandling set to All and passing it to deserialization method, but it didn't worked, note still only have base classes in it.
Basically I need to load level from the file, deserialize it as Chart and make each of the notes in note be one of the types inherited from Note depending on json data. Like if note has x field then load it as NoteSingle and if it has endbeat field then load it as NoteRain.
Repres
class Note
{
public int[] beat;
}
class NoteSingle : Note
{
public int x;
}
class NoteRain : Note
{
public int[] endbeat;
}
class Chart
{
//Some unimportant properties
public Note[] note;
//Some unimportant properties
}
public static void Convert(string path)
{
string rawData = File.ReadAllText(path);
JsonSerializerSettings setts = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.All
};
Chart ch = JsonConvert.DeserializeObject<Chart>(rawData, setts);
//Level converter code
}
Example data I'm trying to load: https://pastebin.com/zgnRsgWZ
What am I doing wrong?
notefrom C#, you can also not accessxand such.xfield then load it asNoteSingleand if it hasendbeatfield then load it asNoteRain. As for repres give me some time, I'll edit this comment