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

The following examples show how to use org.bson.BsonReader#readStartDocument() . 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: 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 3
Source File: BsonRecordReader.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void write(ComplexWriter writer, BsonReader reader) throws IOException {
  this.reader = reader;
  reader.readStartDocument();
  BsonType readBsonType = reader.getCurrentBsonType();
  switch (readBsonType) {
  case DOCUMENT:
    writeToListOrMap(reader, new MapOrListWriterImpl(writer.rootAsMap()), false, null);
    break;
  default:
    throw new DrillRuntimeException("Root object must be DOCUMENT type. Found: " + readBsonType);
  }
}
 
Example 4
Source File: IamCodec.java    From Shadbot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public IamBean decode(BsonReader reader, DecoderContext decoderContext) {
    try (reader) {
        reader.readStartDocument();
        return new IamBean(reader.readString(), reader.readString());
    }
}
 
Example 5
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 6
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 7
Source File: BsonCodecRepoTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Override
public Somebody decode(BsonReader reader, DecoderContext decoderContext) {
  reader.readStartDocument();
  ObjectId id = reader.readObjectId("_id");
  String prop1 = reader.readString("prop1");
  Date date = new Date(reader.readDateTime("dateChanged"));
  reader.readEndDocument();
  return ImmutableSomebody.builder().id(id).prop1(prop1).date(date).build();
}
 
Example 8
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 9
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 10
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);
}