org.bson.BSON类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(182)

本文整理了Java中org.bson.BSON类的一些代码示例,展示了BSON类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BSON类的具体详情如下:
包路径:org.bson.BSON
类名称:BSON

BSON介绍

[英]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.
[中]包含所有BSON类型的字节表示形式(请参见BSON Specification)。还支持编码和解码挂钩的注册,以在编码或解码期间转换BSON类型。

代码示例

代码示例来源:origin: spring-projects/spring-data-mongodb

private Pattern toPattern(String regex, @Nullable String options) {
  Assert.notNull(regex, "Regex string must not be null!");
  return Pattern.compile(regex, options == null ? 0 : BSON.regexFlags(options));
}

代码示例来源:origin: org.mongodb/mongo-java-driver

if (b.containsField("$oid")) {
  o = new ObjectId((String) b.get("$oid"));
} else if (b.containsField("$date")) {
  if (b.get("$date") instanceof Number) {
    o = new Date(((Number) b.get("$date")).longValue());
} else if (b.containsField("$regex")) {
  o = Pattern.compile((String) b.get("$regex"),
            org.bson.BSON.regexFlags((String) b.get("$options")));
} else if (b.containsField("$ts")) { //Legacy timestamp format
  Integer ts = ((Number) b.get("$ts")).intValue();
  _put(name, o);
} else {
  o = !org.bson.BSON.hasDecodeHooks() ? o : org.bson.BSON.applyDecodingHooks(o);
  setRoot(o);

代码示例来源:origin: org.mongodb/mongo-java-driver

@SuppressWarnings("unchecked")
private void writeValue(final BsonWriter bsonWriter, final EncoderContext encoderContext, final Object initialValue) {
  Object value = org.bson.BSON.applyEncodingHooks(initialValue);
  if (value == null) {
    bsonWriter.writeNull();
  } else if (value instanceof DBRef) {
    encodeDBRef(bsonWriter, (DBRef) value);
  } else if (value instanceof Map) {
    encodeMap(bsonWriter, (Map<String, Object>) value);
  } else if (value instanceof Iterable) {
    encodeIterable(bsonWriter, (Iterable) value);
  } else if (value instanceof BSONObject) {
    encodeBsonObject(bsonWriter, ((BSONObject) value));
  } else if (value instanceof CodeWScope) {
    encodeCodeWScope(bsonWriter, (CodeWScope) value);
  } else if (value instanceof byte[]) {
    encodeByteArray(bsonWriter, (byte[]) value);
  } else if (value.getClass().isArray()) {
    encodeArray(bsonWriter, value);
  } else if (value instanceof Symbol) {
    bsonWriter.writeSymbol(((Symbol) value).getSymbol());
  } else {
    Codec codec = codecRegistry.get(value.getClass());
    codec.encode(bsonWriter, value, encoderContext);
  }
}

代码示例来源:origin: org.mongodb/mongo-java-driver

@Override
public Object objectDone() {
  BSONObject o = stack.removeLast();
  if (nameStack.size() > 0) {
    nameStack.removeLast();
  } else if (stack.size() > 0) {
    throw new IllegalStateException("Illegal object end in current context.");
  }
  return !BSON.hasDecodeHooks() ? o : (BSONObject) BSON.applyDecodingHooks(o);
}

代码示例来源:origin: org.mongodb/mongo-java-driver

/**
 * Puts a new value into the document.
 *
 * @param name  the name of the field
 * @param value the value
 */
protected void _put(final String name, final Object value) {
  cur().put(name, !BSON.hasDecodeHooks() ? value : BSON.applyDecodingHooks(value));
}

代码示例来源:origin: fr.opensagres.mongodb/mongo-jee

if (b.containsField("$oid")) {
    o = new ObjectId((String) b.get("$oid"));
    if (!isStackEmpty()) {
      gotObjectId(name, (ObjectId) o);
      setRoot(o);
  } else if (b.containsField("$date")) {
    if(b.get("$date") instanceof Number){
  } else if ( b.containsField( "$regex" ) ) {
o = Pattern.compile( (String)b.get( "$regex" ), 
       BSON.regexFlags( (String)b.get( "$options" )) );
if (!isStackEmpty()) {
  cur().put( name, o );

代码示例来源:origin: org.mongodb/mongo-java-driver

/**
 * Encodes a Pattern field to a {@link org.bson.BsonType#REGULAR_EXPRESSION}.
 *
 * @param name  the field name
 * @param value the value
 * @mongodb.driver.manual reference/operator/query/regex/ $regex
 * @see org.bson.BsonType#BINARY
 */
protected void putPattern(final String name, final Pattern value) {
  putName(name);
  bsonWriter.writeRegularExpression(new BsonRegularExpression(value.pattern(), org.bson.BSON.regexFlags(value.flags())));
}

代码示例来源:origin: com.sequoiadb/sequoiadb-driver

public void serialize(Object obj, StringBuilder buf) {
    BSONObject externalForm = new BasicBSONObject();
    externalForm.put("$regex", obj.toString());
    if (((Pattern) obj).flags() != 0) externalForm.put("$options", BSON.regexFlags(((Pattern) obj).flags()));
    serializer.serialize(externalForm, buf);
  }
}

代码示例来源:origin: kakaochatfriend/KakaoChatFriendAPI

Object obj = e.getMessage();
if (obj instanceof byte[]) {
  BSONObject bsonIn = BSON.decode((byte[]) obj);
  String type = (String) bsonIn.get("type");
  if (type == null) {
  } else if (type.equals("request")) {
    try {
      long user_key = (Long) bsonIn.get("user_key");
      long room_key = (Long) bsonIn.get("room_key");
      int msgId = (Integer) bsonIn.get("msg_id");
        bsonOut.put("message", DATA_NOT_FOUND_MSG);
      e.getChannel().write(BSON.encode(bsonOut));
    bOut.put("message", WELCOME_MSG);
    e.getChannel().write(BSON.encode(bOut));
  } else if (type.equals("ping")) {
    BSONObject bOut = new BasicBSONObject();
    bOut.put("type", "pong");
    bOut.put("time", (Long)bsonIn.get("time"));
    e.getChannel().write(BSON.encode(bOut));

代码示例来源:origin: thiloplanz/jmockmongo

/**
 * BSON objects are considered equal when their binary encoding matches
 */
static boolean equals(BSONObject a, BSONObject b) {
  return a.keySet().equals(b.keySet())
      && Arrays.equals(BSON.encode(a), BSON.encode(b));
}

代码示例来源:origin: com.sequoiadb/sequoiadb-driver

public static byte[] encodeBSONObj(BSONObject obj) {
  return BSON.encode(obj);
}

代码示例来源:origin: com.sequoiadb/sequoiadb-driver

public static BSONObject decodeBSONBytes(byte[] bytes) {
  return BSON.decode(bytes);
}

代码示例来源:origin: com.guestful.module/guestful.module.jsr310-extensions

private static <T> void hook(Class<T> type, Function<T, Object> fn) {
  BSON.addEncodingHook(type, o -> type.isInstance(o) ? fn.apply(type.cast(o)) : o);
}

代码示例来源:origin: org.mongodb/mongo-java-driver

return org.bson.BSON.applyDecodingHooks(initialRetVal);

代码示例来源:origin: org.mongodb/mongo-java-driver

/**
 * Transforms the {@code objectToDecode} using all transformers registered for the class of this object.
 *
 * @param objectToDecode the BSON object to decode
 * @return the transformed object
 */
public static Object applyDecodingHooks(final Object objectToDecode) {
  Object transformedObject = objectToDecode;
  if (!hasDecodeHooks() || objectToDecode == null || decodingHooks.size() == 0) {
    return transformedObject;
  }
  List<Transformer> transformersForObject = decodingHooks.get(objectToDecode.getClass());
  if (transformersForObject != null) {
    for (final Transformer transformer : transformersForObject) {
      transformedObject = transformer.transform(objectToDecode);
    }
  }
  return transformedObject;
}

代码示例来源:origin: com.sequoiadb/sequoiadb-driver

if (b.containsField("$oid")) {
  o = new ObjectId((String) b.get("$oid"));
  if (!isStackEmpty()) {
    gotObjectId(name, (ObjectId) o);
    setRoot(o);
} else if (b.containsField("$numberLong")) {
  o = Long.valueOf((String) b.get("$numberLong"));
  if (!isStackEmpty()) {
} else if (b.containsField("$regex")) {
  o = Pattern.compile((String) b.get("$regex"),
      BSON.regexFlags((String) b.get("$options")));
  if (!isStackEmpty()) {
    cur().put(name, o);

代码示例来源:origin: com.sequoiadb/sequoiadb-driver

private void putPattern(String name, Pattern p) {
  _put(REGEX, name);
  _put(p.pattern());
  _put(regexFlags(p.flags()));
}

代码示例来源:origin: com.sequoiadb/sequoiadb-driver

protected void _put(final String name, final Object o) {
  cur().put(name, !BSON.hasDecodeHooks() ? o : BSON.applyDecodingHooks(o));
}

代码示例来源:origin: de.bwaldvogel/mongo-java-server-test-common

private void writeBson(OutputStream outputStream, Document data) throws Exception {
  outputStream.write(BSON.encode(new BasicBSONObject(data)));
}

代码示例来源:origin: spring-projects/spring-data-mongodb

private Object exceptionSwallowingStackReducingObjectDone/*CauseWeJustNeedTheStructureNotTheActualValue*/() {
    Object value;
    try {
      return super.objectDone();
    } catch (PatternSyntaxException e) {
      value = EMPTY_MARKER;
    }
    if (!isStackEmpty()) {
      _put(curName(), value);
    } else {
      value = !BSON.hasDecodeHooks() ? value : BSON.applyDecodingHooks(value);
      setRoot(value);
    }
    return value;
  }
}

相关文章