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