JSON .Net Unity反序列化匿名对象字典
JSON .Net Unity Deserialize dictionary of anonymous objects
我有一个字典,我将其序列化到二进制文件中,然后使用 JSON .net 从 https://json.codeplex.com/
再次反序列化
字典可能包含任意对象(字符串、classes,甚至是列表)。每个 class 是 [System.Serializable]
在序列化时,我添加 serializer.TypeNameHandling = TypeNameHandling.All;确保反序列化器具有反序列化字典所需的类型信息。
我无法将它正确反序列化为相同的对象列表,我只在我的容器中得到 JObjects 而不是原始类型。谁能帮我完成这个?
提前致谢;
洛朗
更新:
为了输入/输出数据,我使用了这两种方法:
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
反序列化和序列化时都需要设置serializer.TypeNameHandling = TypeNameHandling.All
。否则,"$type"
属性 将被忽略。
因此:
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.
}
}
我有一个字典,我将其序列化到二进制文件中,然后使用 JSON .net 从 https://json.codeplex.com/
再次反序列化字典可能包含任意对象(字符串、classes,甚至是列表)。每个 class 是 [System.Serializable]
在序列化时,我添加 serializer.TypeNameHandling = TypeNameHandling.All;确保反序列化器具有反序列化字典所需的类型信息。
我无法将它正确反序列化为相同的对象列表,我只在我的容器中得到 JObjects 而不是原始类型。谁能帮我完成这个?
提前致谢; 洛朗
更新:
为了输入/输出数据,我使用了这两种方法:
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
反序列化和序列化时都需要设置serializer.TypeNameHandling = TypeNameHandling.All
。否则,"$type"
属性 将被忽略。
因此:
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.
}
}