Java Code Examples for org.bson.BsonType#END_OF_DOCUMENT

The following examples show how to use org.bson.BsonType#END_OF_DOCUMENT . 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: TypedArrayCodec.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public Object decode(final BsonReader reader, final DecoderContext decoderContext) {
    reader.readStartArray();

    List list = new ArrayList<>();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        list.add(getCodec().decode(reader, decoderContext));
    }

    reader.readEndArray();

    Object array = Array.newInstance(type, list.size());
    for (int i = 0; i < list.size(); i++) {
        Array.set(array, i, list.get(i));
    }

    return array;
}
 
Example 3
Source File: ArrayCodec.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public Object[] decode(final BsonReader reader, final DecoderContext decoderContext) {
    List<Object> list = new ArrayList<>();
    if (reader.getCurrentBsonType() == BsonType.ARRAY) {
        reader.readStartArray();

        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            list.add(readValue(reader, decoderContext));
        }

        reader.readEndArray();
    } else {
        list.add(readValue(reader, decoderContext));
    }

    return list.toArray();
}
 
Example 4
Source File: MorphiaMapPropertyCodecProvider.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public Map<K, V> decode(final BsonReader reader, final DecoderContext context) {
    reader.readStartDocument();
    Map<K, V> map = getInstance();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        final K key = (K) Conversions.convert(reader.readName(), keyType);
        if (reader.getCurrentBsonType() == BsonType.NULL) {
            map.put(key, null);
            reader.readNull();
        } else {
            map.put(key, codec.decode(reader, context));
        }
    }
    reader.readEndDocument();
    return map;
}
 
Example 5
Source File: CollectionDecoder.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<ResultT> decode(final BsonReader reader, final DecoderContext decoderContext) {
  final Collection<ResultT> docs = new ArrayList<>();
  reader.readStartArray();
  while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
    final ResultT doc = decoder.decode(reader, decoderContext);
    docs.add(doc);
  }
  reader.readEndArray();
  return docs;
}
 
Example 6
Source File: PrimitiveArrayCodec.java    From Prism with MIT License 5 votes vote down vote up
@Override
public PrimitiveArray decode(BsonReader reader, DecoderContext decoderContext) {
    reader.readStartDocument();
    String key = reader.readString("key");

    List<Number> value = Lists.newArrayList();
    if (StringUtils.equals(key, PrimitiveArray.BYTE_ARRAY_ID)) {
        reader.readName("value");
        reader.readStartArray();
        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            value.add(byteCodec.decode(reader, decoderContext));
        }

        reader.readEndArray();
    } else if (StringUtils.equals(key, PrimitiveArray.INT_ARRAY_ID)) {
        reader.readName("value");
        reader.readStartArray();
        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            value.add(integerCodec.decode(reader, decoderContext));
        }

        reader.readEndArray();
    } else if (StringUtils.equals(key, PrimitiveArray.LONG_ARRAY_ID)) {
        reader.readName("value");
        reader.readStartArray();
        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            value.add(longCodec.decode(reader, decoderContext));
        }

        reader.readEndArray();
    } else {
        reader.readEndDocument();
        throw new BsonInvalidOperationException("Unsupported primitive type");
    }

    reader.readEndDocument();
    return new PrimitiveArray(key, value);
}
 
Example 7
Source File: AbstractJsonCodec.java    From vertx-mongo-client with Apache License 2.0 5 votes vote down vote up
protected O readDocument(BsonReader reader, DecoderContext ctx) {
  O object = newObject();

  reader.readStartDocument();
  while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
    String name = reader.readName();
    add(object, name, readValue(reader, ctx));
  }
  reader.readEndDocument();

  return object;
}
 
Example 8
Source File: AbstractJsonCodec.java    From vertx-mongo-client with Apache License 2.0 5 votes vote down vote up
protected A readArray(BsonReader reader, DecoderContext ctx) {
  A array = newArray();

  reader.readStartArray();
  while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
    add(array, readValue(reader, ctx));
  }
  reader.readEndArray();

  return array;
}
 
Example 9
Source File: EntityDecoder.java    From morphia with Apache License 2.0 5 votes vote down vote up
protected void decodeProperties(final BsonReader reader, final DecoderContext decoderContext,
                                final MorphiaInstanceCreator<T> instanceCreator) {
    reader.readStartDocument();
    EntityModel<T> classModel = morphiaCodec.getEntityModel();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String name = reader.readName();
        if (classModel.useDiscriminator() && classModel.getDiscriminatorKey().equals(name)) {
            reader.readString();
        } else {
            decodeModel(reader, decoderContext, instanceCreator, classModel.getFieldModelByName(name));
        }
    }
    reader.readEndDocument();
}
 
Example 10
Source File: CollectionCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<T> decode(final BsonReader reader, final DecoderContext context) {
    Collection<T> collection = getInstance();
    reader.readStartArray();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        if (reader.getCurrentBsonType() == BsonType.NULL) {
            collection.add(null);
            reader.readNull();
        } else {
            collection.add(codec.decode(reader, context));
        }
    }
    reader.readEndArray();
    return collection;
}
 
Example 11
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);
}
 
Example 12
Source File: ReaderState.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
BsonType getCurrentBsonType() {
    return BsonType.END_OF_DOCUMENT;
}
 
Example 13
Source File: ReaderState.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
BsonType getCurrentBsonType() {
    return BsonType.END_OF_DOCUMENT;
}