use of org.bson.BsonType in project mongo-java-driver by mongodb.
the class LazyPropertyModelCodec method getCodecFromPropertyRegistry.
@SuppressWarnings("unchecked")
private Codec<T> getCodecFromPropertyRegistry(final PropertyModel<T> propertyModel) {
Codec<T> localCodec;
try {
localCodec = propertyCodecRegistry.get(propertyModel.getTypeData());
} catch (CodecConfigurationException e) {
return new LazyMissingCodec<>(propertyModel.getTypeData().getType(), e);
}
if (localCodec == null) {
localCodec = new LazyMissingCodec<>(propertyModel.getTypeData().getType(), new CodecConfigurationException("Unexpected missing codec for: " + propertyModel.getName()));
}
BsonType representation = propertyModel.getBsonRepresentation();
if (representation != null) {
if (localCodec instanceof RepresentationConfigurable) {
return ((RepresentationConfigurable<T>) localCodec).withRepresentation(representation);
}
throw new CodecConfigurationException("Codec must implement RepresentationConfigurable to support BsonRepresentation");
}
return localCodec;
}
use of org.bson.BsonType in project mongo-java-driver by mongodb.
the class ConventionAnnotationImpl method processPropertyAnnotations.
private void processPropertyAnnotations(final ClassModelBuilder<?> classModelBuilder, final PropertyModelBuilder<?> propertyModelBuilder) {
for (Annotation annotation : propertyModelBuilder.getReadAnnotations()) {
if (annotation instanceof BsonProperty) {
BsonProperty bsonProperty = (BsonProperty) annotation;
if (!"".equals(bsonProperty.value())) {
propertyModelBuilder.readName(bsonProperty.value());
}
propertyModelBuilder.discriminatorEnabled(bsonProperty.useDiscriminator());
if (propertyModelBuilder.getName().equals(classModelBuilder.getIdPropertyName())) {
classModelBuilder.idPropertyName(null);
}
} else if (annotation instanceof BsonId) {
classModelBuilder.idPropertyName(propertyModelBuilder.getName());
} else if (annotation instanceof BsonIgnore) {
propertyModelBuilder.readName(null);
} else if (annotation instanceof BsonRepresentation) {
BsonRepresentation bsonRepresentation = (BsonRepresentation) annotation;
BsonType bsonRep = bsonRepresentation.value();
propertyModelBuilder.bsonRepresentation(bsonRep);
}
}
for (Annotation annotation : propertyModelBuilder.getWriteAnnotations()) {
if (annotation instanceof BsonProperty) {
BsonProperty bsonProperty = (BsonProperty) annotation;
if (!"".equals(bsonProperty.value())) {
propertyModelBuilder.writeName(bsonProperty.value());
}
} else if (annotation instanceof BsonIgnore) {
propertyModelBuilder.writeName(null);
}
}
}
use of org.bson.BsonType in project mongo-java-driver by mongodb.
the class NumberCodecHelper method decodeLong.
static long decodeLong(final BsonReader reader) {
long longValue;
BsonType bsonType = reader.getCurrentBsonType();
switch(bsonType) {
case INT32:
longValue = reader.readInt32();
break;
case INT64:
longValue = reader.readInt64();
break;
case DOUBLE:
double doubleValue = reader.readDouble();
longValue = (long) doubleValue;
if (doubleValue != (double) longValue) {
throw invalidConversion(Long.class, doubleValue);
}
break;
case DECIMAL128:
Decimal128 decimal128 = reader.readDecimal128();
longValue = decimal128.longValue();
if (!decimal128.equals(new Decimal128(longValue))) {
throw invalidConversion(Long.class, decimal128);
}
break;
default:
throw new BsonInvalidOperationException(format("Invalid numeric type, found: %s", bsonType));
}
return longValue;
}
use of org.bson.BsonType in project spring-data-mongodb by spring-projects.
the class ParameterBindingDocumentCodec method readValue.
private Object readValue(final BsonReader reader, final DecoderContext decoderContext) {
// Spring Data Customization START
if (reader instanceof ParameterBindingJsonReader) {
ParameterBindingJsonReader bindingReader = (ParameterBindingJsonReader) reader;
// returns the replacement value
if (bindingReader.currentValue != null) {
Object value = bindingReader.currentValue;
if (ObjectUtils.nullSafeEquals(BsonType.DATE_TIME, bindingReader.getCurrentBsonType()) && !(value instanceof Date)) {
if (value instanceof Number) {
value = new Date(NumberUtils.convertNumberToTargetClass((Number) value, Long.class));
} else if (value instanceof String) {
value = new Date(DateTimeFormatter.parse((String) value));
}
}
bindingReader.setState(State.TYPE);
bindingReader.currentValue = null;
return value;
}
}
// Spring Data Customization END
BsonType bsonType = reader.getCurrentBsonType();
if (bsonType == BsonType.NULL) {
reader.readNull();
return null;
} else if (bsonType == BsonType.ARRAY) {
return readList(reader, decoderContext);
} else if (bsonType == BsonType.BINARY && BsonBinarySubType.isUuid(reader.peekBinarySubType()) && reader.peekBinarySize() == 16) {
return registry.get(UUID.class).decode(reader, decoderContext);
}
// Spring Data Customization START
// By default the registry uses DocumentCodec for parsing.
// We need to reroute that to our very own implementation or we'll end up only mapping half the placeholders.
Codec<?> codecToUse = bsonTypeCodecMap.get(bsonType);
if (codecToUse instanceof org.bson.codecs.DocumentCodec) {
codecToUse = this;
}
return valueTransformer.transform(codecToUse.decode(reader, decoderContext));
// Spring Data Customization END
}
use of org.bson.BsonType in project core-ng-project by neowu.
the class BsonReaderWrapper method readLong.
public Long readLong(String field) {
BsonType currentType = reader.getCurrentBsonType();
if (currentType == BsonType.NULL) {
reader.readNull();
return null;
} else if (currentType == BsonType.INT32) {
return (long) reader.readInt32();
} else if (currentType == BsonType.INT64) {
return reader.readInt64();
} else {
logger.warn("unexpected field type, field={}, type={}", field, currentType);
reader.skipValue();
return null;
}
}
Aggregations