本文整理汇总了Java中org.bson.BsonType类的典型用法代码示例。如果您正苦于以下问题:Java BsonType类的具体用法?Java BsonType怎么用?Java BsonType使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BsonType类属于org.bson包,在下文中一共展示了BsonType类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decode
import org.bson.BsonType; //导入依赖的package包/类
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
T map = newInstance();
reader.readStartDocument();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String key = reader.readName();
Object value;
Codec fieldMappingCodec = fieldMappingCodecs.get(key);
if (fieldMappingCodec != null) {
value = fieldMappingCodec.decode(reader, decoderContext);
} else {
value = readValue(reader, decoderContext);
}
map.put(key, value);
}
reader.readEndDocument();
return map;
}
示例2: decode
import org.bson.BsonType; //导入依赖的package包/类
@Override
public Map<String, V> decode(BsonReader reader, DecoderContext decoderContext) {
Map<String, V> map = newInstance();
if (BsonType.DOCUMENT.equals(reader.getCurrentBsonType())) {
reader.readStartDocument();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String key = reader.readName();
V value = valueTypeCodec.decode(reader, decoderContext);
map.put(key, value);
}
reader.readEndDocument();
} else {
LOGGER.warn("Expected {} from reader but got {}. Skipping value.", BsonType.DOCUMENT, reader.getCurrentBsonType());
reader.skipValue();
}
return map;
}
示例3: decode
import org.bson.BsonType; //导入依赖的package包/类
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
if (BsonType.STRING.equals(reader.getCurrentBsonType())) {
String name = reader.readString();
if (name != null) {
try {
return Enum.valueOf(clazz, name);
} catch (IllegalArgumentException e) {
LOGGER.warn("Enum value {} could not be determined for enum type {}", name, clazz, e);
}
}
} else {
LOGGER.warn("Expected {} from reader but got {}. Skipping value.", BsonType.STRING, reader.getCurrentBsonType());
reader.skipValue();
}
return null;
}
示例4: handleArrayForBsonReader
import org.bson.BsonType; //导入依赖的package包/类
private Object handleArrayForBsonReader(BsonReader bsonReader, Field field, BsonMapperConfig bsonMapperConfig) {
Class<?> fieldType = field.getType();
ArrayList<Object> arrayList = new ArrayList<Object>();
bsonReader.readStartArray();
while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) {
BsonType currentBsonType = bsonReader.getCurrentBsonType();
if (currentBsonType == BsonType.NULL) {
continue;
}
if (currentBsonType == BsonType.ARRAY) {
arrayList.add(decode(bsonReader, field, bsonMapperConfig));
} else {
Object javaValue;
if (currentBsonType == BsonType.DOCUMENT) {
javaValue = BsonValueConverterRepertory.getBsonDocumentConverter().decode(bsonReader, fieldType.getComponentType(), bsonMapperConfig);
} else {
javaValue = BsonValueConverterRepertory.getByteIOConverterByBsonType(currentBsonType).decode(bsonReader);
}
arrayList.add(javaValue);
}
}
bsonReader.readEndArray();
return arrayList.toArray((Object[]) Array.newInstance(fieldType.getComponentType(), 0));
}
示例5: scalarToString
import org.bson.BsonType; //导入依赖的package包/类
/**
* Gson library reads numbers lazily when using generic {@link com.google.gson.internal.bind.TypeAdapters#JSON_ELEMENT} type adapter.
* Number is read as string and then wrapped inside {@link LazilyParsedNumber}. This inefficiency should only occur if reading numbers with generic JSON element API
* and not using generated type adapters.
*
* @see LazilyParsedNumber
* @see com.google.gson.internal.bind.TypeAdapters#JSON_ELEMENT
*/
private String scalarToString() throws IOException {
final BsonType type = delegate.getCurrentBsonType();
if (type == BsonType.STRING) {
return delegate.readString();
} else if (type == BsonType.SYMBOL) {
return delegate.readSymbol();
} else if (type == BsonType.INT32) {
return Integer.toString(nextInt());
} else if (type == BsonType.INT64) {
return Long.toString(nextLong());
} else if (type == BsonType.DOUBLE) {
return Double.toString(nextDouble());
}
throw new IllegalStateException(String.format("Unknown scalar type to be converted to string: %s", type));
}
示例6: decode
import org.bson.BsonType; //导入依赖的package包/类
public void decode(BsonReader reader, T instance, DecoderContext decoderContext) {
LOGGER.debug("Decode field : " + getMappedFieldName() + " Signature: " + fieldTypePair.getRealType());
if (field.getType().isPrimitive()) {
if (reader.getCurrentBsonType() == BsonType.NULL || reader.getCurrentBsonType() == BsonType.UNDEFINED) {
reader.skipValue();
} else {
primitiveType.decode(reader, instance, decoderContext, this);
}
} else if (codec != null) {
if (reader.getCurrentBsonType() == BsonType.NULL ) {
reader.readNull();
setFieldValue(instance, null);
}
else if (reader.getCurrentBsonType() == BsonType.UNDEFINED) {
reader.skipValue();
}
else {
F decoded = codec.decode(reader, decoderContext);
setFieldValue(instance, decoded);
}
}
}
示例7: decode
import org.bson.BsonType; //导入依赖的package包/类
@Override
public Map<K, V> decode(BsonReader reader, DecoderContext decoderContext) {
Map<K, V> map = newInstance();
if (BsonType.ARRAY.equals(reader.getCurrentBsonType())) {
reader.readStartArray();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
if (BsonType.DOCUMENT.equals(reader.getCurrentBsonType())) {
reader.readStartDocument();
reader.readName(); // don't need the key == "key"
K key = keyTypeCodec.decode(reader, decoderContext);
reader.readName(); // don't need the key == "value"
V value = valueTypeCodec.decode(reader, decoderContext);
map.put(key, value);
reader.readEndDocument();
} else {
LOGGER.warn("Expected {} from reader but got {}. Skipping value.", BsonType.DOCUMENT, reader.getCurrentBsonType());
reader.skipValue();
}
}
reader.readEndArray();
} else {
LOGGER.warn("Expected {} from reader but got {}. Skipping value.", BsonType.ARRAY, reader.getCurrentBsonType());
reader.skipValue();
}
return map;
}
示例8: decodeFields
import org.bson.BsonType; //导入依赖的package包/类
@Override
public T decodeFields(BsonReader reader, DecoderContext decoderContext, T instance) {
MetaData documentMeta = instance.getMeta();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String fieldName = reader.readName();
if ("data".equals(fieldName)) {
reader.readStartDocument();
super.decodeFields(reader, decoderContext, instance);
reader.readEndDocument();
} else {
MappedField field = documentMetaCodec.getMappedField(fieldName);
if (field != null) {
field.decode(reader, documentMeta, decoderContext);
} else {
reader.skipValue();
}
}
}
documentMetaCodec.postDecode(documentMeta);
return instance;
}
示例9: method_decode_should_evocate_functions_that_reade_user_attributes
import org.bson.BsonType; //导入依赖的package包/类
@Test
public void method_decode_should_evocate_functions_that_reade_user_attributes() throws Exception {
DecoderContext decoderContext = DecoderContext.builder().build();
PowerMockito.when(reader.readString("password")).thenReturn(PASSWORD);
PowerMockito.when(reader.readString("phone")).thenReturn(PHONE);
PowerMockito.when(reader.readBoolean("enable")).thenReturn(ENABLE);
PowerMockito.when(reader.readBoolean("extraction")).thenReturn(EXTRACTIONENABLED);
PowerMockito.when(reader.readString("surname")).thenReturn(SURNAME);
PowerMockito.when(reader.readString("name")).thenReturn(NAME);
PowerMockito.when(reader.readBoolean("adm")).thenReturn(ADMIN);
PowerMockito.when(reader.readString("uuid")).thenReturn(UUIDString);
PowerMockito.when(reader.readString("email")).thenReturn(EMAIL);
PowerMockito.when(reader.readString("acronym")).thenReturn(FIELDCENTERACRONYM);
PowerMockito.when(reader.readBsonType()).thenReturn(BsonType.END_OF_DOCUMENT);
doThrow(new BsonInvalidOperationException("teste")).when(reader).readNull("fieldCenter");
doThrow(new BsonInvalidOperationException("teste")).when(reader).readNull("extraction");
User userLocal = UserCodec.decode(reader, decoderContext);
assertEquals(userLocal.getEmail(),EMAIL);
assertEquals(userLocal.getPassword(),PASSWORD);
assertEquals(userLocal.getPhone(),PHONE);
assertEquals(userLocal.isEnable(),ENABLE);
assertEquals(userLocal.isExtractionEnabled(),EXTRACTIONENABLED);
assertEquals(userLocal.getName(),NAME);
assertEquals(userLocal.getFieldCenter().getAcronym(),FIELDCENTERACRONYM);
assertEquals(userLocal.getUuid().toString(),UUID.fromString(UUIDString).toString());
}
示例10: decode
import org.bson.BsonType; //导入依赖的package包/类
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
final BsonType type = reader.getCurrentBsonType();
switch(type) {
case INT32:
return decode(reader.readInt32() != 0);
case NULL:
return null;
case STRING:
return decode(Boolean.parseBoolean(reader.readString()));
case BOOLEAN:
return decode(reader.readBoolean());
//$CASES-OMITTED$
default:
final String errMsg = format("Invalid date type, found: %s", type);
throw new BsonInvalidOperationException(errMsg);
}
}
示例11: decode
import org.bson.BsonType; //导入依赖的package包/类
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
final BsonType type = reader.getCurrentBsonType();
switch(type) {
case DATE_TIME:
return decode(reader.readDateTime());
case DOUBLE:
return decode((long) reader.readDouble());
case INT32:
return decode(reader.readInt32());
case INT64:
return decode(reader.readInt64());
case NULL:
return null;
case STRING:
return decode(Long.valueOf(reader.readString()));
//$CASES-OMITTED$
default:
final String errMsg = format("Invalid date type, found: %s", type);
throw new BsonInvalidOperationException(errMsg);
}
}
示例12: decode
import org.bson.BsonType; //导入依赖的package包/类
@Override
public TestBean decode(BsonReader reader, DecoderContext decoderContext) {
reader.readStartDocument();
TestBean pojo = new TestBean();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
switch (reader.readName()) {
case "stringField":
pojo.setStringField(reader.getCurrentBsonType() != BsonType.NULL ? reader.readString() : null);
break;
case "longField":
pojo.setLongField(reader.readInt64());
break;
case "listStringField":
pojo.setListStringField(this.decodeGenericList(reader, decoderContext, String.class));
break;
case "setStringField":
pojo.setSetStringField(this.decodeGenericSet(reader, decoderContext, String.class));
break;
default:
Assert.fail("Unexpected name " + reader.getCurrentName());
}
}
reader.readEndDocument();
return pojo;
}
示例13: read
import org.bson.BsonType; //导入依赖的package包/类
public T read(BsonReader reader, String field) {
BsonType currentType = reader.getCurrentBsonType();
if (currentType == BsonType.NULL) {
reader.readNull();
return null;
} else if (currentType == BsonType.STRING) {
String enumValue = reader.readString();
T value = decodingMappings.get(enumValue);
if (value == null) throw Exceptions.error("can not decode value to enum, enumClass={}, value={}", enumClass.getCanonicalName(), enumValue);
return value;
} else {
logger.warn("unexpected field type, field={}, type={}", field, currentType);
reader.skipValue();
return null;
}
}
示例14: readValue
import org.bson.BsonType; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public <U> U readValue(final BsonReader reader,
final DecoderContext decoderContext) {
BsonType bsonType = reader.getCurrentBsonType();
if (bsonType == BsonType.NULL) {
reader.readNull();
return null;
} else if (bsonType == BsonType.ARRAY) {
return (U) readList(reader, decoderContext);
} else if (bsonType == BsonType.BINARY) {
byte bsonSubType = reader.peekBinarySubType();
if (bsonSubType == BsonBinarySubType.UUID_STANDARD.getValue()
|| bsonSubType == BsonBinarySubType.UUID_LEGACY.getValue()) {
return (U) registry.get().get(UUID.class).decode(reader,
decoderContext);
}
}
return (U) registry.get().get(bsonTypeClassMap.get(bsonType)).decode(reader,
decoderContext);
}
示例15: decode
import org.bson.BsonType; //导入依赖的package包/类
/**
* {@inhericDoc}
*/
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
T instance = instantiate();
reader.readStartDocument();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String fieldName = reader.readName();
BsoneeBaseSetter<T> bsoneeBaseSetter = settersByName.get(fieldName);
if (bsoneeBaseSetter != null) {
bsoneeBaseSetter.set(instance, reader, decoderContext);
} else {
// TODO Check options whether to throw an exception or not. By default we skip it :)
logger.warn("No setter for " + fieldName);
reader.skipValue();
}
}
reader.readEndDocument();
return instance;
}