Skip to content

Commit 24262a8

Browse files
committed
Add UnityTest
1 parent 8071573 commit 24262a8

26 files changed

+1919
-0
lines changed

src/UnityTest/.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/[Ll]ibrary/
2+
/[Tt]emp/
3+
/[Oo]bj/
4+
/[Oo]utput/
5+
/[Bb]uild/
6+
7+
# Autogenerated VS/MD solution and project files
8+
*.csproj
9+
*.unityproj
10+
*.sln
11+
*.suo
12+
*.tmp
13+
*.user
14+
*.userprefs
15+
*.pidb
16+
*.booproj
17+
18+
# Unity3D generated meta files
19+
*.pidb.meta
20+
21+
# Unity3D Generated File On Crash Reports
22+
sysinfo.txt
23+
24+
# UnityPackage
25+
*.unitypackage
26+
27+
# Artifacts generated when build
28+
Assets/Newtonsoft.Json.dll*
29+
Assets/Editor/Newtonsoft.Json.Tests.dll*
30+
SpaceShipV2.bson

src/UnityTest/Assets/Editor.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEngine.UI;
4+
using System.Collections.Generic;
5+
using Newtonsoft.Json;
6+
using Newtonsoft.Json.Linq;
7+
8+
public class JsonNetSample : MonoBehaviour
9+
{
10+
public Text Output;
11+
12+
void Start()
13+
{
14+
Output.text = "Start!\n\n";
15+
16+
SerailizeJson();
17+
DeserializeJson();
18+
LinqToJson();
19+
JsonPath();
20+
21+
WriteLine("\nDone!");
22+
}
23+
24+
void WriteLine(string msg)
25+
{
26+
Output.text = Output.text + msg + "\n";
27+
}
28+
29+
public class Product
30+
{
31+
public string Name;
32+
public DateTime ExpiryDate = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);
33+
public decimal Price;
34+
public string[] Sizes;
35+
36+
public override bool Equals(object obj)
37+
{
38+
if (obj is Product)
39+
{
40+
Product p = (Product)obj;
41+
42+
return (p.Name == Name && p.ExpiryDate == ExpiryDate && p.Price == Price);
43+
}
44+
45+
return base.Equals(obj);
46+
}
47+
48+
public override int GetHashCode()
49+
{
50+
return (Name ?? string.Empty).GetHashCode();
51+
}
52+
}
53+
54+
void SerailizeJson()
55+
{
56+
WriteLine("* SerailizeJson");
57+
58+
Product product = new Product();
59+
product.Name = "Apple";
60+
product.ExpiryDate = new DateTime(2008, 12, 28);
61+
product.Sizes = new string[] { "Small" };
62+
63+
string json = JsonConvert.SerializeObject(product);
64+
WriteLine(json);
65+
}
66+
67+
public class Movie
68+
{
69+
public string Name { get; set; }
70+
public string Description { get; set; }
71+
public string Classification { get; set; }
72+
public string Studio { get; set; }
73+
public DateTime? ReleaseDate { get; set; }
74+
public List<string> ReleaseCountries { get; set; }
75+
}
76+
77+
void DeserializeJson()
78+
{
79+
WriteLine("* DeserializeJson");
80+
81+
string json = @"{
82+
'Name': 'Bad Boys',
83+
'ReleaseDate': '1995-4-7T00:00:00',
84+
'Genres': [
85+
'Action',
86+
'Comedy'
87+
]
88+
}";
89+
90+
Movie m = JsonConvert.DeserializeObject<Movie>(json);
91+
92+
string name = m.Name;
93+
WriteLine(name);
94+
}
95+
96+
void LinqToJson()
97+
{
98+
WriteLine("* LinqToJson");
99+
100+
JArray array = new JArray();
101+
array.Add("Manual text");
102+
array.Add(new DateTime(2000, 5, 23));
103+
104+
JObject o = new JObject();
105+
o["MyArray"] = array;
106+
107+
string json = o.ToString();
108+
WriteLine(json);
109+
}
110+
111+
private void JsonPath()
112+
{
113+
WriteLine("* JsonPath");
114+
115+
var o = JObject.Parse(@"{
116+
'Stores': [
117+
'Lambton Quay',
118+
'Willis Street'
119+
],
120+
'Manufacturers': [
121+
{
122+
'Name': 'Acme Co',
123+
'Products': [
124+
{
125+
'Name': 'Anvil',
126+
'Price': 50
127+
}
128+
]
129+
},
130+
{
131+
'Name': 'Contoso',
132+
'Products': [
133+
{
134+
'Name': 'Elbow Grease',
135+
'Price': 99.95
136+
},
137+
{
138+
'Name': 'Headlight Fluid',
139+
'Price': 4
140+
}
141+
]
142+
}
143+
]
144+
}");
145+
146+
JToken acme = o.SelectToken("$.Manufacturers[?(@.Name == 'Acme Co')]");
147+
WriteLine(acme.ToString());
148+
149+
IEnumerable<JToken> pricyProducts = o.SelectTokens("$..Products[?(@.Price >= 50)].Name");
150+
foreach (var item in pricyProducts)
151+
{
152+
WriteLine(item.ToString());
153+
}
154+
}
155+
}

src/UnityTest/Assets/JsonNetSample.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)