Search in sources :

Example 11 with BsonType

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;
}
Also used : BsonType(org.bson.BsonType) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) RepresentationConfigurable(org.bson.codecs.RepresentationConfigurable)

Example 12 with BsonType

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);
        }
    }
}
Also used : BsonType(org.bson.BsonType) BsonId(org.bson.codecs.pojo.annotations.BsonId) BsonRepresentation(org.bson.codecs.pojo.annotations.BsonRepresentation) BsonProperty(org.bson.codecs.pojo.annotations.BsonProperty) Annotation(java.lang.annotation.Annotation) BsonIgnore(org.bson.codecs.pojo.annotations.BsonIgnore)

Example 13 with BsonType

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;
}
Also used : BsonType(org.bson.BsonType) BsonInvalidOperationException(org.bson.BsonInvalidOperationException) Decimal128(org.bson.types.Decimal128)

Example 14 with BsonType

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
}
Also used : BsonType(org.bson.BsonType) UUID(java.util.UUID) Date(java.util.Date)

Example 15 with BsonType

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;
    }
}
Also used : BsonType(org.bson.BsonType)

Aggregations

BsonType (org.bson.BsonType)20 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)4 MapOrListWriterImpl (org.apache.drill.exec.vector.complex.impl.MapOrListWriterImpl)4 BsonInvalidOperationException (org.bson.BsonInvalidOperationException)3 Decimal128 (org.bson.types.Decimal128)3 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 Schema (org.apache.kafka.connect.data.Schema)2 BsonDocument (org.bson.BsonDocument)2 BsonValue (org.bson.BsonValue)2 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 Nullable (com.mongodb.lang.Nullable)1 MappingException (dev.morphia.mapping.MappingException)1 Annotation (java.lang.annotation.Annotation)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 Entry (java.util.Map.Entry)1 UUID (java.util.UUID)1 SchemaBuilder (org.apache.kafka.connect.data.SchemaBuilder)1