Package org.bson
Class BSON
- java.lang.Object
-
- org.bson.BSON
-
- Direct Known Subclasses:
Bytes
public class BSON extends java.lang.Object
Contains byte representations of all the BSON types (see the BSON Specification). Also supports the registration of encoding and decoding hooks to transform BSON types during encoding or decoding.- See Also:
Transformer
-
-
Field Summary
Fields Modifier and Type Field Description static byte
ARRAY
static byte
B_BINARY
static byte
B_FUNC
static byte
B_GENERAL
static byte
B_UUID
static byte
BINARY
static byte
BOOLEAN
static byte
CODE
static byte
CODE_W_SCOPE
static byte
DATE
static byte
EOO
static byte
MAXKEY
static byte
MINKEY
static byte
NULL
static byte
NUMBER
static byte
NUMBER_INT
static byte
NUMBER_LONG
static byte
OBJECT
static byte
OID
static byte
REF
static byte
REGEX
static byte
STRING
static byte
SYMBOL
static byte
TIMESTAMP
static byte
UNDEFINED
-
Constructor Summary
Constructors Constructor Description BSON()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addDecodingHook(java.lang.Class<?> clazz, Transformer transformer)
Registers aTransformer
to use when decoding a specific class from BSON.static void
addEncodingHook(java.lang.Class<?> clazz, Transformer transformer)
Registers aTransformer
to use to encode a specific class into BSON.static java.lang.Object
applyDecodingHooks(java.lang.Object objectToDecode)
Transforms theobjectToDecode
using all transformers registered for the class of this object.static java.lang.Object
applyEncodingHooks(java.lang.Object objectToEncode)
Transforms theobjectToEncode
using all transformers registered for the class of this object.static void
clearAllHooks()
Remove all decoding and encoding hooks for all classes.static void
clearDecodingHooks()
Clears all decoding hooks.static void
clearEncodingHooks()
Clears all encoding hooks.static BSONObject
decode(byte[] bytes)
Decodes a BSON byte array into a DBObject instance.static byte[]
encode(BSONObject doc)
Encodes a DBObject as a BSON byte array.static java.util.List<Transformer>
getDecodingHooks(java.lang.Class<?> clazz)
Returns the decoding hook(s) associated with the specific classstatic java.util.List<Transformer>
getEncodingHooks(java.lang.Class<?> clazz)
Returns the encoding hook(s) associated with the specified class.static boolean
hasDecodeHooks()
Gets whether any decoding transformers have been registered for any classes.static boolean
hasEncodeHooks()
Gets whether any encoding transformers have been registered for any classes.static int
regexFlag(char c)
Converts a regular expression modifier from the database into Java regular expression flags.static java.lang.String
regexFlags(int flags)
Converts Java regular expression flags into regular expression modifiers from the database.static int
regexFlags(java.lang.String s)
Converts a sequence of regular expression modifiers from the database into Java regular expression flags.static void
removeDecodingHook(java.lang.Class<?> clazz, Transformer transformer)
Remove a specific encoding hook for a specific class.static void
removeDecodingHooks(java.lang.Class<?> clazz)
Remove all decoding hooks for a specific class.static void
removeEncodingHook(java.lang.Class<?> clazz, Transformer transformer)
Remove a specific encoding hook for a specific class.static void
removeEncodingHooks(java.lang.Class<?> clazz)
Remove all encoding hooks for a specific class.static int
toInt(java.lang.Object number)
Provides an integer representation of Boolean or Number.
-
-
-
Field Detail
-
EOO
public static final byte EOO
- See Also:
- Constant Field Values
-
NUMBER
public static final byte NUMBER
- See Also:
- Constant Field Values
-
STRING
public static final byte STRING
- See Also:
- Constant Field Values
-
OBJECT
public static final byte OBJECT
- See Also:
- Constant Field Values
-
ARRAY
public static final byte ARRAY
- See Also:
- Constant Field Values
-
BINARY
public static final byte BINARY
- See Also:
- Constant Field Values
-
UNDEFINED
public static final byte UNDEFINED
- See Also:
- Constant Field Values
-
OID
public static final byte OID
- See Also:
- Constant Field Values
-
BOOLEAN
public static final byte BOOLEAN
- See Also:
- Constant Field Values
-
DATE
public static final byte DATE
- See Also:
- Constant Field Values
-
NULL
public static final byte NULL
- See Also:
- Constant Field Values
-
REGEX
public static final byte REGEX
- See Also:
- Constant Field Values
-
REF
public static final byte REF
- See Also:
- Constant Field Values
-
CODE
public static final byte CODE
- See Also:
- Constant Field Values
-
SYMBOL
public static final byte SYMBOL
- See Also:
- Constant Field Values
-
CODE_W_SCOPE
public static final byte CODE_W_SCOPE
- See Also:
- Constant Field Values
-
NUMBER_INT
public static final byte NUMBER_INT
- See Also:
- Constant Field Values
-
TIMESTAMP
public static final byte TIMESTAMP
- See Also:
- Constant Field Values
-
NUMBER_LONG
public static final byte NUMBER_LONG
- See Also:
- Constant Field Values
-
MINKEY
public static final byte MINKEY
- See Also:
- Constant Field Values
-
MAXKEY
public static final byte MAXKEY
- See Also:
- Constant Field Values
-
B_GENERAL
public static final byte B_GENERAL
- See Also:
- Constant Field Values
-
B_FUNC
public static final byte B_FUNC
- See Also:
- Constant Field Values
-
B_BINARY
public static final byte B_BINARY
- See Also:
- Constant Field Values
-
B_UUID
public static final byte B_UUID
- See Also:
- Constant Field Values
-
-
Method Detail
-
hasEncodeHooks
public static boolean hasEncodeHooks()
Gets whether any encoding transformers have been registered for any classes.- Returns:
- true if any encoding hooks have been registered.
-
hasDecodeHooks
public static boolean hasDecodeHooks()
Gets whether any decoding transformers have been registered for any classes.- Returns:
- true if any decoding hooks have been registered.
-
addEncodingHook
public static void addEncodingHook(java.lang.Class<?> clazz, Transformer transformer)
Registers aTransformer
to use to encode a specific class into BSON.- Parameters:
clazz
- the class to be transformed during encodingtransformer
- the transformer to use during encoding
-
addDecodingHook
public static void addDecodingHook(java.lang.Class<?> clazz, Transformer transformer)
Registers aTransformer
to use when decoding a specific class from BSON. This class will be one of the basic types supported by BSON.- Parameters:
clazz
- the class to be transformed during decodingtransformer
- the transformer to use during decoding
-
applyEncodingHooks
public static java.lang.Object applyEncodingHooks(java.lang.Object objectToEncode)
Transforms theobjectToEncode
using all transformers registered for the class of this object.- Parameters:
objectToEncode
- the object being written to BSON.- Returns:
- the transformed object
-
applyDecodingHooks
public static java.lang.Object applyDecodingHooks(java.lang.Object objectToDecode)
Transforms theobjectToDecode
using all transformers registered for the class of this object.- Parameters:
objectToDecode
- the BSON object to decode- Returns:
- the transformed object
-
getEncodingHooks
public static java.util.List<Transformer> getEncodingHooks(java.lang.Class<?> clazz)
Returns the encoding hook(s) associated with the specified class.- Parameters:
clazz
- the class to fetch the encoding hooks for- Returns:
- a List of encoding transformers that apply to the given class
-
clearEncodingHooks
public static void clearEncodingHooks()
Clears all encoding hooks.
-
removeEncodingHooks
public static void removeEncodingHooks(java.lang.Class<?> clazz)
Remove all encoding hooks for a specific class.- Parameters:
clazz
- the class to remove all the decoding hooks for
-
removeEncodingHook
public static void removeEncodingHook(java.lang.Class<?> clazz, Transformer transformer)
Remove a specific encoding hook for a specific class. Thetransformer
passed as the parameter must beequals
to the transformer to remove.- Parameters:
clazz
- the class to remove the encoding hook fortransformer
- the specific encoding hook to remove.
-
getDecodingHooks
public static java.util.List<Transformer> getDecodingHooks(java.lang.Class<?> clazz)
Returns the decoding hook(s) associated with the specific class- Parameters:
clazz
- the class to fetch the decoding hooks for- Returns:
- a List of all the decoding Transformers that apply to the given class
-
clearDecodingHooks
public static void clearDecodingHooks()
Clears all decoding hooks.
-
removeDecodingHooks
public static void removeDecodingHooks(java.lang.Class<?> clazz)
Remove all decoding hooks for a specific class.- Parameters:
clazz
- the class to remove all the decoding hooks for
-
removeDecodingHook
public static void removeDecodingHook(java.lang.Class<?> clazz, Transformer transformer)
Remove a specific encoding hook for a specific class. Thetransformer
passed as the parameter must beequals
to the transformer to remove.- Parameters:
clazz
- the class to remove the decoding hook fortransformer
- the specific decoding hook to remove.
-
clearAllHooks
public static void clearAllHooks()
Remove all decoding and encoding hooks for all classes.
-
encode
public static byte[] encode(BSONObject doc)
Encodes a DBObject as a BSON byte array.- Parameters:
doc
- the document to encode- Returns:
- the document encoded as BSON
-
decode
public static BSONObject decode(byte[] bytes)
Decodes a BSON byte array into a DBObject instance.- Parameters:
bytes
- a document encoded as BSON- Returns:
- the document as a DBObject
-
regexFlags
public static int regexFlags(java.lang.String s)
Converts a sequence of regular expression modifiers from the database into Java regular expression flags.- Parameters:
s
- regular expression modifiers- Returns:
- the Java flags
- Throws:
java.lang.IllegalArgumentException
- If sequence contains invalid flags.
-
regexFlag
public static int regexFlag(char c)
Converts a regular expression modifier from the database into Java regular expression flags.- Parameters:
c
- regular expression modifier- Returns:
- the Java flags
- Throws:
java.lang.IllegalArgumentException
- If sequence contains invalid flags.
-
regexFlags
public static java.lang.String regexFlags(int flags)
Converts Java regular expression flags into regular expression modifiers from the database.- Parameters:
flags
- the Java flags- Returns:
- the Java flags
- Throws:
java.lang.IllegalArgumentException
- if some flags couldn't be recognized.
-
toInt
public static int toInt(java.lang.Object number)
Provides an integer representation of Boolean or Number. If argument isBoolean
, then1
fortrue
will be returned or @{code 0} otherwise. If argument isNumber
, thenNumber.intValue()
will be called.- Parameters:
number
- the number to convert to an int- Returns:
- integer value
- Throws:
java.lang.IllegalArgumentException
- if the argument isnull
or notBoolean
orNumber
-
-