org.bson.BsonReader Java Examples

The following examples show how to use org.bson.BsonReader. 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: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
@Override
public AwsS3SignPolicyResult decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final Document document = (new DocumentCodec()).decode(reader, decoderContext);
  keyPresent(Fields.POLICY_FIELD, document);
  keyPresent(Fields.SIGNATURE_FIELD, document);
  keyPresent(Fields.ALGORITHM_FIELD, document);
  keyPresent(Fields.DATE_FIELD, document);
  keyPresent(Fields.CREDENTIAL_FIELD, document);
  return new AwsS3SignPolicyResult(
      document.getString(Fields.POLICY_FIELD),
      document.getString(Fields.SIGNATURE_FIELD),
      document.getString(Fields.ALGORITHM_FIELD),
      document.getString(Fields.DATE_FIELD),
      document.getString(Fields.CREDENTIAL_FIELD));
}
 
Example #2
Source File: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompactChangeEvent<DocumentT> decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  final CompactChangeEvent<BsonDocument> rawChangeEvent =
      CompactChangeEvent.fromBsonDocument(document);

  if (codec == null || codec.getClass().equals(BsonDocumentCodec.class)) {
    return (CompactChangeEvent<DocumentT>)rawChangeEvent;
  }
  return new CompactChangeEvent<>(
      rawChangeEvent.getOperationType(),
      rawChangeEvent.getFullDocument() == null ? null : codec.decode(
          rawChangeEvent.getFullDocument().asBsonReader(),
          DecoderContext.builder().build()),
      rawChangeEvent.getDocumentKey(),
      rawChangeEvent.getUpdateDescription(),
      rawChangeEvent.getStitchDocumentVersion(),
      rawChangeEvent.getStitchDocumentHash(),
      rawChangeEvent.hasUncommittedWrites());
}
 
Example #3
Source File: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public ChangeEvent<DocumentT> decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  final ChangeEvent<BsonDocument> rawChangeEvent = ChangeEvent.fromBsonDocument(document);

  if (codec == null || codec.getClass().equals(BsonDocumentCodec.class)) {
    return (ChangeEvent<DocumentT>)rawChangeEvent;
  }
  return new ChangeEvent<>(
      rawChangeEvent.getId(),
      rawChangeEvent.getOperationType(),
      rawChangeEvent.getFullDocument() == null ? null : codec.decode(
          rawChangeEvent.getFullDocument().asBsonReader(),
          DecoderContext.builder().build()),
      rawChangeEvent.getNamespace(),
      rawChangeEvent.getDocumentKey(),
      rawChangeEvent.getUpdateDescription(),
      rawChangeEvent.hasUncommittedWrites());
}
 
Example #4
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 #5
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 #6
Source File: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
public RemoteUpdateResult decode(
    final BsonReader reader,
    final DecoderContext decoderContext) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  keyPresent(Fields.MATCHED_COUNT_FIELD, document);
  keyPresent(Fields.MODIFIED_COUNT_FIELD, document);
  final long matchedCount = document.getNumber(Fields.MATCHED_COUNT_FIELD).longValue();
  final long modifiedCount = document.getNumber(Fields.MODIFIED_COUNT_FIELD).longValue();
  if (!document.containsKey(Fields.UPSERTED_ID_FIELD)) {
    return new RemoteUpdateResult(matchedCount, modifiedCount, null);
  }

  return new RemoteUpdateResult(
      matchedCount,
      modifiedCount,
      document.get(Fields.UPSERTED_ID_FIELD));
}
 
Example #7
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 #8
Source File: EntityDecoder.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public T decode(final BsonReader reader, final DecoderContext decoderContext) {
    T entity;
    if (morphiaCodec.getMappedClass().hasLifecycle(PreLoad.class)
        || morphiaCodec.getMappedClass().hasLifecycle(PostLoad.class)
        || morphiaCodec.getMapper().hasInterceptors()) {
        entity = decodeWithLifecycle(reader, decoderContext);
    } else {
        EntityModel<T> classModel = morphiaCodec.getEntityModel();
        if (decoderContext.hasCheckedDiscriminator()) {
            MorphiaInstanceCreator<T> instanceCreator = getInstanceCreator(classModel);
            decodeProperties(reader, decoderContext, instanceCreator);
            return instanceCreator.getInstance();
        } else {
            entity = getCodecFromDocument(reader, classModel.useDiscriminator(), classModel.getDiscriminatorKey(),
                morphiaCodec.getRegistry(), morphiaCodec.getDiscriminatorLookup(), morphiaCodec)
                         .decode(reader, DecoderContext.builder().checkedDiscriminator(true).build());
        }
    }

    return entity;
}
 
Example #9
Source File: MorphiaReferenceCodec.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public MorphiaReference decode(final BsonReader reader, final DecoderContext decoderContext) {
    Mapper mapper = getDatastore().getMapper();
    Object value = mapper.getCodecRegistry()
                         .get(bsonTypeClassMap.get(reader.getCurrentBsonType()))
                         .decode(reader, decoderContext);
    value = processId(value, mapper, decoderContext);
    if (Set.class.isAssignableFrom(getTypeData().getType())) {
        return new SetReference<>(getDatastore(), getFieldMappedClass(), (List) value);
    } else if (Collection.class.isAssignableFrom(getTypeData().getType())) {
        return new ListReference<>(getDatastore(), getFieldMappedClass(), (List) value);
    } else if (Map.class.isAssignableFrom(getTypeData().getType())) {
        return new MapReference<>(getDatastore(), (Map) value, getFieldMappedClass());
    } else {
        return new SingleReference<>(getDatastore(), getFieldMappedClass(), value);
    }
}
 
