Java Code Examples for org.bson.BsonReader#getMark()

The following examples show how to use org.bson.BsonReader#getMark() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityDecoder.java    From morphia with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected Codec<T> getCodecFromDocument(final BsonReader reader, final boolean useDiscriminator, final String discriminatorKey,
                                        final CodecRegistry registry, final DiscriminatorLookup discriminatorLookup,
                                        final Codec<T> defaultCodec) {
    Codec<T> codec = null;
    if (useDiscriminator) {
        BsonReaderMark mark = reader.getMark();
        try {
            reader.readStartDocument();
            while (codec == null && reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
                if (discriminatorKey.equals(reader.readName())) {
                    codec = (Codec<T>) registry.get(discriminatorLookup.lookup(reader.readString()));
                } else {
                    reader.skipValue();
                }
            }
        } catch (Exception e) {
            throw new CodecConfigurationException(String.format("Failed to decode '%s'. Decoding errored with: %s",
                morphiaCodec.getEntityModel().getName(), e.getMessage()), e);
        } finally {
            mark.reset();
        }
    }
    return codec != null ? codec : defaultCodec;
}
 
Example 2
Source File: KeyCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public Key decode(final BsonReader reader, final DecoderContext decoderContext) {
    reader.readStartDocument();

    final String ref = reader.readString("$ref");
    final List<MappedClass> classes = mapper.getClassesMappedToCollection(ref);
    reader.readName();
    final BsonReaderMark mark = reader.getMark();
    final Iterator<MappedClass> iterator = classes.iterator();
    Object idValue = null;
    MappedClass mappedClass = null;
    while (idValue == null && iterator.hasNext()) {
        mappedClass = iterator.next();
        try {
            final MappedField idField = mappedClass.getIdField();
            if (idField != null) {
                final Class<?> idType = idField.getTypeData().getType();
                idValue = mapper.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<>(mappedClass.getType(), ref, idValue);
}
 
Example 3
Source File: ObjectCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(final BsonReader reader, final DecoderContext decoderContext) {
    BsonType bsonType = reader.getCurrentBsonType();
    Class<?> clazz;
    if (bsonType == BsonType.DOCUMENT) {
        clazz = Document.class;
        String discriminatorField = mapper.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 = mapper.getClass(reader.readString());
                } catch (CodecConfigurationException e) {
                    throw new MappingException(e.getMessage(), e);
                }
            } else {
                reader.skipValue();
            }
        }
        mark.reset();
    } else {
        clazz = bsonTypeClassMap.get(bsonType);
    }
    return mapper.getCodecRegistry()
                 .get(clazz)
                 .decode(reader, decoderContext);
}