use of dev.morphia.mapping.MappingException in project morphia by mongodb.
the class Conversions method registerStringConversions.
private static void registerStringConversions() {
register(String.class, BigDecimal.class, BigDecimal::new);
register(String.class, ObjectId.class, ObjectId::new);
register(String.class, Character.class, s -> {
if (s.length() == 1) {
return s.charAt(0);
} else if (s.isEmpty()) {
return (char) 0;
} else {
throw new MappingException("Could not convert String to char: " + s);
}
});
register(String.class, Boolean.class, Boolean::parseBoolean);
register(String.class, Byte.class, Byte::parseByte);
register(String.class, Double.class, Double::parseDouble);
register(String.class, Integer.class, Integer::valueOf);
register(String.class, Long.class, Long::parseLong);
register(String.class, Float.class, Float::parseFloat);
register(String.class, Short.class, Short::parseShort);
register(String.class, URI.class, str -> URI.create(str.replace("%46", ".")));
register(String.class, UUID.class, UUID::fromString);
}
use of dev.morphia.mapping.MappingException in project morphia by mongodb.
the class KeyCodec method decode.
@Override
public Key decode(BsonReader reader, DecoderContext decoderContext) {
reader.readStartDocument();
final String ref = reader.readString("$ref");
reader.readName();
final BsonReaderMark mark = reader.getMark();
Object idValue = null;
EntityModel model = null;
final Iterator<EntityModel> iterator = datastore.getMapper().getClassesMappedToCollection(ref).iterator();
while (idValue == null && iterator.hasNext()) {
model = iterator.next();
try {
final PropertyModel idField = model.getIdProperty();
if (idField != null) {
final Class<?> idType = idField.getType();
idValue = datastore.getCodecRegistry().get(idType).decode(reader, decoderContext);
}
} catch (Exception e) {
mark.reset();
}
}
if (idValue == null) {
throw new MappingException("Could not map the Key to a type.");
}
reader.readEndDocument();
return new Key<>(model.getType(), ref, idValue);
}
Aggregations