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);
}
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);
}
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();
}
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;
}
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);
}
Aggregations