|
27 | 27 | import org.bson.types.*;
|
28 | 28 |
|
29 | 29 | import com.mongodb.*;
|
| 30 | +import org.bson.BSON; |
| 31 | +import org.bson.BasicBSONObject; |
30 | 32 |
|
31 | 33 | import org.testng.annotations.Test;
|
32 | 34 |
|
@@ -300,6 +302,35 @@ public void testDate() {
|
300 | 302 | assertEquals(d.toString(), d2.toString());
|
301 | 303 | }
|
302 | 304 |
|
| 305 | + @org.testng.annotations.Test |
| 306 | + public void testJSONEncoding() throws ParseException { |
| 307 | + String json = "{ 'str' : 'asdfasd' , 'long' : 5 , 'float' : 0.4 , 'bool' : false , 'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , 'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , '$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , 'codews' : { '$code' : 'ggggg' , '$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' : null }"; |
| 308 | + BasicDBObject a = (BasicDBObject) JSON.parse(json); |
| 309 | + assert (a.get("str").equals("asdfasd")); |
| 310 | + assert (a.get("long").equals(5L)); |
| 311 | + assert (a.get("float").equals(0.4d)); |
| 312 | + assert (a.get("bool").equals(false)); |
| 313 | + SimpleDateFormat format = |
| 314 | + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); |
| 315 | + format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT"))); |
| 316 | + assert (a.get("date").equals(format.parse("2011-05-18T18:56:00Z"))); |
| 317 | + Pattern pat = (Pattern) a.get("pat"); |
| 318 | + Pattern pat2 = Pattern.compile(".*", BSON.regexFlags("")); |
| 319 | + assert (pat.pattern().equals(pat2.pattern())); |
| 320 | + assert (pat.flags() == (pat2.flags())); |
| 321 | + ObjectId oid = (ObjectId) a.get("oid"); |
| 322 | + assert (oid.equals(new ObjectId("4d83ab3ea39562db9c1ae2ae"))); |
| 323 | + DBRef ref = (DBRef) a.get("ref"); |
| 324 | + assert (ref.equals(new DBRef(null, "test.test", new ObjectId("4d83ab59a39562db9c1ae2af")))); |
| 325 | + assert (a.get("code").equals(new Code("asdfdsa"))); |
| 326 | + assert (a.get("codews").equals(new CodeWScope("ggggg", new BasicBSONObject()))); |
| 327 | + assert (a.get("ts").equals(new BSONTimestamp(1300474885, 10))); |
| 328 | + String json2 = JSON.serialize(a); |
| 329 | + BasicDBObject b = (BasicDBObject) JSON.parse(json2); |
| 330 | + a.equals(b); |
| 331 | + assert (a.equals(b)); |
| 332 | + } |
| 333 | + |
303 | 334 | public static void main( String args[] ){
|
304 | 335 | (new JSONTest()).runConsole();
|
305 | 336 | }
|
|
0 commit comments