Unity几个Json库的序列化简单比较
粗略需求是对比Unity自带的JsonUtility、Newtonsoft、LitJson、SimpleJson这几个库在序列化统一对象时的时间消耗。
- 要序列化的对象类定义
[Serializable]
public class ToSerializeObject
{
public int logtype;
public long userid;
public string platform;
public string clientversion;
public string message;
public string title;
}
- 比较代码
public class JsonTest : MonoBehaviour
{
private readonly StringBuilder _stringBuilder1 = new StringBuilder();
private readonly StringBuilder _stringBuilder2 = new StringBuilder();
private readonly StringBuilder _stringBuilder3 = new StringBuilder();
private readonly StringBuilder _stringBuilder4 = new StringBuilder();
private const int TestCount = 100000;
private readonly Stopwatch _stopwatch = new Stopwatch();
// Start is called before the first frame update
void Start()
{
ClearCache();
Debug.Log("Newtonsoft ============================");
_stopwatch.Start();
NewtonsoftJsonTest();
_stopwatch.Stop();
Debug.Log($"Json of Newtonsoft = {_stopwatch.ElapsedMilliseconds}");
Debug.Log("Unity ============================");
_stopwatch.Reset();
_stopwatch.Restart();
UnityJsonTest();
_stopwatch.Stop();
Debug.Log($"Json of Unity = {_stopwatch.ElapsedMilliseconds}");
Debug.Log("SimpleJson ============================");
_stopwatch.Reset();
_stopwatch.Restart();
SimpleJsonTest();
_stopwatch.Stop();
Debug.Log($"Json of SimpleJson = {_stopwatch.ElapsedMilliseconds}");
Debug.Log("LitJson ============================");
_stopwatch.Reset();
_stopwatch.Restart();
LitJsonTest();
_stopwatch.Stop();
Debug.Log($"Json of LitJson = {_stopwatch.ElapsedMilliseconds}");
}
/// <summary>
/// Newtonsoft的json序列化测试
/// </summary>
private void NewtonsoftJsonTest()
{
Profiler.BeginSample("Newtonsoft");
for (var i = 0; i < TestCount; i++)
{
var obj = new ToSerializeObject()
{
clientversion = $"client{i + 1}",
logtype = 1,
message = $"message {i + 1}",
platform = "android",
title = $"title {i + 1}",
userid = 1000 + i
};
_stringBuilder1.Append(JsonConvert.SerializeObject(obj));
_stringBuilder1.Append("#@#");
}
Profiler.EndSample();
Debug.Log($"message1 length = {_stringBuilder1.Length}, data = {_stringBuilder1.ToString()}");
}
/// <summary>
/// Unity自带json库的json序列化测试
/// </summary>
private void UnityJsonTest()
{
Profiler.BeginSample("Unity");
for (var i = 0; i < TestCount; i++)
{
var obj = new ToSerializeObject()
{
clientversion = $"client{i + 1}",
logtype = 1,
message = $"message {i + 1}",
platform = "android",
title = $"title {i + 1}",
userid = 1000 + i
};
_stringBuilder2.Append(JsonUtility.ToJson(obj));
_stringBuilder2.Append("#@#");
}
Profiler.EndSample();
Debug.Log($"message2 length = {_stringBuilder2.Length}, data = {_stringBuilder2.ToString()}");
}
/// <summary>
/// SimpleJson的json序列化测试
/// </summary>
private void SimpleJsonTest()
{
Profiler.BeginSample("SimpleJSON");
var jsonClass = new JSONClass();
for (var i = 0; i < TestCount; i++)
{
var obj = new ToSerializeObject()
{
clientversion = $"client{i + 1}",
logtype = 1,
message = $"message {i + 1}",
platform = "android",
title = $"title {i + 1}",
userid = 1000 + i
};
jsonClass.Add("clientversion", new JSONData(obj.clientversion));
jsonClass.Add("logtype", new JSONData(obj.logtype));
jsonClass.Add("message", new JSONData(obj.message));
jsonClass.Add("platform", new JSONData(obj.platform));
jsonClass.Add("title", new JSONData(obj.title));
jsonClass.Add("userid", new JSONData(obj.userid));
_stringBuilder3.Append(jsonClass.ToString());
_stringBuilder3.Append("#@#");
}
Profiler.EndSample();
Debug.Log($"message2 length = {_stringBuilder3.Length}, data = {_stringBuilder3.ToString()}");
}
/// <summary>
/// LitJson的json序列化测试
/// </summary>
private void LitJsonTest()
{
Profiler.BeginSample("LitJson");
for (var i = 0; i < TestCount; i++)
{
var obj = new ToSerializeObject()
{
clientversion = $"client{i + 1}",
logtype = 1,
message = $"message {i + 1}",
platform = "android",
title = $"title {i + 1}",
userid = 1000 + i
};
_stringBuilder4.Append(JsonMapper.ToJson(obj));
_stringBuilder4.Append("#@#");
}
Profiler.EndSample();
Debug.Log($"message2 length = {_stringBuilder4.Length}, data = {_stringBuilder4.ToString()}");
}
private void ClearCache()
{
_stringBuilder1.Clear();
_stringBuilder2.Clear();
_stringBuilder3.Clear();
_stringBuilder4.Clear();
}
- 结果
序列化次数\时间(ms) | Newtonsoft | Unity自带库 | SimpleJson | LitJson |
---|---|---|---|---|
10000 | 411 | 257 | 624 | 355 |
100000 | 3411 | 2606 | 9191 | 3468 |
- GC等
特意也看了下GC的消耗,利用unity的Profile在执行10000次的情况下的消耗
这里只是略微的比较了在序列化自定义对象的时候几种库的表现,目前能看到的是unity自带的库表现的是最好的,当然,不排除有使用的不正确的情况导致的情况。这个数据只有在实际这么使用的时候,才具有价值,其他情况下只做参考。