Search in sources :

Example 6 with CodecConfigurationException

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

the class GeometryDecoderHelper method decodeGeometry.

private static Geometry decodeGeometry(final BsonReader reader) {
    String type = null;
    BsonReaderMark mark = reader.getMark();
    validateIsDocument(reader);
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String key = reader.readName();
        if (key.equals("type")) {
            type = reader.readString();
            break;
        } else {
            reader.skipValue();
        }
    }
    mark.reset();
    if (type == null) {
        throw new CodecConfigurationException("Invalid Geometry item, document contained no type information.");
    }
    Geometry geometry = null;
    if (type.equals("Point")) {
        geometry = decodePoint(reader);
    } else if (type.equals("MultiPoint")) {
        geometry = decodeMultiPoint(reader);
    } else if (type.equals("Polygon")) {
        geometry = decodePolygon(reader);
    } else if (type.equals("MultiPolygon")) {
        geometry = decodeMultiPolygon(reader);
    } else if (type.equals("LineString")) {
        geometry = decodeLineString(reader);
    } else if (type.equals("MultiLineString")) {
        geometry = decodeMultiLineString(reader);
    } else if (type.equals("GeometryCollection")) {
        geometry = decodeGeometryCollection(reader);
    } else {
        throw new CodecConfigurationException(format("Invalid Geometry item, found type '%s'.", type));
    }
    return geometry;
}
Also used : Geometry(com.mongodb.client.model.geojson.Geometry) BsonReaderMark(org.bson.BsonReaderMark) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString)

Example 7 with CodecConfigurationException

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

the class GeometryDecoderHelper method decodeMultiPoint.

private static MultiPoint decodeMultiPoint(final BsonReader reader) {
    String type = null;
    List<Position> 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 = decodeCoordinates(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 MultiPoint, document contained no type information.");
    } else if (!type.equals("MultiPoint")) {
        throw new CodecConfigurationException(format("Invalid MultiPoint, found type '%s'.", type));
    } else if (coordinates == null) {
        throw new CodecConfigurationException("Invalid MultiPoint, missing position coordinates.");
    }
    return crs != null ? new MultiPoint(crs, coordinates) : new MultiPoint(coordinates);
}
Also used : MultiPoint(com.mongodb.client.model.geojson.MultiPoint) 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)

Example 8 with CodecConfigurationException

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

the class GeometryDecoderHelper method decodeMultiPolygon.

private static MultiPolygon decodeMultiPolygon(final BsonReader reader) {
    String type = null;
    List<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 = decodeMultiPolygonCoordinates(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 MultiPolygon, document contained no type information.");
    } else if (!type.equals("MultiPolygon")) {
        throw new CodecConfigurationException(format("Invalid MultiPolygon, found type '%s'.", type));
    } else if (coordinates == null) {
        throw new CodecConfigurationException("Invalid MultiPolygon, missing coordinates.");
    }
    return crs != null ? new MultiPolygon(crs, coordinates) : new MultiPolygon(coordinates);
}
Also used : MultiPolygon(com.mongodb.client.model.geojson.MultiPolygon) 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)

Example 9 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project realm-java by realm.

the class JniBsonProtocol method decode.

public static <T> T decode(String string, Decoder<T> decoder) {
    try {
        StringReader stringReader = new StringReader(string);
        JsonReader jsonReader = new JsonReader(stringReader);
        jsonReader.readStartDocument();
        jsonReader.readName(VALUE);
        T value = decoder.decode(jsonReader, DecoderContext.builder().build());
        jsonReader.readEndDocument();
        return value;
    } catch (CodecConfigurationException e) {
        // might be missing
        throw new AppException(ErrorCode.BSON_CODEC_NOT_FOUND, "Could not resolve decoder for end type" + string, e);
    } catch (Exception e) {
        throw new AppException(ErrorCode.BSON_DECODING, "Error decoding value " + string, e);
    }
}
Also used : AppException(io.realm.mongodb.AppException) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) StringReader(java.io.StringReader) JsonReader(org.bson.json.JsonReader) AppException(io.realm.mongodb.AppException) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException)

Example 10 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project realm-java by realm.

the class JniBsonProtocol method encode.

public static <T> String encode(T value, Encoder<T> encoder) {
    try {
        StringWriter stringWriter = new StringWriter();
        JsonWriter jsonWriter = new JsonWriter(stringWriter, writerSettings);
        jsonWriter.writeStartDocument();
        jsonWriter.writeName(VALUE);
        encoder.encode(jsonWriter, value, EncoderContext.builder().build());
        jsonWriter.writeEndDocument();
        return stringWriter.toString();
    } catch (CodecConfigurationException e) {
        // might be missing
        throw new AppException(ErrorCode.BSON_CODEC_NOT_FOUND, "Could not resolve encoder for end type", e);
    } catch (Exception e) {
        throw new AppException(ErrorCode.BSON_ENCODING, "Error encoding value", e);
    }
}
Also used : AppException(io.realm.mongodb.AppException) StringWriter(java.io.StringWriter) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) JsonWriter(org.bson.json.JsonWriter) AppException(io.realm.mongodb.AppException) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException)

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