Search in sources :

Example 1 with BsonInvalidOperationException

use of org.bson.BsonInvalidOperationException in project mongo-java-driver by mongodb.

the class PojoCodecImpl method decodePropertyModel.

@SuppressWarnings("unchecked")
private <S> void decodePropertyModel(final BsonReader reader, final DecoderContext decoderContext, final InstanceCreator<T> instanceCreator, final String name, final PropertyModel<S> propertyModel) {
    if (propertyModel != null) {
        try {
            S value = null;
            if (reader.getCurrentBsonType() == BsonType.NULL) {
                reader.readNull();
            } else {
                Codec<S> codec = propertyModel.getCachedCodec();
                if (codec == null) {
                    throw new CodecConfigurationException(format("Missing codec in '%s' for '%s'", classModel.getName(), propertyModel.getName()));
                }
                value = decoderContext.decodeWithChildContext(codec, reader);
            }
            if (propertyModel.isWritable()) {
                instanceCreator.set(value, propertyModel);
            }
        } catch (BsonInvalidOperationException | CodecConfigurationException e) {
            throw new CodecConfigurationException(format("Failed to decode '%s'. Decoding '%s' errored with: %s", classModel.getName(), name, e.getMessage()), e);
        }
    } else {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(format("Found property not present in the ClassModel: %s", name));
        }
        reader.skipValue();
    }
}
Also used : BsonInvalidOperationException(org.bson.BsonInvalidOperationException) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException)

Example 2 with BsonInvalidOperationException

use of org.bson.BsonInvalidOperationException in project mongo-java-driver by mongodb.

the class NumberCodecHelper method decodeInt.

static int decodeInt(final BsonReader reader) {
    int intValue;
    BsonType bsonType = reader.getCurrentBsonType();
    switch(bsonType) {
        case INT32:
            intValue = reader.readInt32();
            break;
        case INT64:
            long longValue = reader.readInt64();
            intValue = (int) longValue;
            if (longValue != (long) intValue) {
                throw invalidConversion(Integer.class, longValue);
            }
            break;
        case DOUBLE:
            double doubleValue = reader.readDouble();
            intValue = (int) doubleValue;
            if (doubleValue != (double) intValue) {
                throw invalidConversion(Integer.class, doubleValue);
            }
            break;
        case DECIMAL128:
            Decimal128 decimal128 = reader.readDecimal128();
            intValue = decimal128.intValue();
            if (!decimal128.equals(new Decimal128(intValue))) {
                throw invalidConversion(Integer.class, decimal128);
            }
            break;
        default:
            throw new BsonInvalidOperationException(format("Invalid numeric type, found: %s", bsonType));
    }
    return intValue;
}
Also used : BsonType(org.bson.BsonType) BsonInvalidOperationException(org.bson.BsonInvalidOperationException) Decimal128(org.bson.types.Decimal128)

Example 3 with BsonInvalidOperationException

use of org.bson.BsonInvalidOperationException in project mongo-java-driver by mongodb.

the class NumberCodecHelper method decodeDouble.

static double decodeDouble(final BsonReader reader) {
    double doubleValue;
    BsonType bsonType = reader.getCurrentBsonType();
    switch(bsonType) {
        case INT32:
            doubleValue = reader.readInt32();
            break;
        case INT64:
            long longValue = reader.readInt64();
            doubleValue = longValue;
            if (longValue != (long) doubleValue) {
                throw invalidConversion(Double.class, longValue);
            }
            break;
        case DOUBLE:
            doubleValue = reader.readDouble();
            break;
        case DECIMAL128:
            Decimal128 decimal128 = reader.readDecimal128();
            try {
                doubleValue = decimal128.doubleValue();
                if (!decimal128.equals(new Decimal128(new BigDecimal(doubleValue)))) {
                    throw invalidConversion(Double.class, decimal128);
                }
            } catch (NumberFormatException e) {
                throw invalidConversion(Double.class, decimal128);
            }
            break;
        default:
            throw new BsonInvalidOperationException(format("Invalid numeric type, found: %s", bsonType));
    }
    return doubleValue;
}
Also used : BsonType(org.bson.BsonType) BsonInvalidOperationException(org.bson.BsonInvalidOperationException) Decimal128(org.bson.types.Decimal128) BigDecimal(java.math.BigDecimal)

