Java Code Examples for org.bson.BsonReader#getCurrentBsonType()

The following examples show how to use org.bson.BsonReader#getCurrentBsonType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnumCodec.java    From core-ng-project with Apache License 2.0 6 votes vote down vote up
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 new Error(format("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;
    }
}
 
Example 2
Source File: ArrayCodec.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public Object[] decode(final BsonReader reader, final DecoderContext decoderContext) {
    List<Object> list = new ArrayList<>();
    if (reader.getCurrentBsonType() == BsonType.ARRAY) {
        reader.readStartArray();

        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            list.add(readValue(reader, decoderContext));
        }

        reader.readEndArray();
    } else {
        list.add(readValue(reader, decoderContext));
    }

    return list.toArray();
}
 
Example 3
Source File: MorphiaMapPropertyCodecProvider.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public Map<K, V> decode(final BsonReader reader, final DecoderContext context) {
    reader.readStartDocument();
    Map<K, V> map = getInstance();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        final K key = (K) Conversions.convert(reader.readName(), keyType);
        if (reader.getCurrentBsonType() == BsonType.NULL) {
            map.put(key, null);
            reader.readNull();
        } else {
            map.put(key, codec.decode(reader, context));
        }
    }
    reader.readEndDocument();
    return map;
}
 
Example 4
Source File: BsonRecordReader.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void write(ComplexWriter writer, BsonReader reader) throws IOException {
  this.reader = reader;
  reader.readStartDocument();
  BsonType readBsonType = reader.getCurrentBsonType();
  switch (readBsonType) {
  case DOCUMENT:
    writeToListOrMap(reader, new MapOrListWriterImpl(writer.rootAsMap()), false, null);
    break;
  default:
    throw new DrillRuntimeException("Root object must be DOCUMENT type. Found: " + readBsonType);
  }
}
 
Example 5
Source File: StringCodecExt.java    From xian with Apache License 2.0 5 votes vote down vote up
@Override
public String decode(BsonReader reader, DecoderContext decoderContext) {
    if (reader.getCurrentBsonType() == BsonType.OBJECT_ID) {
        return reader.readObjectId().toString();
    }
    return super.decode(reader, decoderContext);
}
 
Example 6
Source File: LocalDateTimeCodec.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
static LocalDateTime read(BsonReader reader, String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.DATE_TIME) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(reader.readDateTime()), ZoneId.systemDefault());
    } else {
        LOGGER.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
 
Example 7
Source File: ZonedDateTimeCodec.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
static ZonedDateTime read(BsonReader reader, String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.DATE_TIME) {
        return ZonedDateTime.ofInstant(Instant.ofEpochMilli(reader.readDateTime()), ZoneId.systemDefault());
    } else {
        LOGGER.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
 
Example 8
Source File: OptionalProvider.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Override
public final O decode(BsonReader reader, DecoderContext decoderContext) {
  if (reader.getCurrentBsonType() == BsonType.NULL) {
    reader.readNull();
    return empty();
  }
  return nullable(delegate.decode(reader, decoderContext));
}
 
Example 9
Source File: ArrayCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
private Object readValue(final BsonReader reader, final DecoderContext decoderContext) {
    BsonType bsonType = reader.getCurrentBsonType();
    if (bsonType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (bsonType == BsonType.BINARY && BsonBinarySubType.isUuid(reader.peekBinarySubType()) && reader.peekBinarySize() == 16) {
        return mapper.getCodecRegistry().get(UUID.class).decode(reader, decoderContext);
    }
    return mapper.getCodecRegistry().get(type.getComponentType()).decode(reader, decoderContext);
}
 
Example 10
Source File: CollectionCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<T> decode(final BsonReader reader, final DecoderContext context) {
    Collection<T> collection = getInstance();
    reader.readStartArray();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        if (reader.getCurrentBsonType() == BsonType.NULL) {
            collection.add(null);
            reader.readNull();
        } else {
            collection.add(codec.decode(reader, context));
        }
    }
    reader.readEndArray();
    return collection;
}
 
Example 11
Source File: ObjectCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(final BsonReader reader, final DecoderContext decoderContext) {
    BsonType bsonType = reader.getCurrentBsonType();
    Class<?> clazz;
    if (bsonType == BsonType.DOCUMENT) {
        clazz = Document.class;
        String discriminatorField = mapper.getOptions().getDiscriminatorKey();

        BsonReaderMark mark = reader.getMark();
        reader.readStartDocument();
        while (clazz.equals(Document.class) && reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            if (reader.readName().equals(discriminatorField)) {
                try {
                    clazz = mapper.getClass(reader.readString());
                } catch (CodecConfigurationException e) {
                    throw new MappingException(e.getMessage(), e);
                }
            } else {
                reader.skipValue();
            }
        }
        mark.reset();
    } else {
        clazz = bsonTypeClassMap.get(bsonType);
    }
    return mapper.getCodecRegistry()
                 .get(clazz)
                 .decode(reader, decoderContext);
}
 
Example 12
Source File: AbstractJsonCodec.java    From vertx-mongo-client with Apache License 2.0 4 votes vote down vote up
protected Object readValue(BsonReader reader, DecoderContext ctx) {
  BsonType type = reader.getCurrentBsonType();
  switch (type) {
    case NULL:
      return readNull(reader, ctx);
    case ARRAY:
      return readArray(reader, ctx);
    case BINARY:
      return readBinary(reader, ctx);
    case BOOLEAN:
      return readBoolean(reader, ctx);
    case DATE_TIME:
      return readDateTime(reader, ctx);
    case DB_POINTER:
      return readDbPointer(reader, ctx);
    case DOCUMENT:
      return readDocument(reader, ctx);
    case DOUBLE:
      return readDouble(reader, ctx);
    case INT32:
      return readInt32(reader, ctx);
    case INT64:
      return readInt64(reader, ctx);
    case MAX_KEY:
      return readMaxKey(reader, ctx);
    case MIN_KEY:
      return readMinKey(reader, ctx);
    case JAVASCRIPT:
      return readJavaScript(reader, ctx);
    case JAVASCRIPT_WITH_SCOPE:
      return readJavaScriptWithScope(reader, ctx);
    case OBJECT_ID:
      return readObjectId(reader, ctx);
    case REGULAR_EXPRESSION:
      return readRegularExpression(reader, ctx);
    case STRING:
      return readString(reader, ctx);
    case SYMBOL:
      return readSymbol(reader, ctx);
    case TIMESTAMP:
      return readTimeStamp(reader, ctx);
    case UNDEFINED:
      return readUndefined(reader, ctx);
    case DECIMAL128:
      return readNumberDecimal(reader, ctx);
    default:
      throw new IllegalStateException("Unknown bson type " + type);
  }
}