I can't find anything in the docs or in related questions.
I'm trying to deserialize following json file(tile_config.json):
{
"tile_info": [
{
"height": 32,
"width": 32
}
],
"tiles-new": [
{
"file": "tiles.png",
"tiles": [
{
"id": "t_dirt",
"fg": [
{ "weight":500, "sprite":640},
{ "weight":1, "sprite":3620},
{ "weight":1, "sprite":3621},
{ "weight":1, "sprite":3622}
],
"rotates": false
},
{
"id": "t_fence_rope",
"fg": 2016,
"bg": 640
}
]
}
]
}
The key "fg" can be an Integer or an Array of Objects as you can see in the example.
I tried deserializing with the following code:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace JSONTest
{
class Program
{
static void Main(string[] args)
{
load_json();
}
public static void load_json()
{
var ser = new DataContractJsonSerializer(typeof(TileConfig));
System.IO.FileStream file =
new System.IO.FileStream(".\\tile_config.json", System.IO.FileMode.Open);
var tileconfig = (TileConfig)ser.ReadObject(file);
file.Close();
var Tile0 = tileconfig.tiles_new[0].tiles[0];
Console.WriteLine(Tile0.id);
//Output: t_dirt
Console.WriteLine(Tile0.fg);
//Output: System.Object[]
Console.WriteLine(((Object[])Tile0.fg)[0]);
//Output: System.Object
//This doesn't work => InvalidCastException
// var try1 = (JSON_Loader.TileConfig.TileValues.TileIndex)((Object[])tileconfig.tiles_new[0].tiles[0].fg)[0];
//This doesn't work either => InvalidCastException
//var try2 = ((Dictionary<object,object>)tileconfig.tiles_new[0].tiles[0].fg);
//var try3 = ((Dictionary<string, object>)tileconfig.tiles_new[0].tiles[0].fg);
//For the second one this works
var Tile1 = tileconfig.tiles_new[0].tiles[1];
Console.WriteLine(Tile1.id);
//Output: t_fence_rope
Console.WriteLine(Tile1.fg);
//Output: 2016
var try4 = ((int)Tile1.fg);
Console.WriteLine(try4);
//Output: 2016
Console.ReadLine();
}
}
[DataContract]
public class TileConfig
{
[DataMember(Order = 0)]
public List<TileInfo> tile_info;
[DataMember(Name = "tiles-new", Order = 1)]
public List<TileValues> tiles_new;
[DataContract]
public class TileInfo
{
[DataMember(EmitDefaultValue = false, Order = 0)]
public int height;
[DataMember(EmitDefaultValue = false, Order = 1)]
public int width;
}
[DataContract]
public class TileValues
{
[DataMember(EmitDefaultValue = false, Order = 0)]
public string file;
[DataMember(EmitDefaultValue = false, Order = 5)]
public Tile[] tiles;
[DataContract]
public class Tile
{
[DataMember(EmitDefaultValue = false, Order = 0)]
public string id;
[DataMember(EmitDefaultValue = false, Order = 1)]
public Object fg;
[DataMember(EmitDefaultValue = false, Order = 2)]
public int bg;
[DataMember(EmitDefaultValue = false, Order = 3)]
bool rotates;
}
[DataContract]
public class TileIndex
{
[DataMember(EmitDefaultValue = false, Order = 0)]
public int weight;
[DataMember(EmitDefaultValue = false, Order = 1)]
public int[] sprite;
}
}
}
}
As you can see I tried to solve the issue by declaring fg as Object. This works partially. The file gets deserialized no problem. But I have no way(no way I found yet) to get the data of the "fg". I tried casting it to a Dictionary, casting it to TileIndex(as I would've guessed that's what's in it) and I tried to convert it to a string but that doesn't work or I don't know how to make it work in this case.
Now I'm guessing that fg is set to something as it's not null and ToString is returning System.Object. My question is what type did DataContractJsonSerializer use and store in this Object?