sdpgames
sdpgames

Reputation: 23

JSON .Net Unity Deserialize dictionary of anonymous objects

I have a Dictionary that I serialize onto a binary file, and deserialize back again using JSON .net from https://json.codeplex.com/

The dictionary may contain abritrary objects (string, classes, even List). Each class is [System.Serializable]

At serialize time, I add serializer.TypeNameHandling = TypeNameHandling.All; to make sure the deserializer has the type info required to deserialize the dictionary.

I am unable to deserialize it correctly to the identical list of object, I only get JObjects in my container and not the original type. Can anyone help me accomplish this?

Thanks in advance; Laurent

Update:

To get data in / out I use those two methods:

public static byte[] SerializeToByteArray<T>(T data)
{
    byte[] serializedData = new byte[]{}; 

    using(var stream  = new MemoryStream())
    {
        using (BsonWriter writer = new BsonWriter(stream))
        {
            JsonSerializer serializer = new JsonSerializer();
            serializer.TypeNameHandling = TypeNameHandling.All;
            serializer.Serialize(writer, data);
        }

        return stream.ToArray();
    }
}

public static T DeserializeFromByteArray<T>(byte[] serializedData )
{
    using (var stream = new MemoryStream(serializedData))
    {
        using (BsonReader reader = new BsonReader(stream))
        {
            JsonSerializer serializer = new JsonSerializer();
            return (T)serializer.Deserialize<T>( reader );
        }
    }
}

[System.Serializable]
public class FavoriteLevel
{
public FavoriteLevel(string ID, int TYPE) { id = ID; type = TYPE;}
public string id;
public int type;
}

Dictionary<string,object> dict = new Dictionary<string,object>(1);
List<FavoriteLevel> levels = new List<FavoriteLevel>(1);
levels.Add (new FavoriteLevel("123",FavoriteType.Favorite) );
dict.Add ( "123", levels );        
byte[] data = SerializeToByteArray( dict );

Dictionary<string,object> incomingDict =     DeserializeFromByteArray<Dictionary<string,object>>( data );

object listBack = incomingDict["123"];
// ERROR: listBack is a Json object and not a List<FavoriteLevel> object

Upvotes: 1

Views: 2206

Answers (1)

dbc
dbc

Reputation: 116991

You need to set serializer.TypeNameHandling = TypeNameHandling.All when deserializing as well as serializing. Otherwise, the "$type" property will be ignored.

Thus:

public static class JsonExtensions
{
    public static byte[] SerializeToByteArray<T>(T data, JsonSerializerSettings settings)
    {
        using (var stream = new MemoryStream())
        {
            using (var writer = new BsonWriter(stream))
            {
                JsonSerializer serializer = JsonSerializer.Create(settings);
                serializer.Serialize(writer, data);
            }
            return stream.ToArray();
        }
    }

    public static T DeserializeFromByteArray<T>(byte[] serializedData, JsonSerializerSettings settings)
    {
        using (var stream = new MemoryStream(serializedData))
        {
            using (var reader = new BsonReader(stream))
            {
                JsonSerializer serializer = JsonSerializer.Create(settings);
                return (T)serializer.Deserialize<T>(reader);
            }
        }
    }
}

public static class TestClass
{
    public static void Test()
    {
        Dictionary<string, object> dict = new Dictionary<string, object>(1);
        List<FavoriteLevel> levels = new List<FavoriteLevel>(1);
        levels.Add(new FavoriteLevel("123", 0));
        dict.Add("123", levels);

        var settings = new JsonSerializerSettings();
        settings.TypeNameHandling = TypeNameHandling.All;

        byte[] data = JsonExtensions.SerializeToByteArray(dict, settings);

        Dictionary<string, object> incomingDict = JsonExtensions.DeserializeFromByteArray<Dictionary<string, object>>(data, settings);

        object listBack = incomingDict["123"];

        Debug.Assert(listBack.GetType() == levels.GetType()); // No assert.
    }
}

Upvotes: 1

Related Questions