public class BSON extends Object
限定符和类型 | 字段和说明 |
---|---|
protected static Charset |
_utf8 |
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_DECIMAL |
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 |
构造器和说明 |
---|
BSON() |
限定符和类型 | 方法和说明 |
---|---|
static void |
addDecodingHook(Class c,
Transformer t)
Add a decoding hook for transforming a instance of BSON supported classes into a
instance of class c.
|
static void |
addEncodingHook(Class c,
Transformer t)
Add a encoding hook for transforming a instance of class c into a
instance of the BSON supported classes.
|
static Object |
applyDecodingHooks(Object o)
Use the registered decoding hooks to transform the instance of BSON supported classes into another
instance of user expect.
|
static Object |
applyEncodingHooks(Object o)
Use the registered encoding hooks to transform the specific instance into the instance of BSON
supported classes.
|
static void |
clearAllHooks()
Remove all the hooks.
|
static void |
clearDecodingHooks()
Clears all decoding hooks.
|
static void |
clearEncodingHooks()
Clears all encoding hooks.
|
static BSONObject |
decode(byte[] b)
Decode the bytes into a BSONObject instance.
|
static BSONObject |
decode(byte[] b,
int offset)
Decode the bytes into a BSONObject instance.
|
static byte[] |
encode(BSONObject o)
Encoding a BSONObject instance into bytes
|
static List<Transformer> |
getDecodingHooks(Class c)
Returns the decoding hook(s) associated with the specific class
|
static List<Transformer> |
getEncodingHooks(Class c)
Returns the encoding hook(s) associated with the specified class
|
static boolean |
getJSCompatibility()
Get whether the display mode of BSON is the same with that in sdb shell or not.
|
static boolean |
hasDecodeHooks()
check has decode hooks or not.
|
static boolean |
IsBasicType(Object obj) |
static int |
regexFlag(char c) |
static String |
regexFlags(int flags)
Converts Java regular expression flags into a string of flags for the database
|
static int |
regexFlags(String flags)
Converts a string of regular expression flags from the database in Java regular
expression flags.
|
static void |
removeDecodingHook(Class c,
Transformer t)
Remove a specific encoding hook for a specific class.
|
static void |
removeDecodingHooks(Class c)
Remove all decoding hooks for a specific class.
|
static void |
removeEncodingHook(Class c,
Transformer t)
Remove a specific encoding hook for a specific class.
|
static void |
removeEncodingHooks(Class c)
Remove all encoding hooks for a specific class.
|
static void |
setJSCompatibility(boolean compatible)
When "compatible" is true, the content of BasicBSONObject method "toString" is show
absolutely the same with which is show in sdb shell.
|
static int |
toInt(Object o) |
protected static Charset _utf8
public static final byte ARRAY
public static final byte B_BINARY
public static final byte B_FUNC
public static final byte B_GENERAL
public static final byte B_UUID
public static final byte BINARY
public static final byte BOOLEAN
public static final byte CODE
public static final byte CODE_W_SCOPE
public static final byte DATE
public static final byte EOO
public static final byte MAXKEY
public static final byte MINKEY
public static final byte NULL
public static final byte NUMBER
public static final byte NUMBER_DECIMAL
public static final byte NUMBER_INT
public static final byte NUMBER_LONG
public static final byte OBJECT
public static final byte OID
public static final byte REF
public static final byte REGEX
public static final byte STRING
public static final byte SYMBOL
public static final byte TIMESTAMP
public static final byte UNDEFINED
public static void addDecodingHook(Class c, Transformer t)
Usage: public void testEncodeDecodeHook() { BSON.addEncodingHook(GregorianCalendar.class, new Transformer() { public Object transform(Object o) { return ((GregorianCalendar)o).getTime(); } }); BSON.addDecodingHook(Date.class, new Transformer() { public Object transform(Object o) { Calendar calendar = new GregorianCalendar(); calendar.setTime((Date)o); return calendar; } }); Calendar calendar = new GregorianCalendar(); calendar.set(2015, 0,1); BSONObject object = new BasicBSONObject(); object.put("calendar", calendar); cl.insert(object); DBCursor cursor = cl.query(); while(cursor.hasNext()) { BSONObject record = cursor.getNext(); System.out.println("record is: " + record); Object resultObject = record.get("calendar"); calendar.equals(resultObject); Assert.assertTrue(resultObject instanceof GregorianCalendar); } }
c
- the class of the instance which is going to be transformed into.t
- the Transformer, user should defines the transform rule.public static void addEncodingHook(Class c, Transformer t)
Usage: public void testEncodeDecodeHook() { BSON.addEncodingHook(GregorianCalendar.class, new Transformer() { public Object transform(Object o) { return ((GregorianCalendar)o).getTime(); } }); BSON.addDecodingHook(Date.class, new Transformer() { public Object transform(Object o) { Calendar calendar = new GregorianCalendar(); calendar.setTime((Date)o); return calendar; } }); Calendar calendar = new GregorianCalendar(); calendar.set(2015, 0,1); BSONObject object = new BasicBSONObject(); object.put("calendar", calendar); cl.insert(object); DBCursor cursor = cl.query(); while(cursor.hasNext()) { BSONObject record = cursor.getNext(); System.out.println("record is: " + record); Object resultObject = record.get("calendar"); calendar.equals(resultObject); Assert.assertTrue(resultObject instanceof GregorianCalendar); } }
c
- the class of the instance which is going to be transformed.t
- the Transformer, user should defines the transform rule.public static Object applyDecodingHooks(Object o)
o
- public static Object applyEncodingHooks(Object o)
o
- public static void clearAllHooks()
public static void clearDecodingHooks()
public static void clearEncodingHooks()
public static BSONObject decode(byte[] b)
b
- bytes to be decoded.public static BSONObject decode(byte[] b, int offset)
b
- bytes to be decoded.offset
- the start position.public static byte[] encode(BSONObject o)
o
- the instance of BSONObject.public static List<Transformer> getDecodingHooks(Class c)
public static List<Transformer> getEncodingHooks(Class c)
c
- the specified class for getting it's registered encoding hook.public static boolean getJSCompatibility()
setJSCompatibility(boolean)
public static boolean hasDecodeHooks()
public static boolean IsBasicType(Object obj)
public static int regexFlag(char c)
public static String regexFlags(int flags)
flags
- Java flagspublic static int regexFlags(String flags)
flags
- flags from databasepublic static void removeDecodingHook(Class c, Transformer t)
public static void removeDecodingHooks(Class c)
public static void removeEncodingHook(Class c, Transformer t)
public static void removeEncodingHooks(Class c)
public static void setJSCompatibility(boolean compatible)
compatible
- true or false, default to be false;
// we have a bson as below:
BSONObject obj = new BasicBSONObject("a", Long.MAX_VALUE);
// sdb shell shows this bson like this:
{"a" : { "$numberLong" : "9223372036854775807"}}
// sdb shell use javascript grammer, so, it can't display number
// which is great that 2^53 - 1. So it use "$numberLong" to represent
// the type, and keep the number between the quotes.
// However, in java, when we use "obj.toString()",
// most of the time, we don't hope to get a result with
// the format "$numberLong", we hope to see the result as
// below:
{"a" : 9223372036854775807}
// When parameter "compatible" is false, we get this kind of result
// all the time. Otherwise, we get a result which is show as the sdb shell shows.
public static int toInt(Object o)
Copyright © 2018. All rights reserved.