Search in sources :

Example 21 with CodecConfigurationException

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

the class GeometryDecoderHelper method decodePolygon.

private static Polygon decodePolygon(final BsonReader reader) {
    String type = null;
    PolygonCoordinates coordinates = 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")) {
            coordinates = decodePolygonCoordinates(reader);
        } else if (key.equals("crs")) {
            crs = decodeCoordinateReferenceSystem(reader);
        } else {
            throw new CodecConfigurationException(format("Unexpected key '%s' found when decoding a GeoJSON Polygon", key));
        }
    }
    reader.readEndDocument();
    if (type == null) {
        throw new CodecConfigurationException("Invalid Polygon, document contained no type information.");
    } else if (!type.equals("Polygon")) {
        throw new CodecConfigurationException(format("Invalid Polygon, found type '%s'.", type));
    } else if (coordinates == null) {
        throw new CodecConfigurationException("Invalid Polygon, missing coordinates.");
    }
    return crs != null ? new Polygon(crs, coordinates) : new Polygon(coordinates);
}
Also used : CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) PolygonCoordinates(com.mongodb.client.model.geojson.PolygonCoordinates) CoordinateReferenceSystem(com.mongodb.client.model.geojson.CoordinateReferenceSystem) NamedCoordinateReferenceSystem(com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem) Polygon(com.mongodb.client.model.geojson.Polygon) MultiPolygon(com.mongodb.client.model.geojson.MultiPolygon)

Example 22 with CodecConfigurationException

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

the class GeometryDecoderHelper method decodeGeometryCollection.

private static GeometryCollection decodeGeometryCollection(final BsonReader reader) {
    String type = null;
    List<? extends Geometry> geometries = 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("geometries")) {
            geometries = decodeGeometries(reader);
        } else if (key.equals("crs")) {
            crs = decodeCoordinateReferenceSystem(reader);
        } else {
            throw new CodecConfigurationException(format("Unexpected key '%s' found when decoding a GeoJSON Polygon", key));
        }
    }
    reader.readEndDocument();
    if (type == null) {
        throw new CodecConfigurationException("Invalid GeometryCollection, document contained no type information.");
    } else if (!type.equals("GeometryCollection")) {
        throw new CodecConfigurationException(format("Invalid GeometryCollection, found type '%s'.", type));
    } else if (geometries == null) {
        throw new CodecConfigurationException("Invalid GeometryCollection, missing geometries.");
    }
    return crs != null ? new GeometryCollection(crs, geometries) : new GeometryCollection(geometries);
}
Also used : GeometryCollection(com.mongodb.client.model.geojson.GeometryCollection) 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)

Example 23 with CodecConfigurationException

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

the class GeometryEncoderHelper method encodeGeometry.

@SuppressWarnings("unchecked")
static void encodeGeometry(final BsonWriter writer, final Geometry value, final EncoderContext encoderContext, final CodecRegistry registry) {
    writer.writeStartDocument();
    writer.writeString("type", value.getType().getTypeName());
    if (value instanceof GeometryCollection) {
        writer.writeName("geometries");
        encodeGeometryCollection(writer, (GeometryCollection) value, encoderContext, registry);
    } else {
        writer.writeName("coordinates");
        if (value instanceof Point) {
            encodePoint(writer, (Point) value);
        } else if (value instanceof MultiPoint) {
            encodeMultiPoint(writer, (MultiPoint) value);
        } else if (value instanceof Polygon) {
            encodePolygon(writer, (Polygon) value);
        } else if (value instanceof MultiPolygon) {
            encodeMultiPolygon(writer, (MultiPolygon) value);
        } else if (value instanceof LineString) {
            encodeLineString(writer, (LineString) value);
        } else if (value instanceof MultiLineString) {
            encodeMultiLineString(writer, (MultiLineString) value);
        } else {
            throw new CodecConfigurationException(format("Unsupported Geometry: %s", value));
        }
    }
    encodeCoordinateReferenceSystem(writer, value, encoderContext, registry);
    writer.writeEndDocument();
}
Also used : GeometryCollection(com.mongodb.client.model.geojson.GeometryCollection) MultiPoint(com.mongodb.client.model.geojson.MultiPoint) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) MultiPolygon(com.mongodb.client.model.geojson.MultiPolygon) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Point(com.mongodb.client.model.geojson.Point) MultiPoint(com.mongodb.client.model.geojson.MultiPoint) Polygon(com.mongodb.client.model.geojson.Polygon) MultiPolygon(com.mongodb.client.model.geojson.MultiPolygon)

Example 24 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project morphia by mongodb.

the class ReferenceCodec method processId.

/**
 * Decodes an ID value
 *
 * @param datastore      the Datastore to use
 * @param decode         the value to decode
 * @param decoderContext the decoder context
 * @return the decoded value
 */
@NonNull
public static Object processId(Datastore datastore, Object decode, DecoderContext decoderContext) {
    Object id = decode;
    if (id instanceof Iterable) {
        Iterable<?> iterable = (Iterable<?>) id;
        List<Object> ids = new ArrayList<>();
        for (Object o : iterable) {
            ids.add(processId(datastore, o, decoderContext));
        }
        id = ids;
    } else if (id instanceof Document) {
        Document document = (Document) id;
        if (document.containsKey("$ref")) {
            id = processId(datastore, new DBRef(document.getString("$db"), document.getString("$ref"), document.get("$id")), decoderContext);
        } else if (document.containsKey(datastore.getMapper().getOptions().getDiscriminatorKey())) {
            try {
                id = datastore.getCodecRegistry().get(datastore.getMapper().getClass(document)).decode(new DocumentReader(document), decoderContext);
            } catch (CodecConfigurationException e) {
                throw new MappingException(Sofia.cannotFindTypeInDocument(), e);
            }
        }
    } else if (id instanceof DBRef) {
        DBRef ref = (DBRef) id;
        Object refId = ref.getId();
        if (refId instanceof Document) {
            refId = datastore.getCodecRegistry().get(Object.class).decode(new DocumentReader((Document) refId), decoderContext);
        }
        id = new DBRef(ref.getDatabaseName(), ref.getCollectionName(), refId);
    }
    return id;
}
Also used : CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) ArrayList(java.util.ArrayList) DBRef(com.mongodb.DBRef) DocumentReader(dev.morphia.mapping.codec.reader.DocumentReader) Document(org.bson.Document) MappingException(dev.morphia.mapping.MappingException) NonNull(com.mongodb.lang.NonNull)

Example 25 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project morphia by mongodb.

the class ObjectCodec method decode.

@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
    BsonType bsonType = reader.getCurrentBsonType();
    Class<?> clazz;
    if (bsonType == BsonType.DOCUMENT) {
        clazz = Document.class;
        String discriminatorField = datastore.getMapper().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 = datastore.getMapper().getClass(reader.readString());
                } catch (CodecConfigurationException e) {
                    throw new MappingException(e.getMessage(), e);
                }
            } else {
                reader.skipValue();
            }
        }
        mark.reset();
    } else {
        clazz = bsonTypeClassMap.get(bsonType);
    }
    return datastore.getCodecRegistry().get(clazz).decode(reader, decoderContext);
}
Also used : BsonType(org.bson.BsonType) BsonReaderMark(org.bson.BsonReaderMark) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Document(org.bson.Document) MappingException(dev.morphia.mapping.MappingException)

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