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

The following examples show how to use org.bson.BsonReader#readStartArray() . 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: 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 2
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 3
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 4
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 5
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 6
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;
}