| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// ------------------------------------------------------------------------ |
| 5 |
// All rights reserved. This program and the accompanying materials |
| 6 |
// are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
// and Apache License v2.0 which accompanies this distribution. |
| 8 |
// |
| 9 |
// The Eclipse Public License is available at |
| 10 |
// http://www.eclipse.org/legal/epl-v10.html |
| 11 |
// |
| 12 |
// The Apache License v2.0 is available at |
| 13 |
// http://www.opensource.org/licenses/apache2.0.php |
| 14 |
// |
| 15 |
// You may elect to redistribute this code under either of these licenses. |
| 16 |
// ======================================================================== |
| 17 |
// |
| 18 |
|
| 19 |
package org.eclipse.jetty.util.ajax; |
| 20 |
|
| 21 |
import java.io.IOException; |
| 22 |
import java.io.StringReader; |
| 23 |
import java.lang.reflect.Array; |
| 24 |
import java.math.BigDecimal; |
| 25 |
import java.util.Date; |
| 26 |
import java.util.HashMap; |
| 27 |
import java.util.Locale; |
| 28 |
import java.util.Map; |
| 29 |
import java.util.TimeZone; |
| 30 |
|
| 31 |
import org.eclipse.jetty.util.DateCache; |
| 32 |
import org.eclipse.jetty.util.ajax.JSON.Output; |
| 33 |
import org.junit.jupiter.api.BeforeEach; |
| 34 |
import org.junit.jupiter.api.Test; |
| 35 |
|
| 36 |
import static org.hamcrest.MatcherAssert.assertThat; |
| 37 |
import static org.hamcrest.Matchers.containsString; |
| 38 |
import static org.hamcrest.Matchers.is; |
| 39 |
import static org.hamcrest.Matchers.startsWith; |
| 40 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
| 41 |
import static org.junit.jupiter.api.Assertions.assertTrue; |
| 42 |
|
| 43 |
public class JSONTest |
| 44 |
{ |
| 45 |
// @checkstyle-disable-check : AvoidEscapedUnicodeCharactersCheck |
| 46 |
|
| 47 |
String test = "\n\n\n\t\t " + |
| 48 |
"// ignore this ,a [ \" \n" + |
| 49 |
"/* and this \n" + |
| 50 |
"/* and * // this \n" + |
| 51 |
"*/" + |
| 52 |
"{ " + |
| 53 |
"\"onehundred\" : 100 ," + |
| 54 |
"\"small\":-0.2," + |
| 55 |
"\"name\" : \"fred\" ," + |
| 56 |
"\"empty\" : {} ," + |
| 57 |
"\"map\" : {\"a\":-1.0e2} ," + |
| 58 |
"\"array\" : [\"a\",-1.0e2,[],null,true,false] ," + |
| 59 |
"\"w0\":{\"class\":\"org.eclipse.jetty.util.ajax.JSONTest$Woggle\",\"name\":\"woggle0\",\"nested\":{\"class\":\"org.eclipse.jetty.util.ajax.JSONTest$Woggle\",\"name\":\"woggle1\",\"nested\":null,\"number\":-101},\"number\":100}," + |
| 60 |
"\"NaN\": NaN," + |
| 61 |
"\"undefined\": undefined," + |
| 62 |
"}"; |
| 63 |
|
| 64 |
@BeforeEach |
| 65 |
public void resetJSON() |
| 66 |
{ |
| 67 |
// Reset JSON configuration to default with each testcase |
| 68 |
JSON.reset(); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void testToString() |
| 73 |
{ |
| 74 |
JSON.registerConvertor(Gadget.class, new JSONObjectConvertor(false)); |
| 75 |
HashMap map = new HashMap(); |
| 76 |
HashMap obj6 = new HashMap(); |
| 77 |
HashMap obj7 = new HashMap(); |
| 78 |
|
| 79 |
Woggle w0 = new Woggle(); |
| 80 |
Woggle w1 = new Woggle(); |
| 81 |
|
| 82 |
w0.name = "woggle0"; |
| 83 |
w0.nested = w1; |
| 84 |
w0.number = 100; |
| 85 |
w1.name = "woggle1"; |
| 86 |
w1.nested = null; |
| 87 |
w1.number = -101; |
| 88 |
|
| 89 |
map.put("n1", null); |
| 90 |
map.put("n2", new Integer(2)); |
| 91 |
map.put("n3", new Double(-0.00000000003)); |
| 92 |
map.put("n4", "4\n\r\t\"4"); |
| 93 |
map.put("n5", new Object[]{"a", new Character('b'), new Integer(3), new String[]{}, null, Boolean.TRUE, Boolean.FALSE}); |
| 94 |
map.put("n6", obj6); |
| 95 |
map.put("n7", obj7); |
| 96 |
map.put("n8", new int[]{1, 2, 3, 4}); |
| 97 |
map.put("n9", new JSON.Literal("[{}, [], {}]")); |
| 98 |
map.put("w0", w0); |
| 99 |
|
| 100 |
obj7.put("x", "value"); |
| 101 |
|
| 102 |
String s = JSON.toString(map); |
| 103 |
assertTrue(s.indexOf("\"n1\":null") >= 0); |
| 104 |
assertTrue(s.indexOf("\"n2\":2") >= 0); |
| 105 |
assertTrue(s.indexOf("\"n3\":-3.0E-11") >= 0); |
| 106 |
assertTrue(s.indexOf("\"n4\":\"4\\n") >= 0); |
| 107 |
assertTrue(s.indexOf("\"n5\":[\"a\",\"b\",") >= 0); |
| 108 |
assertTrue(s.indexOf("\"n6\":{}") >= 0); |
| 109 |
assertTrue(s.indexOf("\"n7\":{\"x\":\"value\"}") >= 0); |
| 110 |
assertTrue(s.indexOf("\"n8\":[1,2,3,4]") >= 0); |
| 111 |
assertTrue(s.indexOf("\"n9\":[{}, [], {}]") >= 0); |
| 112 |
assertTrue(s.indexOf("\"w0\":{\"class\":\"org.eclipse.jetty.util.ajax.JSONTest$Woggle\",\"name\":\"woggle0\",\"nested\":{\"class\":\"org.eclipse.jetty.util.ajax.JSONTest$Woggle\",\"name\":\"woggle1\",\"nested\":null,\"number\":-101},\"number\":100}") >= 0); |
| 113 |
|
| 114 |
Gadget gadget = new Gadget(); |
| 115 |
gadget.setShields(42); |
| 116 |
gadget.setWoggles(new Woggle[]{w0, w1}); |
| 117 |
|
| 118 |
s = JSON.toString(new Gadget[]{gadget}); |
| 119 |
assertThat(s, startsWith("[")); |
| 120 |
assertThat(s, containsString("\"modulated\":false")); |
| 121 |
assertThat(s, containsString("\"shields\":42")); |
| 122 |
assertThat(s, containsString("\"name\":\"woggle0\"")); |
| 123 |
assertThat(s, containsString("\"name\":\"woggle1\"")); |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void testParse() |
| 128 |
{ |
| 129 |
Map map = (Map)JSON.parse(test); |
| 130 |
assertEquals(new Long(100), map.get("onehundred")); |
| 131 |
assertEquals("fred", map.get("name")); |
| 132 |
assertEquals(-0.2, map.get("small")); |
| 133 |
assertTrue(map.get("array").getClass().isArray()); |
| 134 |
assertTrue(map.get("w0") instanceof Woggle); |
| 135 |
assertTrue(((Woggle)map.get("w0")).nested instanceof Woggle); |
| 136 |
assertEquals(-101, ((Woggle)((Woggle)map.get("w0")).nested).number); |
| 137 |
assertTrue(map.containsKey("NaN")); |
| 138 |
assertEquals(null, map.get("NaN")); |
| 139 |
assertTrue(map.containsKey("undefined")); |
| 140 |
assertEquals(null, map.get("undefined")); |
| 141 |
|
| 142 |
test = "{\"data\":{\"source\":\"15831407eqdaawf7\",\"widgetId\":\"Magnet_8\"},\"channel\":\"/magnets/moveStart\",\"connectionId\":null,\"clientId\":\"15831407eqdaawf7\"}"; |
| 143 |
map = (Map)JSON.parse(test); |
| 144 |
} |
| 145 |
|
| 146 |
@Test |
| 147 |
public void testToStringLineFeed() |
| 148 |
{ |
| 149 |
Map<String, String> map = new HashMap<>(); |
| 150 |
map.put("str", "line\nfeed"); |
| 151 |
String jsonStr = JSON.toString(map); |
| 152 |
assertThat(jsonStr, is("{\"str\":\"line\\nfeed\"}")); |
| 153 |
} |
| 154 |
|
| 155 |
@Test |
| 156 |
public void testToStringTab() |
| 157 |
{ |
| 158 |
Map<String, String> map = new HashMap<>(); |
| 159 |
map.put("str", "tab\tchar"); |
| 160 |
String jsonStr = JSON.toString(map); |
| 161 |
assertThat(jsonStr, is("{\"str\":\"tab\\tchar\"}")); |
| 162 |
} |
| 163 |
|
| 164 |
@Test |
| 165 |
public void testToStringBel() |
| 166 |
{ |
| 167 |
Map<String, String> map = new HashMap<>(); |
| 168 |
map.put("str", "ascii\u0007bel"); |
| 169 |
String jsonStr = JSON.toString(map); |
| 170 |
assertThat(jsonStr, is("{\"str\":\"ascii\\u0007bel\"}")); |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void testToStringUtf8() |
| 175 |
{ |
| 176 |
Map<String, String> map = new HashMap<>(); |
| 177 |
map.put("str", "japanese: 桟橋"); |
| 178 |
String jsonStr = JSON.toString(map); |
| 179 |
assertThat(jsonStr, is("{\"str\":\"japanese: 桟橋\"}")); |
| 180 |
} |
| 181 |
|
| 182 |
@Test |
| 183 |
public void testToJsonUtf8Encoded() |
| 184 |
{ |
| 185 |
JSON jsonUnicode = new JSON() |
| 186 |
{ |
| 187 |
@Override |
| 188 |
protected void escapeUnicode(Appendable buffer, char c) throws IOException |
| 189 |
{ |
| 190 |
buffer.append(String.format("\\u%04x", (int)c)); |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
Map<String, String> map = new HashMap<>(); |
| 195 |
map.put("str", "japanese: 桟橋"); |
| 196 |
String jsonStr = jsonUnicode.toJSON(map); |
| 197 |
assertThat(jsonStr, is("{\"str\":\"japanese: \\u685f\\u6a4b\"}")); |
| 198 |
} |
| 199 |
|
| 200 |
@Test |
| 201 |
public void testParseUtf8JsonEncoded() |
| 202 |
{ |
| 203 |
String jsonStr = "{\"str\": \"japanese: \\u685f\\u6a4b\"}"; |
| 204 |
Map map = (Map)JSON.parse(jsonStr); |
| 205 |
assertThat(map.get("str"), is("japanese: 桟橋")); |
| 206 |
} |
| 207 |
|
| 208 |
@Test |
| 209 |
public void testParseUtf8JavaEncoded() |
| 210 |
{ |
| 211 |
String jsonStr = "{\"str\": \"japanese: \u685f\u6a4b\"}"; |
| 212 |
Map map = (Map)JSON.parse(jsonStr); |
| 213 |
assertThat(map.get("str"), is("japanese: 桟橋")); |
| 214 |
} |
| 215 |
|
| 216 |
@Test |
| 217 |
public void testParseUtf8Raw() |
| 218 |
{ |
| 219 |
String jsonStr = "{\"str\": \"japanese: 桟橋\"}"; |
| 220 |
Map map = (Map)JSON.parse(jsonStr); |
| 221 |
assertThat(map.get("str"), is("japanese: 桟橋")); |
| 222 |
} |
| 223 |
|
| 224 |
@Test |
| 225 |
public void testParseReader() throws Exception |
| 226 |
{ |
| 227 |
Map map = (Map)JSON.parse(new StringReader(test)); |
| 228 |
|
| 229 |
assertEquals(new Long(100), map.get("onehundred")); |
| 230 |
assertEquals("fred", map.get("name")); |
| 231 |
assertTrue(map.get("array").getClass().isArray()); |
| 232 |
assertTrue(map.get("w0") instanceof Woggle); |
| 233 |
assertTrue(((Woggle)map.get("w0")).nested instanceof Woggle); |
| 234 |
|
| 235 |
test = "{\"data\":{\"source\":\"15831407eqdaawf7\",\"widgetId\":\"Magnet_8\"},\"channel\":\"/magnets/moveStart\",\"connectionId\":null,\"clientId\":\"15831407eqdaawf7\"}"; |
| 236 |
map = (Map)JSON.parse(test); |
| 237 |
} |
| 238 |
|
| 239 |
@Test |
| 240 |
public void testStripComment() |
| 241 |
{ |
| 242 |
String test = "\n\n\n\t\t " + |
| 243 |
"// ignore this ,a [ \" \n" + |
| 244 |
"/* " + |
| 245 |
"{ " + |
| 246 |
"\"onehundred\" : 100 ," + |
| 247 |
"\"name\" : \"fred\" ," + |
| 248 |
"\"empty\" : {} ," + |
| 249 |
"\"map\" : {\"a\":-1.0e2} ," + |
| 250 |
"\"array\" : [\"a\",-1.0e2,[],null,true,false] ," + |
| 251 |
"} */"; |
| 252 |
|
| 253 |
Object o = JSON.parse(test, false); |
| 254 |
assertTrue(o == null); |
| 255 |
o = JSON.parse(test, true); |
| 256 |
assertTrue(o instanceof Map); |
| 257 |
assertEquals("fred", ((Map)o).get("name")); |
| 258 |
} |
| 259 |
|
| 260 |
@Test |
| 261 |
public void testQuote() |
| 262 |
{ |
| 263 |
String test = "\"abc123|\\\"|\\\\|\\/|\\b|\\f|\\n|\\r|\\t|\\uaaaa|\""; |
| 264 |
|
| 265 |
String result = (String)JSON.parse(test, false); |
| 266 |
assertEquals("abc123|\"|\\|/|\b|\f|\n|\r|\t|\uaaaa|", result); |
| 267 |
} |
| 268 |
|
| 269 |
@Test |
| 270 |
public void testBigDecimal() |
| 271 |
{ |
| 272 |
Object obj = JSON.parse("1.0E7"); |
| 273 |
assertTrue(obj instanceof Double); |
| 274 |
BigDecimal bd = BigDecimal.valueOf(10000000d); |
| 275 |
String string = JSON.toString(new Object[]{bd}); |
| 276 |
obj = Array.get(JSON.parse(string), 0); |
| 277 |
assertTrue(obj instanceof Double); |
| 278 |
} |
| 279 |
|
| 280 |
@Test |
| 281 |
public void testZeroByte() |
| 282 |
{ |
| 283 |
String withzero = "\u0000"; |
| 284 |
JSON.toString(withzero); |
| 285 |
} |
| 286 |
|
| 287 |
public static class Gadget |
| 288 |
{ |
| 289 |
private boolean modulated; |
| 290 |
private long shields; |
| 291 |
private Woggle[] woggles; |
| 292 |
|
| 293 |
/** |
| 294 |
* @return the modulated |
| 295 |
*/ |
| 296 |
public boolean isModulated() |
| 297 |
{ |
| 298 |
return modulated; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* @param modulated the modulated to set |
| 303 |
*/ |
| 304 |
public void setModulated(boolean modulated) |
| 305 |
{ |
| 306 |
this.modulated = modulated; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* @return the shields |
| 311 |
*/ |
| 312 |
public long getShields() |
| 313 |
{ |
| 314 |
return shields; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* @param shields the shields to set |
| 319 |
*/ |
| 320 |
public void setShields(long shields) |
| 321 |
{ |
| 322 |
this.shields = shields; |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* @return the woggles |
| 327 |
*/ |
| 328 |
public Woggle[] getWoggles() |
| 329 |
{ |
| 330 |
return woggles; |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* @param woggles the woggles to set |
| 335 |
*/ |
| 336 |
public void setWoggles(Woggle[] woggles) |
| 337 |
{ |
| 338 |
this.woggles = woggles; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
@Test |
| 343 |
public void testConvertor() |
| 344 |
{ |
| 345 |
// test case#1 - force timezone to GMT |
| 346 |
JSON json = new JSON(); |
| 347 |
json.addConvertor(Date.class, new JSONDateConvertor("MM/dd/yyyy HH:mm:ss zzz", TimeZone.getTimeZone("GMT"), false)); |
| 348 |
json.addConvertor(Object.class, new JSONObjectConvertor()); |
| 349 |
|
| 350 |
Woggle w0 = new Woggle(); |
| 351 |
Gizmo g0 = new Gizmo(); |
| 352 |
|
| 353 |
w0.name = "woggle0"; |
| 354 |
w0.nested = g0; |
| 355 |
w0.number = 100; |
| 356 |
g0.name = "woggle1"; |
| 357 |
g0.nested = null; |
| 358 |
g0.number = -101; |
| 359 |
g0.tested = true; |
| 360 |
|
| 361 |
HashMap map = new HashMap(); |
| 362 |
Date dummyDate = new Date(1); |
| 363 |
map.put("date", dummyDate); |
| 364 |
map.put("w0", w0); |
| 365 |
|
| 366 |
StringBuffer buf = new StringBuffer(); |
| 367 |
json.append(buf, map); |
| 368 |
String js = buf.toString(); |
| 369 |
|
| 370 |
assertTrue(js.indexOf("\"date\":\"01/01/1970 00:00:00 GMT\"") >= 0); |
| 371 |
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); |
| 372 |
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); |
| 373 |
assertTrue(js.indexOf("\"tested\":true") >= 0); |
| 374 |
|
| 375 |
// test case#3 |
| 376 |
TimeZone tzone = TimeZone.getTimeZone("JST"); |
| 377 |
String tzone3Letter = tzone.getDisplayName(false, TimeZone.SHORT); |
| 378 |
String format = "EEE MMMMM dd HH:mm:ss zzz yyyy"; |
| 379 |
|
| 380 |
Locale l = new Locale("ja", "JP"); |
| 381 |
if (l != null) |
| 382 |
{ |
| 383 |
json.addConvertor(Date.class, new JSONDateConvertor(format, tzone, false, l)); |
| 384 |
buf = new StringBuffer(); |
| 385 |
json.append(buf, map); |
| 386 |
js = buf.toString(); |
| 387 |
//assertTrue(js.indexOf("\"date\":\"\u6728 1\u6708 01 09:00:00 JST 1970\"")>=0); |
| 388 |
assertTrue(js.indexOf(" 01 09:00:00 JST 1970\"") >= 0); |
| 389 |
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); |
| 390 |
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); |
| 391 |
assertTrue(js.indexOf("\"tested\":true") >= 0); |
| 392 |
} |
| 393 |
|
| 394 |
// test case#4 |
| 395 |
json.addConvertor(Date.class, new JSONDateConvertor(true)); |
| 396 |
w0.nested = null; |
| 397 |
buf = new StringBuffer(); |
| 398 |
json.append(buf, map); |
| 399 |
js = buf.toString(); |
| 400 |
assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") < 0); |
| 401 |
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); |
| 402 |
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); |
| 403 |
|
| 404 |
map = (HashMap)json.parse(new JSON.StringSource(js)); |
| 405 |
|
| 406 |
assertTrue(map.get("date") instanceof Date); |
| 407 |
assertTrue(map.get("w0") instanceof Woggle); |
| 408 |
} |
| 409 |
|
| 410 |
enum Color |
| 411 |
{ |
| 412 |
Red, Green, Blue |
| 413 |
} |
| 414 |
|
| 415 |
; |
| 416 |
|
| 417 |
@Test |
| 418 |
public void testEnumConvertor() |
| 419 |
{ |
| 420 |
JSON json = new JSON(); |
| 421 |
Locale l = new Locale("en", "US"); |
| 422 |
json.addConvertor(Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), false, l)); |
| 423 |
json.addConvertor(Enum.class, new JSONEnumConvertor(false)); |
| 424 |
json.addConvertor(Object.class, new JSONObjectConvertor()); |
| 425 |
|
| 426 |
Woggle w0 = new Woggle(); |
| 427 |
Gizmo g0 = new Gizmo(); |
| 428 |
|
| 429 |
w0.name = "woggle0"; |
| 430 |
w0.nested = g0; |
| 431 |
w0.number = 100; |
| 432 |
w0.other = Color.Blue; |
| 433 |
g0.name = "woggle1"; |
| 434 |
g0.nested = null; |
| 435 |
g0.number = -101; |
| 436 |
g0.tested = true; |
| 437 |
g0.other = Color.Green; |
| 438 |
|
| 439 |
HashMap map = new HashMap(); |
| 440 |
map.put("date", new Date(1)); |
| 441 |
map.put("w0", w0); |
| 442 |
map.put("g0", g0); |
| 443 |
|
| 444 |
StringBuffer buf = new StringBuffer(); |
| 445 |
json.append((Appendable)buf, map); |
| 446 |
String js = buf.toString(); |
| 447 |
|
| 448 |
assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") >= 0); |
| 449 |
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); |
| 450 |
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); |
| 451 |
assertTrue(js.indexOf("\"tested\":true") >= 0); |
| 452 |
assertTrue(js.indexOf("\"Green\"") >= 0); |
| 453 |
assertTrue(js.indexOf("\"Blue\"") < 0); |
| 454 |
|
| 455 |
json.addConvertor(Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), true, l)); |
| 456 |
json.addConvertor(Enum.class, new JSONEnumConvertor(false)); |
| 457 |
w0.nested = null; |
| 458 |
buf = new StringBuffer(); |
| 459 |
json.append((Appendable)buf, map); |
| 460 |
js = buf.toString(); |
| 461 |
assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") < 0); |
| 462 |
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); |
| 463 |
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); |
| 464 |
|
| 465 |
Map map2 = (HashMap)json.parse(new JSON.StringSource(js)); |
| 466 |
|
| 467 |
assertTrue(map2.get("date") instanceof Date); |
| 468 |
assertTrue(map2.get("w0") instanceof Woggle); |
| 469 |
assertEquals(null, ((Woggle)map2.get("w0")).getOther()); |
| 470 |
assertEquals(Color.Green.toString(), ((Map)map2.get("g0")).get("other")); |
| 471 |
|
| 472 |
json.addConvertor(Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), true, l)); |
| 473 |
json.addConvertor(Enum.class, new JSONEnumConvertor(true)); |
| 474 |
buf = new StringBuffer(); |
| 475 |
json.append((Appendable)buf, map); |
| 476 |
js = buf.toString(); |
| 477 |
map2 = (HashMap)json.parse(new JSON.StringSource(js)); |
| 478 |
|
| 479 |
assertTrue(map2.get("date") instanceof Date); |
| 480 |
assertTrue(map2.get("w0") instanceof Woggle); |
| 481 |
assertEquals(null, ((Woggle)map2.get("w0")).getOther()); |
| 482 |
Object o = ((Map)map2.get("g0")).get("other"); |
| 483 |
assertEquals(Color.Green, o); |
| 484 |
} |
| 485 |
|
| 486 |
public static class Gizmo |
| 487 |
{ |
| 488 |
String name; |
| 489 |
Gizmo nested; |
| 490 |
long number; |
| 491 |
boolean tested; |
| 492 |
Object other; |
| 493 |
|
| 494 |
public String getName() |
| 495 |
{ |
| 496 |
return name; |
| 497 |
} |
| 498 |
|
| 499 |
public Gizmo getNested() |
| 500 |
{ |
| 501 |
return nested; |
| 502 |
} |
| 503 |
|
| 504 |
public long getNumber() |
| 505 |
{ |
| 506 |
return number; |
| 507 |
} |
| 508 |
|
| 509 |
public boolean isTested() |
| 510 |
{ |
| 511 |
return tested; |
| 512 |
} |
| 513 |
|
| 514 |
public Object getOther() |
| 515 |
{ |
| 516 |
return other; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
public static class Woggle extends Gizmo implements JSON.Convertible |
| 521 |
{ |
| 522 |
|
| 523 |
public Woggle() |
| 524 |
{ |
| 525 |
} |
| 526 |
|
| 527 |
@Override |
| 528 |
public void fromJSON(Map object) |
| 529 |
{ |
| 530 |
name = (String)object.get("name"); |
| 531 |
nested = (Gizmo)object.get("nested"); |
| 532 |
number = ((Number)object.get("number")).intValue(); |
| 533 |
} |
| 534 |
|
| 535 |
@Override |
| 536 |
public void toJSON(Output out) |
| 537 |
{ |
| 538 |
out.addClass(Woggle.class); |
| 539 |
out.add("name", name); |
| 540 |
out.add("nested", nested); |
| 541 |
out.add("number", number); |
| 542 |
} |
| 543 |
|
| 544 |
@Override |
| 545 |
public String toString() |
| 546 |
{ |
| 547 |
return name + "<<" + nested + ">>" + number; |
| 548 |
} |
| 549 |
} |
| 550 |
} |