1
2package com.badlogic.gdx.tests;
3
4import java.util.ArrayList;
5
6import com.badlogic.gdx.tests.utils.GdxTest;
7import com.badlogic.gdx.utils.Array;
8import com.badlogic.gdx.utils.Json;
9import com.badlogic.gdx.utils.ObjectMap;
10import com.badlogic.gdx.utils.reflect.ArrayReflection;
11
12public class JsonTest extends GdxTest {
13	Json json;
14
15	public void create () {
16		json = new Json();
17
18// json.fromJson(Test1.class, //
19// "{byteArrayField:[-1\n,-2]}"
20// );
21// if (true) return;
22
23		Test1 test = new Test1();
24		test.booleanField = true;
25		test.byteField = 123;
26		test.charField = 'Z';
27		test.shortField = 12345;
28		test.intField = 123456;
29		test.longField = 123456789;
30		test.floatField = 123.456f;
31		test.doubleField = 1.23456d;
32		test.BooleanField = true;
33		test.ByteField = -12;
34		test.CharacterField = 'X';
35		test.ShortField = -12345;
36		test.IntegerField = -123456;
37		test.LongField = -123456789l;
38		test.FloatField = -123.3f;
39		test.DoubleField = -0.121231d;
40		test.stringField = "stringvalue";
41		test.byteArrayField = new byte[] {2, 1, 0, -1, -2};
42		test.map = new ObjectMap();
43		test.map.put("one", 1);
44		test.map.put("two", 2);
45		test.map.put("nine", 9);
46		test.stringArray = new Array();
47		test.stringArray.add("meow");
48		test.stringArray.add("moo");
49		test.objectArray = new Array();
50		test.objectArray.add("meow");
51		test.objectArray.add(new Test1());
52		test.someEnum = SomeEnum.b;
53		roundTrip(test);
54
55		test.someEnum = null;
56		roundTrip(test);
57
58		test = new Test1();
59		roundTrip(test);
60
61		test.stringArray = new Array();
62		roundTrip(test);
63
64		test.stringArray.add("meow");
65		roundTrip(test);
66
67		test.stringArray.add("moo");
68		roundTrip(test);
69
70		test = new Test1();
71		test.map = new ObjectMap();
72		roundTrip(test);
73
74		test.map.put("one", 1);
75		roundTrip(test);
76
77		test.map.put("two", 2);
78		test.map.put("nine", 9);
79		roundTrip(test);
80
81		test.map.put("\nst\nuff\n", 9);
82		test.map.put("\r\nst\r\nuff\r\n", 9);
83		roundTrip(test);
84
85		equals(json.toJson("meow"), "meow");
86		equals(json.toJson("meow "), "\"meow \"");
87		equals(json.toJson(" meow"), "\" meow\"");
88		equals(json.toJson(" meow "), "\" meow \"");
89		equals(json.toJson("\nmeow\n"), "\\nmeow\\n");
90		equals(json.toJson(Array.with(1, 2, 3), null, int.class), "[1,2,3]");
91		equals(json.toJson(Array.with("1", "2", "3"), null, String.class), "[1,2,3]");
92		equals(json.toJson(Array.with(" 1", "2 ", " 3 "), null, String.class), "[\" 1\",\"2 \",\" 3 \"]");
93		equals(json.toJson(Array.with("1", "", "3"), null, String.class), "[1,\"\",3]");
94
95		System.out.println();
96		System.out.println("Success!");
97	}
98
99	private String roundTrip (Object object) {
100		String text = json.toJson(object);
101		System.out.println(text);
102		test(text, object);
103
104		text = json.prettyPrint(object, 130);
105		test(text, object);
106
107		return text;
108	}
109
110	private void test (String text, Object object) {
111		check(text, object);
112
113		text = text.replace("{", "/*moo*/{/*moo*/");
114		check(text, object);
115
116		text = text.replace("}", "/*moo*/}/*moo*/");
117		text = text.replace("[", "/*moo*/[/*moo*/");
118		text = text.replace("]", "/*moo*/]/*moo*/");
119		text = text.replace(":", "/*moo*/:/*moo*/");
120		text = text.replace(",", "/*moo*/,/*moo*/");
121		check(text, object);
122
123		text = text.replace("/*moo*/", " /*moo*/ ");
124		check(text, object);
125
126		text = text.replace("/*moo*/", "// moo\n");
127		check(text, object);
128
129		text = text.replace("\n", "\r\n");
130		check(text, object);
131
132		text = text.replace(",", "\n");
133		check(text, object);
134
135		text = text.replace("\n", "\r\n");
136		check(text, object);
137
138		text = text.replace("\r\n", "\r\n\r\n");
139		check(text, object);
140	}
141
142	private void check (String text, Object object) {
143		Object object2 = json.fromJson(object.getClass(), text);
144		equals(object, object2);
145	}
146
147	private void equals (Object a, Object b) {
148		if (!a.equals(b)) throw new RuntimeException("Fail!\n" + a + "\n!=\n" + b);
149	}
150
151	static public class Test1 {
152		// Primitives.
153		public boolean booleanField;
154		public byte byteField;
155		public char charField;
156		public short shortField;
157		public int intField;
158		public long longField;
159		public float floatField;
160		public double doubleField;
161		// Primitive wrappers.
162		public Boolean BooleanField;
163		public Byte ByteField;
164		public Character CharacterField;
165		public Short ShortField;
166		public Integer IntegerField;
167		public Long LongField;
168		public Float FloatField;
169		public Double DoubleField;
170		// Other.
171		public String stringField;
172		public byte[] byteArrayField;
173		public Object object;
174		public ObjectMap<String, Integer> map;
175		public Array<String> stringArray;
176		public Array objectArray;
177		public SomeEnum someEnum;
178
179		public boolean equals (Object obj) {
180			if (this == obj) return true;
181			if (obj == null) return false;
182			if (getClass() != obj.getClass()) return false;
183			Test1 other = (Test1)obj;
184			if (BooleanField == null) {
185				if (other.BooleanField != null) return false;
186			} else if (!BooleanField.equals(other.BooleanField)) return false;
187			if (ByteField == null) {
188				if (other.ByteField != null) return false;
189			} else if (!ByteField.equals(other.ByteField)) return false;
190			if (CharacterField == null) {
191				if (other.CharacterField != null) return false;
192			} else if (!CharacterField.equals(other.CharacterField)) return false;
193			if (DoubleField == null) {
194				if (other.DoubleField != null) return false;
195			} else if (!DoubleField.equals(other.DoubleField)) return false;
196			if (FloatField == null) {
197				if (other.FloatField != null) return false;
198			} else if (!FloatField.equals(other.FloatField)) return false;
199			if (IntegerField == null) {
200				if (other.IntegerField != null) return false;
201			} else if (!IntegerField.equals(other.IntegerField)) return false;
202			if (LongField == null) {
203				if (other.LongField != null) return false;
204			} else if (!LongField.equals(other.LongField)) return false;
205			if (ShortField == null) {
206				if (other.ShortField != null) return false;
207			} else if (!ShortField.equals(other.ShortField)) return false;
208			if (stringField == null) {
209				if (other.stringField != null) return false;
210			} else if (!stringField.equals(other.stringField)) return false;
211			if (booleanField != other.booleanField) return false;
212
213			Object list1 = arrayToList(byteArrayField);
214			Object list2 = arrayToList(other.byteArrayField);
215			if (list1 != list2) {
216				if (list1 == null || list2 == null) return false;
217				if (!list1.equals(list2)) return false;
218			}
219
220			if (object != other.object) {
221				if (object == null || other.object == null) return false;
222				if (object != this && !object.equals(other.object)) return false;
223			}
224
225			if (map != other.map) {
226				if (map == null || other.map == null) return false;
227				if (!map.keys().toArray().equals(other.map.keys().toArray())) return false;
228				if (!map.values().toArray().equals(other.map.values().toArray())) return false;
229			}
230
231			if (stringArray != other.stringArray) {
232				if (stringArray == null || other.stringArray == null) return false;
233				if (!stringArray.equals(other.stringArray)) return false;
234			}
235
236			if (byteField != other.byteField) return false;
237			if (charField != other.charField) return false;
238			if (Double.doubleToLongBits(doubleField) != Double.doubleToLongBits(other.doubleField)) return false;
239			if (Float.floatToIntBits(floatField) != Float.floatToIntBits(other.floatField)) return false;
240			if (intField != other.intField) return false;
241			if (longField != other.longField) return false;
242			if (shortField != other.shortField) return false;
243			return true;
244		}
245	}
246
247	public enum SomeEnum {
248		a, b, c;
249	}
250
251	static Object arrayToList (Object array) {
252		if (array == null || !array.getClass().isArray()) return array;
253		ArrayList list = new ArrayList(ArrayReflection.getLength(array));
254		for (int i = 0, n = ArrayReflection.getLength(array); i < n; i++)
255			list.add(arrayToList(ArrayReflection.get(array, i)));
256		return list;
257	}
258}
259