Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

JAVA-743: JSONCallback now applies decoding hooks for all types

  • Loading branch information...
commit 37845665cefa016881ae070efcca8b4c3b5f3b3e 1 parent 10d8a0b
Jeff Yemin jyemin authored
9 src/main/com/mongodb/util/JSONCallback.java
View
@@ -63,12 +63,6 @@ public Object objectDone() {
// override the object if it's a special type
if (b.containsField("$oid")) {
o = new ObjectId((String) b.get("$oid"));
- if (!isStackEmpty()) {
- gotObjectId(name, (ObjectId) o);
- } else {
- setRoot(o);
- }
- return o;
} else if (b.containsField("$date")) {
if (b.get("$date") instanceof Number) {
o = new Date(((Number) b.get("$date")).longValue());
@@ -117,8 +111,9 @@ public Object objectDone() {
}
if (!isStackEmpty()) {
- cur().put(name, o);
+ _put(name, o);
} else {
+ o = !BSON.hasDecodeHooks() ? o : BSON.applyDecodingHooks( o );
setRoot(o);
}
return o;
25 src/test/com/mongodb/util/JSONCallbackTest.java
View
@@ -1,6 +1,9 @@
package com.mongodb.util;
+import com.mongodb.DBObject;
import com.mongodb.DBRef;
+import org.bson.BSON;
+import org.bson.Transformer;
import org.bson.types.BSONTimestamp;
import org.bson.types.Binary;
import org.bson.types.ObjectId;
@@ -42,6 +45,28 @@ public void dateParsing() {
}
@org.testng.annotations.Test(groups = {"basic"})
+ public void encodingHooks() {
+ BSON.addDecodingHook(Date.class, new Transformer() {
+ @Override
+ public Object transform(final Object o) {
+ return ((Date) o).getTime();
+ }
+ });
+
+ try {
+ Date now = new Date();
+
+ Object parsedDate = JSON.parse("{ \"$date\" : " + now.getTime() + "}");
+ assertEquals(Long.class, parsedDate.getClass());
+
+ DBObject doc = (DBObject) JSON.parse("{ date : { \"$date\" : " + now.getTime() + "} }");
+ assertEquals(Long.class, doc.get("date").getClass());
+ } finally {
+ BSON.removeDecodingHooks(Date.class);
+ }
+ }
+
+ @org.testng.annotations.Test(groups = {"basic"})
public void binaryParsing() {
Binary parsedBinary = (Binary) JSON.parse(("{ \"$binary\" : \"YWJjZA==\", \"$type\" : 0 }"));
org.testng.Assert.assertEquals(parsedBinary.getType(), 0);
Please sign in to comment.
Something went wrong with that request. Please try again.