@ -287,36 +287,22 @@ public class BsonUtils {
@@ -287,36 +287,22 @@ public class BsonUtils {
* /
public static Object toJavaType ( BsonValue value ) {
switch ( value . getBsonType ( ) ) {
case INT32 :
return value . asInt32 ( ) . getValue ( ) ;
case INT64 :
return value . asInt64 ( ) . getValue ( ) ;
case STRING :
return value . asString ( ) . getValue ( ) ;
case DECIMAL128 :
return value . asDecimal128 ( ) . doubleValue ( ) ;
case DOUBLE :
return value . asDouble ( ) . getValue ( ) ;
case BOOLEAN :
return value . asBoolean ( ) . getValue ( ) ;
case OBJECT_ID :
return value . asObjectId ( ) . getValue ( ) ;
case DB_POINTER :
return new DBRef ( value . asDBPointer ( ) . getNamespace ( ) , value . asDBPointer ( ) . getId ( ) ) ;
case BINARY :
return value . asBinary ( ) . getData ( ) ;
case DATE_TIME :
return new Date ( value . asDateTime ( ) . getValue ( ) ) ;
case SYMBOL :
return value . asSymbol ( ) . getSymbol ( ) ;
case ARRAY :
return value . asArray ( ) . toArray ( ) ;
case DOCUMENT :
return Document . parse ( value . asDocument ( ) . toJson ( ) ) ;
default :
return value ;
}
return switch ( value . getBsonType ( ) ) {
case INT32 - > value . asInt32 ( ) . getValue ( ) ;
case INT64 - > value . asInt64 ( ) . getValue ( ) ;
case STRING - > value . asString ( ) . getValue ( ) ;
case DECIMAL128 - > value . asDecimal128 ( ) . doubleValue ( ) ;
case DOUBLE - > value . asDouble ( ) . getValue ( ) ;
case BOOLEAN - > value . asBoolean ( ) . getValue ( ) ;
case OBJECT_ID - > value . asObjectId ( ) . getValue ( ) ;
case DB_POINTER - > new DBRef ( value . asDBPointer ( ) . getNamespace ( ) , value . asDBPointer ( ) . getId ( ) ) ;
case BINARY - > value . asBinary ( ) . getData ( ) ;
case DATE_TIME - > new Date ( value . asDateTime ( ) . getValue ( ) ) ;
case SYMBOL - > value . asSymbol ( ) . getSymbol ( ) ;
case ARRAY - > value . asArray ( ) . toArray ( ) ;
case DOCUMENT - > Document . parse ( value . asDocument ( ) . toJson ( ) ) ;
default - > value ;
} ;
}
/ * *
@ -341,6 +327,7 @@ public class BsonUtils {
@@ -341,6 +327,7 @@ public class BsonUtils {
* @throws IllegalArgumentException if { @literal source } does not correspond to a { @link BsonValue } type .
* @since 4 . 2
* /
@SuppressWarnings ( "unchecked" )
public static BsonValue simpleToBsonValue ( Object source , CodecRegistry codecRegistry ) {
if ( source instanceof BsonValue bsonValue ) {
@ -407,7 +394,7 @@ public class BsonUtils {
@@ -407,7 +394,7 @@ public class BsonUtils {
/ * *
* Merge the given { @link Document documents } into on in the given order . Keys contained within multiple documents are
* overwritten by their follow ups .
* overwritten by their follow - ups .
*
* @param documents must not be { @literal null } . Can be empty .
* @return the document containing all key value pairs .
@ -730,8 +717,9 @@ public class BsonUtils {
@@ -730,8 +717,9 @@ public class BsonUtils {
private static String toString ( Map < ? , ? > source ) {
// Avoid String.format for performance
return iterableToDelimitedString ( source . entrySet ( ) , "{ " , " }" ,
entry - > String . format ( "\"%s\" : %s" , entry . getKey ( ) , toJson ( entry . getValue ( ) ) ) ) ;
entry - > "\"" + entry . getKey ( ) + "\" : " + toJson ( entry . getValue ( ) ) ) ;
}
private static String toString ( Collection < ? > source ) {
@ -748,12 +736,13 @@ public class BsonUtils {
@@ -748,12 +736,13 @@ public class BsonUtils {
return joiner . toString ( ) ;
}
private static class BsonCapturingWriter extends AbstractBsonWriter {
static class BsonCapturingWriter extends AbstractBsonWriter {
List < BsonValue > values = new ArrayList < > ( 0 ) ;
private final List < BsonValue > values = new ArrayList < > ( 0 ) ;
public BsonCapturingWriter ( Class < ? > type ) {
super ( new BsonWriterSettings ( ) ) ;
if ( ClassUtils . isAssignable ( Map . class , type ) ) {
setContext ( new Context ( null , BsonContextType . DOCUMENT ) ) ;
} else if ( ClassUtils . isAssignable ( List . class , type ) | | type . isArray ( ) ) {
@ -763,6 +752,7 @@ public class BsonUtils {
@@ -763,6 +752,7 @@ public class BsonUtils {
}
}
@Nullable
BsonValue getCapturedValue ( ) {
if ( values . isEmpty ( ) ) {
@ -852,18 +842,14 @@ public class BsonUtils {
@@ -852,18 +842,14 @@ public class BsonUtils {
@Override
protected void doWriteJavaScriptWithScope ( String value ) {
values . add ( new BsonJavaScriptWithScope ( value , null ) ) ;
throw new UnsupportedOperationException ( "Cannot capture JavaScriptWith" ) ;
}
@Override
protected void doWriteMaxKey ( ) {
}
protected void doWriteMaxKey ( ) { }
@Override
protected void doWriteMinKey ( ) {
}
protected void doWriteMinKey ( ) { }
@Override
protected void doWriteNull ( ) {