Search in sources :

Example 1 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException 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 CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class GeometryDecoderHelper method decodePolygonCoordinates.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static PolygonCoordinates decodePolygonCoordinates(final BsonReader reader) {
    validateIsArray(reader);
    reader.readStartArray();
    List<List<Position>> values = new ArrayList<List<Position>>();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        values.add(decodeCoordinates(reader));
    }
    reader.readEndArray();
    if (values.isEmpty()) {
        throw new CodecConfigurationException("Invalid Polygon no coordinates.");
    }
    List<Position> exterior = values.remove(0);
    ArrayList[] holes = values.toArray(new ArrayList[values.size()]);
    try {
        return new PolygonCoordinates(exterior, holes);
    } catch (IllegalArgumentException e) {
        throw new CodecConfigurationException(format("Invalid Polygon: %s", e.getMessage()));
    }
}
Also used : Position(com.mongodb.client.model.geojson.Position) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PolygonCoordinates(com.mongodb.client.model.geojson.PolygonCoordinates)

Example 3 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class GeometryDecoderHelper method decodePoint.

private static Point decodePoint(final BsonReader reader) {
    String type = null;
    Position position = null;
    CoordinateReferenceSystem crs = null;
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String key = reader.readName();
        if (key.equals("type")) {
            type = reader.readString();
        } else if (key.equals("coordinates")) {
            position = decodePosition(reader);
        } else if (key.equals("crs")) {
            crs = decodeCoordinateReferenceSystem(reader);
        } else {
            throw new CodecConfigurationException(format("Unexpected key '%s' found when decoding a GeoJSON point", key));
        }
    }
    reader.readEndDocument();
    if (type == null) {
        throw new CodecConfigurationException("Invalid Point, document contained no type information.");
    } else if (!type.equals("Point")) {
        throw new CodecConfigurationException(format("Invalid Point, found type '%s'.", type));
    } else if (position == null) {
        throw new CodecConfigurationException("Invalid Point, missing position coordinates.");
    }
    return crs != null ? new Point(crs, position) : new Point(position);
}
Also used : Position(com.mongodb.client.model.geojson.Position) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) CoordinateReferenceSystem(com.mongodb.client.model.geojson.CoordinateReferenceSystem) NamedCoordinateReferenceSystem(com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem) Point(com.mongodb.client.model.geojson.Point) MultiPoint(com.mongodb.client.model.geojson.MultiPoint)

Example 4 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class GeometryDecoderHelper method decodeCoordinateReferenceSystemProperties.

private static String decodeCoordinateReferenceSystemProperties(final BsonReader reader) {
    String crsName = null;
    validateIsDocument(reader);
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String name = reader.readName();
        if (name.equals("name")) {
            crsName = reader.readString();
        } else {
            throw new CodecConfigurationException(format("Found invalid key '%s' in the CoordinateReferenceSystem.", name));
        }
    }
    reader.readEndDocument();
    if (crsName == null) {
        throw new CodecConfigurationException("Found invalid properties in the CoordinateReferenceSystem.");
    }
    return crsName;
}
Also used : CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString)

Example 5 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class GeometryDecoderHelper method decodePosition.

private static Position decodePosition(final BsonReader reader) {
    validateIsArray(reader);
    reader.readStartArray();
    List<Double> values = new ArrayList<Double>();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        values.add(readAsDouble(reader));
    }
    reader.readEndArray();
    try {
        return new Position(values);
    } catch (IllegalArgumentException e) {
        throw new CodecConfigurationException(format("Invalid Position: %s", e.getMessage()));
    }
}
Also used : Position(com.mongodb.client.model.geojson.Position) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) ArrayList(java.util.ArrayList)

Aggregations

CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)26 LineString (com.mongodb.client.model.geojson.LineString)11 MultiLineString (com.mongodb.client.model.geojson.MultiLineString)11 NamedCoordinateReferenceSystem (com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)8 CoordinateReferenceSystem (com.mongodb.client.model.geojson.CoordinateReferenceSystem)7 Position (com.mongodb.client.model.geojson.Position)5 ArrayList (java.util.ArrayList)4 BsonDocument (org.bson.BsonDocument)4 Document (org.bson.Document)4 MultiPoint (com.mongodb.client.model.geojson.MultiPoint)3 MultiPolygon (com.mongodb.client.model.geojson.MultiPolygon)3 PolygonCoordinates (com.mongodb.client.model.geojson.PolygonCoordinates)3 BsonBinary (org.bson.BsonBinary)3 BsonReaderMark (org.bson.BsonReaderMark)3 BsonType (org.bson.BsonType)3 Test (org.junit.Test)3 GeometryCollection (com.mongodb.client.model.geojson.GeometryCollection)2 Point (com.mongodb.client.model.geojson.Point)2 Polygon (com.mongodb.client.model.geojson.Polygon)2 MappingException (dev.morphia.mapping.MappingException)2