Example 4 with BsonInvalidOperationException

use of org.bson.BsonInvalidOperationException in project morphia by mongodb.

the class EntityDecoder method decodeModel.

protected void decodeModel(BsonReader reader, DecoderContext decoderContext, MorphiaInstanceCreator instanceCreator, @Nullable PropertyModel model) {
    if (model != null) {
        final BsonReaderMark mark = reader.getMark();
        try {
            if (reader.getCurrentBsonType() == BsonType.NULL) {
                reader.readNull();
            } else {
                Object value = decoderContext.decodeWithChildContext(model.getCodec(), reader);
                instanceCreator.set(value, model);
            }
        } catch (BsonInvalidOperationException e) {
            mark.reset();
            final Object value = morphiaCodec.getDatastore().getCodecRegistry().get(Object.class).decode(reader, decoderContext);
            instanceCreator.set(convert(value, model.getTypeData().getType()), model);
        }
    } else {
        reader.skipValue();
    }
}
Also used : BsonReaderMark(org.bson.BsonReaderMark) BsonInvalidOperationException(org.bson.BsonInvalidOperationException)

Example 5 with BsonInvalidOperationException

use of org.bson.BsonInvalidOperationException in project spring-data-mongodb by spring-projects.

the class ParameterBindingDocumentCodec method decode.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Document decode(final BsonReader reader, final DecoderContext decoderContext) {
    if (reader instanceof ParameterBindingJsonReader) {
        ParameterBindingJsonReader bindingReader = (ParameterBindingJsonReader) reader;
        // binds just placeholder queries like: `@Query(?0)`
        if (bindingReader.currentValue instanceof org.bson.Document) {
            return (Document) bindingReader.currentValue;
        } else if (bindingReader.currentValue instanceof String) {
            try {
                return decode((String) bindingReader.currentValue, new Object[0]);
            } catch (JsonParseException jsonParseException) {
                throw new IllegalArgumentException("Expression result is not a valid json document!", jsonParseException);
            }
        } else if (bindingReader.currentValue instanceof Map) {
            return new Document((Map) bindingReader.currentValue);
        }
    }
    Document document = new Document();
    try {
        reader.readStartDocument();
        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            String fieldName = reader.readName();
            Object value = readValue(reader, decoderContext);
            document.put(fieldName, value);
        }
        reader.readEndDocument();
    } catch (JsonParseException | BsonInvalidOperationException e) {
        try {
            Object value = readValue(reader, decoderContext);
            if (value instanceof Map<?, ?>) {
                if (!((Map) value).isEmpty()) {
                    return new Document((Map<String, Object>) value);
                }
            }
        } catch (Exception ex) {
            e.addSuppressed(ex);
            throw e;
        }
    }
    return document;
}
Also used : BsonInvalidOperationException(org.bson.BsonInvalidOperationException) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) JsonParseException(org.bson.json.JsonParseException) Map(java.util.Map) BsonInvalidOperationException(org.bson.BsonInvalidOperationException) JsonParseException(org.bson.json.JsonParseException)

Aggregations

BsonInvalidOperationException (org.bson.BsonInvalidOperationException)7 BsonType (org.bson.BsonType)3 Decimal128 (org.bson.types.Decimal128)3 BigDecimal (java.math.BigDecimal)1 Map (java.util.Map)1 INodeBuilder (nl.nn.adapterframework.stream.document.INodeBuilder)1 BsonDocument (org.bson.BsonDocument)1 BsonReaderMark (org.bson.BsonReaderMark)1 Document (org.bson.Document)1 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)1 JsonParseException (org.bson.json.JsonParseException)1 SAXException (org.xml.sax.SAXException)1