Unity Newtonsoft.Json 大对象序列化失败
- 💣崩溃了没?
- 😀替代方案
💣崩溃了没?
Newtonsoft.Json.JsonTextWriter:WriteValueInternal(string,Newtonsoft.Json.JsonToken)
InvalidCastException: Specified cast is not valid
😀替代方案
使用新的序列化库进行序列化,经过测试20M的Json没问题:传送门👈
使用方法:
var p = new Person { Age = 99, Name = "foobar" };// Object -> byte[] (UTF8)
byte[] result = Utf8Json.JsonSerializer.Serialize(p);// byte[] -> Object
var p2 = Utf8Json.JsonSerializer.Deserialize<Person>(result);// Object -> String
var json = Utf8Json.JsonSerializer.ToJsonString(p2);// Write to Stream
Utf8Json.JsonSerializer.Serialize(stream, p2);