本文整理汇总了Java中org.bson.BsonReader.readBsonType方法的典型用法代码示例。如果您正苦于以下问题:Java BsonReader.readBsonType方法的具体用法?Java BsonReader.readBsonType怎么用?Java BsonReader.readBsonType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bson.BsonReader
的用法示例。
在下文中一共展示了BsonReader.readBsonType方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decode
import org.bson.BsonReader; //导入方法依赖的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.BsonReader; //导入方法依赖的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: handleArrayForBsonReader
import org.bson.BsonReader; //导入方法依赖的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));
}
示例4: decode
import org.bson.BsonReader; //导入方法依赖的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;
}
示例5: decodeFields
import org.bson.BsonReader; //导入方法依赖的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;
}
示例6: readList
import org.bson.BsonReader; //导入方法依赖的package包/类
private List<Object> readList(final BsonReader reader, final DecoderContext decoderContext) {
reader.readStartArray();
List<Object> list = new ArrayList<>();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
list.add(readValue(reader, decoderContext));
}
reader.readEndArray();
return list;
}
示例7: decode
import org.bson.BsonReader; //导入方法依赖的package包/类
@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
List<Boolean> arrayList = new ArrayList();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
arrayList.add(reader.readBoolean());
}
boolean[] booleans = new boolean[arrayList.size()];
int i = 0;
for (boolean aPrimitiveBoolean : arrayList) {
booleans[i++] = aPrimitiveBoolean;
}
return booleans;
}
示例8: decodeFields
import org.bson.BsonReader; //导入方法依赖的package包/类
public T decodeFields(BsonReader reader, DecoderContext decoderContext, T instance) {
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String fieldName = reader.readName();
if ("enum".equals(fieldName)) {
return codec.decode(reader, decoderContext);
}
else
{
reader.skipValue();
}
}
// throw new EnumValueNotFoundException??? instead of returning null?
return null;
}
示例9: decode
import org.bson.BsonReader; //导入方法依赖的package包/类
/**
* bsonReader中读取Bson到POJO/decode from bsonReader->POJO
*
* @param bsonReader
* @param targetClazz
* @param bsonMapperConfig
* @param <T>
* @return
*/
<T> T decode(BsonReader bsonReader, Class<T> targetClazz, BsonMapperConfig bsonMapperConfig) {
MAPPER_LAYER_COUNTER.addCount(bsonMapperConfig);
try {
bsonReader.readStartDocument();
Map<String, Field> bsonNameFieldInfoMap = getBsonNameFieldInfoMap(targetClazz);
T target = Utils.newInstanceByClazz(targetClazz);
while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String bsonName = bsonReader.readName();
Field field = bsonNameFieldInfoMap.get(bsonName);
if (field == null || bsonReader.getCurrentBsonType() == BsonType.NULL) {
bsonReader.skipValue();
continue;
}
Object javaValue;
try {
javaValue = getJavaValueByBinaryReader(bsonReader, field, bsonMapperConfig);
} catch (BsonMapperConverterException e) {
throw new BsonMapperConverterException("error when try to get java value from Bson.BsonName:" + bsonName, e);
}
setJavaValueToField(targetClazz, target, field, javaValue);
}
bsonReader.readEndDocument();
return target;
} finally {
MAPPER_LAYER_COUNTER.reduceCount();
}
}
示例10: handleCollectionForBsonReader
import org.bson.BsonReader; //导入方法依赖的package包/类
Object handleCollectionForBsonReader(BsonReader bsonReader, Field field, BsonMapperConfig bsonMapperConfig) {
Class<?> fieldType = field.getType();
Class<?> implClass = Utils.giveImplClassIfSupport(fieldType);
if (Utils.isUnableAddCollectionClazz(implClass)) {
return null;
}
BsonArrayField bsonArrayField = Utils.getBsonArrayFieldAnnotation(field);
Class<?> targetComponentClazz = bsonArrayField.componentType();
Object collectionObject = Utils.newInstanceByClazz(implClass);
Method method;
try {
method = implClass.getMethod("add", Object.class);
} catch (NoSuchMethodException e) {
throw new BsonMapperConverterException("NoSuchMethodException", e);
}
bsonReader.readStartArray();
while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) {
BsonType currentBsonType = bsonReader.getCurrentBsonType();
if (currentBsonType == BsonType.NULL) {
continue;
}
if (currentBsonType == BsonType.ARRAY) {
Utils.methodInvoke(method, collectionObject, decode(bsonReader, field, bsonMapperConfig));
} else {
Object javaValue;
if (currentBsonType == BsonType.DOCUMENT) {
javaValue = BsonValueConverterRepertory.getBsonDocumentConverter().decode(bsonReader, targetComponentClazz, bsonMapperConfig);
} else {
javaValue = BsonValueConverterRepertory.getByteIOConverterByBsonType(currentBsonType).decode(bsonReader);
}
Utils.methodInvoke(method, collectionObject, javaValue);
}
}
bsonReader.readEndArray();
return collectionObject;
}
示例11: decodeDimension
import org.bson.BsonReader; //导入方法依赖的package包/类
private Object decodeDimension(BsonReader reader, DecoderContext decoderContext, Class<?> componentType) {
Object array = null;
boolean decodeLastDimension;
Class<?> childComponentType = componentType.getComponentType();
decodeLastDimension = !childComponentType.isArray();
if (childComponentType == byte.class) {
array = arrayElementCodec.decode(reader, decoderContext);
} else if (BsonType.ARRAY.equals(reader.getCurrentBsonType())) {
reader.readStartArray();
if (decodeLastDimension) {
if (isPrimitive) {
array = arrayElementCodec.decode(reader, decoderContext);
} else {
List list = new ArrayList();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
list.add(arrayElementCodec.decode(reader, decoderContext));
}
array = Array.newInstance(childComponentType, list.size());
for (int i = 0; i < list.size(); i++) {
Array.set(array, i, list.get(i));
}
}
} else {
List arrayList = new ArrayList();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
Object decoded = decodeDimension(reader, decoderContext, childComponentType);
arrayList.add(decoded);
}
array = Array.newInstance(childComponentType, arrayList.size());
for (int i = 0; i < arrayList.size(); i++) {
Array.set(array, i, arrayList.get(i));
}
}
reader.readEndArray();
} else {
LOGGER.warn("Expected {} from reader but got {}. Skipping value.", BsonType.ARRAY, reader.getCurrentBsonType());
reader.skipValue();
}
return array;
}
示例12: decode
import org.bson.BsonReader; //导入方法依赖的package包/类
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
if (reader.getCurrentBsonType() == BsonType.NULL) {
reader.readNull();
return null;
}
String discriminator = null;
reader.mark();
reader.readStartDocument();
PolymorphicCodec<T> codec = null;
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String fieldName = reader.readName();
if (allDiscriminatorKeys.contains(fieldName)) {
discriminator = reader.readString();
codec = getCodecForDiscriminator(discriminator);
if (codec != null) {
//now check that the codec found actually has the correct
String discriminatorKeyForClass = discriminatorKeys.get(codec.getEncoderClass());
if (fieldName.equals(discriminatorKeyForClass)) {
break;
} else {
discriminator = null;
codec = null;
LOGGER.warn("Confusing. Skipping discriminator {} encoded in discriminator key {} since the " +
"destination class is declaring a different discriminator key {}.",
discriminator, fieldName, discriminatorKeyForClass);
}
}
} else {
reader.skipValue();
}
}
reader.reset();
// try fallback and legacy handling
if (codec == null) {
if (discriminator != null) {
LOGGER.warn("At least one valid discriminator {} was found in database, but no matching codec found at all.", discriminator);
reader.skipValue();
return null; // todo: when switching to mongo db 3.6 an exception should be thrown instead of returning null
}
LOGGER.debug("No discriminator found in db for entity. Trying fallback. Fallback is {}", fallBackCodec);
codec = fallBackCodec;
if (codec == null) {
LOGGER.debug("FallbackCodec is null. Still no matching codec found for discriminator {} within discriminatorToCodec {}", discriminator, discriminatorToCodec);
if (classToCodec.values().size() == 1) {
codec = classToCodec.values().iterator().next();
LOGGER.debug("Found single possible codec {} for type {}", codec, getEncoderClass());
}
else {
LOGGER.warn("Legacy handling to resolve entities in db without discriminator failed as there are (now?) more than one codecs available {}. One option is to use @DiscriminatrFallback at the legacy class or to add discriminators to the entities within the database. For now, skipping value.", classToCodec);
// TODO is skipping the right way to handle this? This might lead to lost data if a read object is rewritten to the database again...
reader.skipValue();
return null;// todo: when switching to mongo db 3.6 an exception should be thrown instead of returning null
}
}
}
return decodeWithType(reader, decoderContext, codec);
}