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

The following examples show how to use org.bson.BsonReader#readString() . 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: EnumCodec.java    From core-ng-project with Apache License 2.0 6 votes vote down vote up
public T read(BsonReader reader, String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.STRING) {
        String enumValue = reader.readString();
        T value = decodingMappings.get(enumValue);
        if (value == null) throw new Error(format("can not decode value to enum, enumClass={}, value={}", enumClass.getCanonicalName(), enumValue));
        return value;
    } else {
        logger.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
 
Example 2
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 3
Source File: PersistenceModule.java    From EDDI with Apache License 2.0 5 votes vote down vote up
@Override
public URI decode(BsonReader reader, DecoderContext decoderContext) {
    String uriString = reader.readString();
    try {
        return new URI(uriString);
    } catch (URISyntaxException e) {
        throw new BsonInvalidOperationException(
                String.format("Cannot create URI from string '%s'", uriString));

    }
}
 
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: 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 6
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 7
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 8
Source File: BigDecimalCodec.java    From mongo-mapper with Apache License 2.0 4 votes vote down vote up
@Override
public BigDecimal decode(final BsonReader reader, final DecoderContext decoderContext) {
    return new BigDecimal(reader.readString());
}
 
Example 9
Source File: CustomFieldCodec.java    From mongo-mapper with Apache License 2.0 4 votes vote down vote up
@Override
public CustomCodecField decode(BsonReader reader, DecoderContext decoderContext) {
    String s = reader.readString();
    return new CustomCodecField(s);
}
 
Example 10
Source File: AbstractJsonCodec.java    From vertx-mongo-client with Apache License 2.0 4 votes vote down vote up
protected Object readString(BsonReader reader, DecoderContext ctx) {
  return reader.readString();
}