Skip to content

Commit cdab1ae

Browse files
committed
JAVA-377: driver cannot BSON encode / decode the MinKey / MaxKey types
1 parent 4e456ba commit cdab1ae

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

src/main/com/mongodb/util/JSON.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,16 @@ public static void serialize( Object o , StringBuilder buf ){
218218
serialize( temp, buf );
219219
return;
220220
}
221-
221+
222+
if ( o instanceof MinKey ){
223+
serialize( new BasicDBObject("$minKey", 1), buf );
224+
return;
225+
}
226+
if ( o instanceof MaxKey ){
227+
serialize( new BasicDBObject("$maxKey", 1), buf );
228+
return;
229+
}
230+
222231
throw new RuntimeException( "json can't serialize type : " + o.getClass() );
223232
}
224233

src/main/com/mongodb/util/JSONCallback.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ public Object objectDone(){
8888
} else {
8989
setRoot(o);
9090
}
91+
} else if ( b.containsField( "$minKey" ) ) {
92+
o = new MinKey();
93+
if (!isStackEmpty()) {
94+
cur().put( name, o );
95+
} else {
96+
setRoot(o);
97+
}
98+
} else if ( b.containsField( "$maxKey" ) ) {
99+
o = new MaxKey();
100+
if (!isStackEmpty()) {
101+
cur().put( name, o );
102+
} else {
103+
setRoot(o);
104+
}
91105
} else if ( b.containsField( "$uuid" ) ) {
92106
o = UUID.fromString((String)b.get("$uuid"));
93107
if (!isStackEmpty()) {

src/main/org/bson/BSONEncoder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ else if (val instanceof DBRefBase) {
207207
temp.put("$id", ((DBRefBase)val).getId());
208208
putObject( name, temp );
209209
}
210+
else if ( val instanceof MinKey )
211+
putMinKey( name );
212+
else if ( val instanceof MaxKey )
213+
putMaxKey( name );
210214
else if ( putSpecial( name , val ) ){
211215
// no-op
212216
}
@@ -378,6 +382,14 @@ private void putPattern( String name, Pattern p ) {
378382
_put( regexFlags( p.flags() ) );
379383
}
380384

385+
private void putMinKey( String name ) {
386+
_put( MINKEY , name );
387+
}
388+
389+
private void putMaxKey( String name ) {
390+
_put( MAXKEY , name );
391+
}
392+
381393

382394
// ----------------------------------------------
383395

src/main/org/bson/BasicBSONCallback.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ public void gotUndefined( String name ){
8484
}
8585

8686
public void gotMinKey( String name ){
87-
cur().put( name , "MinKey" );
87+
cur().put( name , new MinKey() );
8888
}
8989
public void gotMaxKey( String name ){
90-
cur().put( name , "MaxKey" );
90+
cur().put( name , new MaxKey() );
9191
}
9292

9393
public void gotBoolean( String name , boolean v ){

src/test/com/mongodb/JavaClientTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,20 @@ public void testBinary()
200200
}
201201

202202
}
203+
204+
@Test
205+
public void testMinMaxKey()
206+
throws MongoException {
207+
DBCollection c = _db.getCollection( "testMinMaxKey" );
208+
c.drop();
209+
c.save( BasicDBObjectBuilder.start().add( "min" , new MinKey() ).add( "max" , new MaxKey() ).get() );
210+
211+
DBObject out = c.findOne();
212+
MinKey min = (MinKey)(out.get( "min" ) );
213+
MaxKey max = (MaxKey)(out.get( "max" ) );
214+
assertTrue( JSON.serialize(min).contains("$minKey") );
215+
assertTrue( JSON.serialize(max).contains("$maxKey") );
216+
}
203217

204218
@Test
205219
public void testBinaryOld()

0 commit comments

Comments
 (0)