-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathJsonUtilityTest.cs
85 lines (69 loc) · 2.5 KB
/
JsonUtilityTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
namespace TinaX.Tests.Core
{
public class JsonUtilityTest
{
[Test]
public void TestSerializeDict()
{
var myData = new Dictionary<string, string>();
myData.Add("Id", "0");
myData.Add("Name", "Alice");
//Serialize
var json1 = JsonUtility.ToJson(new Serialization<string,string>(myData));
TestContext.WriteLine("Json 1: \n" + json1);
var json_obj1 = JsonUtility.FromJson<Serialization<string, string>>(json1)
.ToDictionary();
Assert.AreEqual(myData, json_obj1);
//By Ext Method
var json2 = myData.ToJson();
TestContext.WriteLine("Json 2: \n" + json2);
var json_obj2 = JsonHelper.FromJsonToDictionary<string, string>(json2);
Assert.AreEqual(json_obj1, json_obj2);
}
[Test]
public void TestSerializeList()
{
var myData = new List<AModel>
{
new AModel("Alice", Guid.NewGuid()),
new AModel("Bob", Guid.NewGuid())
};
//serialize
var json1 = JsonUtility.ToJson(new Serialization<AModel>(myData));
TestContext.WriteLine("Json 1: \n" + json1);
var json_obj1 = JsonUtility.FromJson<Serialization<AModel>>(json1)
.ToList();
//Assert.AreEqual(myData, json_obj1);
//Ext Method
var json2 = myData.ToJson();
TestContext.WriteLine("Json 2:\n" + json2);
var json_obj2 = JsonHelper.FromJsonToList<AModel>(json2);
//Assert.AreEqual(json_obj1, json_obj2);
}
#pragma warning disable CA2235 // Mark all non-serializable fields
[System.Serializable]
#pragma warning disable CA1034 // Nested types should not be visible
public class AModel
#pragma warning restore CA1034 // Nested types should not be visible
{
public string Name;
public string Guid;
public override string ToString()
{
return $"Name:{this.Name}\nGuid:{this.Guid}";
}
public AModel(string name, Guid guid)
{
Name = name;
this.Guid = guid.ToString();
}
}
#pragma warning restore CA2235 // Mark all non-serializable fields
}
}