Search in sources :

Example 11 with MappingException

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);
}
Also used : ObjectId(org.bson.types.ObjectId) UUID(java.util.UUID) BigDecimal(java.math.BigDecimal) MappingException(dev.morphia.mapping.MappingException)

Example 12 with MappingException

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);
}
Also used : BsonReaderMark(org.bson.BsonReaderMark) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) MappingException(dev.morphia.mapping.MappingException) Key(dev.morphia.Key) MappingException(dev.morphia.mapping.MappingException)

Aggregations

MappingException (dev.morphia.mapping.MappingException)12 Document (org.bson.Document)4 ArrayList (java.util.ArrayList)3 DBRef (com.mongodb.DBRef)2 Key (dev.morphia.Key)2 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)2 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)2 Field (java.lang.reflect.Field)2 BsonReaderMark (org.bson.BsonReaderMark)2 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)2 Test (org.testng.annotations.Test)2 NonNull (com.mongodb.lang.NonNull)1 Nullable (com.mongodb.lang.Nullable)1 Index (dev.morphia.annotations.Index)1 Reference (dev.morphia.annotations.Reference)1 Mapper (dev.morphia.mapping.Mapper)1 DocumentReader (dev.morphia.mapping.codec.reader.DocumentReader)1 ListReference (dev.morphia.mapping.experimental.ListReference)1 MapReference (dev.morphia.mapping.experimental.MapReference)1 MorphiaReference (dev.morphia.mapping.experimental.MorphiaReference)1