Example #10
Source File: DocumentDecoder.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Document decode(BsonReader reader, DecoderContext decoderContext) {
    Document document = super.decode(reader, decoderContext);

    //特殊处理operateTime,时间格式
    String operateTime = "operateTime";
    long time = document.getLong(operateTime);
    document.put(operateTime, new Date(time));

    return document;
}
 
Example #11
Source File: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
public RemoteDeleteResult decode(
    final BsonReader reader,
    final DecoderContext decoderContext) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  keyPresent(Fields.DELETED_COUNT_FIELD, document);
  return new RemoteDeleteResult(document.getNumber(Fields.DELETED_COUNT_FIELD).longValue());
}
 
Example #12
Source File: JacksonCodec.java    From EDDI with Apache License 2.0 5 votes vote down vote up
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
    try {
        RawBsonDocument document = rawBsonDocumentCodec.decode(reader, decoderContext);
        return bsonObjectMapper.readValue(document.getByteBuffer().array(), type);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example #13
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 #14
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 #15
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 #16
Source File: FruitCodec.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Override
public Fruit decode(BsonReader reader, DecoderContext decoderContext) {
    Document document = documentCodec.decode(reader, decoderContext);
    Fruit fruit = new Fruit();
    if (document.getString("id") != null) {
        fruit.setId(document.getString("id"));
    }
    fruit.setName(document.getString("name"));
    fruit.setDescription(document.getString("description"));
    return fruit;
}
 
Example #17
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 #18
Source File: NamespaceSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public NamespaceSynchronizationConfig decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  return fromBsonDocument(document);
}
 
Example #19
Source File: ZonedDateTimeCodec.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
static ZonedDateTime read(BsonReader reader, String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.DATE_TIME) {
        return ZonedDateTime.ofInstant(Instant.ofEpochMilli(reader.readDateTime()), ZoneId.systemDefault());
    } else {
        LOGGER.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
 
Example #20
Source File: JacksonCodec.java    From clouditor with Apache License 2.0 5 votes vote down vote up
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
  RawBsonDocument doc = codec.decode(reader, decoderContext);
  try {
    return mapper
        .readerWithView(DatabaseOnly.class)
        .forType(this.clazz)
        .readValue(doc.getByteBuffer().array());
  } catch (IOException e) {
    throw new MongoException(e.getMessage());
  }
}
 
Example #21
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 #22
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 #23
Source File: ArrayCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
private Object readValue(final BsonReader reader, final DecoderContext decoderContext) {
    BsonType bsonType = reader.getCurrentBsonType();
    if (bsonType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (bsonType == BsonType.BINARY && BsonBinarySubType.isUuid(reader.peekBinarySubType()) && reader.peekBinarySize() == 16) {
        return mapper.getCodecRegistry().get(UUID.class).decode(reader, decoderContext);
    }
    return mapper.getCodecRegistry().get(type.getComponentType()).decode(reader, decoderContext);
}
 
Example #24
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 #25
Source File: ClassCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public Class decode(final BsonReader reader, final DecoderContext decoderContext) {
    try {
        return Class.forName(reader.readString());
    } catch (ClassNotFoundException e) {
        throw new MappingException(e.getMessage(), e);
    }
}
 
Example #26
Source File: JacksonCodecs.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
  final IOContext ioContext = new IOContext(new BufferRecycler(), null, false);
  final BsonParser parser = new BsonParser(ioContext, 0, (AbstractBsonReader) reader);
  try {
    return mapper.readValue(parser, getEncoderClass());
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #27
Source File: MorphiaCollectionCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<T> decode(final BsonReader reader, final DecoderContext decoderContext) {
    if (reader.getCurrentBsonType().equals(BsonType.ARRAY)) {
        return super.decode(reader, decoderContext);
    }
    final Collection<T> collection = getInstance();
    T value = getCodec().decode(reader, decoderContext);
    collection.add(value);
    return collection;
}
 
Example #28
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 #29
Source File: OptionalProvider.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Override
public final O decode(BsonReader reader, DecoderContext decoderContext) {
  if (reader.getCurrentBsonType() == BsonType.NULL) {
    reader.readNull();
    return empty();
  }
  return nullable(delegate.decode(reader, decoderContext));
}
 
Example #30
Source File: JacksonCodecRegistry.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
  Preconditions.checkArgument(reader instanceof AbstractBsonReader,
          "Expected reader to be %s for %s but was %s",
          AbstractBsonReader.class.getName(), clazz, reader.getClass());
  final BsonParser parser = new BsonParser(ioContext, 0, (AbstractBsonReader) reader);
  try {
    return this.reader.readValue(parser);
  } catch (IOException e) {
    throw new UncheckedIOException("Error while decoding " + clazz, e);
  }
}