private Codec<T> getWrapped() { if (wrapped == null) { wrapped = registry.get(clazz); } return wrapped; } }
/** * Create a new instance * * @param registry the codec registry */ public GridFSFileCodec(final CodecRegistry registry) { this.documentCodec = notNull("DocumentCodec", notNull("registry", registry).get(Document.class)); this.bsonDocumentCodec = notNull("BsonDocumentCodec", registry.get(BsonDocument.class)); }
@Override public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) { try { Codec<?> codec = registry.get(type.getRawClass()); return serializer(codec); } catch (CodecConfigurationException e) { return null; } } };
@SuppressWarnings({"unchecked", "rawtypes"}) private void writeValue(final BsonWriter writer, final EncoderContext encoderContext, final Object value) { if (value == null) { writer.writeNull(); } else { Codec codec = registry.get(value.getClass()); encoderContext.encodeWithChildContext(codec, writer, value); } }
/** * Parses a string in MongoDB Extended JSON format to a {@code BasicDBObject}. * * @param json the JSON string * @return a corresponding {@code BasicDBObject} object * @see org.bson.json.JsonReader * @mongodb.driver.manual reference/mongodb-extended-json/ MongoDB Extended JSON */ public static BasicDBObject parse(final String json) { return parse(json, DBObjectCodec.getDefaultRegistry().get(BasicDBObject.class)); }
@Override @SuppressWarnings("unchecked") public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) { if (clazz == CodeWithScope.class) { return (Codec<T>) new CodeWithScopeCodec(registry.get(Document.class)); } if (clazz == Document.class) { return (Codec<T>) new DocumentCodec(registry, bsonTypeClassMap, valueTransformer); } return null; }
/** * Gets a JSON representation of this document * * <p>With the default {@link DBObjectCodec}.</p> * * @param writerSettings the json writer settings to use when encoding * @return a JSON representation of this document * @throws org.bson.codecs.configuration.CodecConfigurationException if the document contains types not in the default registry */ public String toJson(final JsonWriterSettings writerSettings) { return toJson(writerSettings, DBObjectCodec.getDefaultRegistry().get(BasicDBObject.class)); }
@Override @SuppressWarnings({"unchecked", "rawtypes"}) public void encode(final BsonWriter writer, final BsonArray array, final EncoderContext encoderContext) { writer.writeStartArray(); for (BsonValue value : array) { Codec codec = codecRegistry.get(value.getClass()); encoderContext.encodeWithChildContext(codec, writer, value); } writer.writeEndArray(); }
@SuppressWarnings({"unchecked", "rawtypes"}) static void encodeCoordinateReferenceSystem(final BsonWriter writer, final Geometry geometry, final EncoderContext encoderContext, final CodecRegistry registry) { CoordinateReferenceSystem coordinateReferenceSystem = geometry.getCoordinateReferenceSystem(); if (coordinateReferenceSystem != null) { writer.writeName("crs"); Codec codec = registry.get(coordinateReferenceSystem.getClass()); encoderContext.encodeWithChildContext(codec, writer, coordinateReferenceSystem); } }
@SuppressWarnings("unchecked") static <TItem> void encodeValue(final BsonDocumentWriter writer, final TItem value, final CodecRegistry codecRegistry) { if (value == null) { writer.writeNull(); } else if (value instanceof Bson) { ((Encoder) codecRegistry.get(BsonDocument.class)).encode(writer, ((Bson) value).toBsonDocument(BsonDocument.class, codecRegistry), EncoderContext.builder().build()); } else { ((Encoder) codecRegistry.get(value.getClass())).encode(writer, value, EncoderContext.builder().build()); } }
<TResult> ListDatabasesOperation<TResult> listDatabases(final Class<TResult> resultClass, final Bson filter, final Boolean nameOnly, final long maxTimeMS) { return new ListDatabasesOperation<TResult>(codecRegistry.get(resultClass)).maxTime(maxTimeMS, MILLISECONDS) .filter(toBsonDocumentOrNull(filter)) .nameOnly(nameOnly); }
@Override public <TDocument> MongoIterable<TDocument> withDocumentClass(final Class<TDocument> clazz) { return new MongoIterableImpl<TDocument>(getClientSession(), getExecutor(), getReadConcern(), getReadPreference()) { private ReadOperation<BatchCursor<TDocument>> operation = createChangeStreamOperation(codecRegistry.get(clazz)); @Override public ReadOperation<BatchCursor<TDocument>> asReadOperation() { return operation; } }; }
<TResult> ListCollectionsOperation<TResult> listCollections(final String databaseName, final Class<TResult> resultClass, final Bson filter, final boolean collectionNamesOnly, final Integer batchSize, final long maxTimeMS) { return new ListCollectionsOperation<TResult>(databaseName, codecRegistry.get(resultClass)) .filter(toBsonDocumentOrNull(filter)) .nameOnly(collectionNamesOnly) .batchSize(batchSize == null ? 0 : batchSize) .maxTime(maxTimeMS, MILLISECONDS); }
@SuppressWarnings("unchecked") private BsonDocument toBsonDocument(final String key, final Object value) { BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument()); writer.writeStartDocument(); writer.writeName(key); ((Encoder) database.getCodecRegistry().get(value.getClass())).encode(writer, value, ENCODER_CONTEXT); writer.writeEndDocument(); return writer.getDocument(); }
private <TResult> TResult executeCommand(@Nullable final ClientSession clientSession, final Bson command, final ReadPreference readPreference, final Class<TResult> resultClass) { notNull("readPreference", readPreference); if (clientSession != null && clientSession.hasActiveTransaction() && !readPreference.equals(ReadPreference.primary())) { throw new MongoClientException("Read preference in a transaction must be primary"); } return executor.execute(new CommandReadOperation<TResult>(getName(), toBsonDocument(command), codecRegistry.get(resultClass)), readPreference, readConcern, clientSession); }
private static BsonValue getField(final BsonReader bsonReader, final String fieldName) { bsonReader.readStartDocument(); while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) { if (bsonReader.readName().equals(fieldName)) { return REGISTRY.get(getClassForBsonType(bsonReader.getCurrentBsonType())).decode(bsonReader, DecoderContext.builder().build()); } bsonReader.skipValue(); } bsonReader.readEndDocument(); return null; }
private static byte[] toBson(final DBObject dbObject) { OutputBuffer outputBuffer = new BasicOutputBuffer(); DBObjectCodec.getDefaultRegistry().get(DBObject.class).encode(new BsonBinaryWriter(outputBuffer), dbObject, EncoderContext.builder().build()); return outputBuffer.toByteArray(); }
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 registry.get(UUID.class).decode(reader, decoderContext); } return valueTransformer.transform(bsonTypeCodecMap.get(bsonType).decode(reader, decoderContext)); } }
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.ARRAY) { return readList(reader, decoderContext); } else if (bsonType == BsonType.BINARY && BsonBinarySubType.isUuid(reader.peekBinarySubType()) && reader.peekBinarySize() == 16) { return registry.get(UUID.class).decode(reader, decoderContext); } return valueTransformer.transform(bsonTypeCodecMap.get(bsonType).decode(reader, decoderContext)); }
BulkWriteResult executeBulkWriteOperation(final boolean ordered, final Boolean bypassDocumentValidation, final List<WriteRequest> writeRequests, final WriteConcern writeConcern) { try { return translateBulkWriteResult(executor.execute(new MixedBulkWriteOperation(getNamespace(), translateWriteRequestsToNew(writeRequests), ordered, writeConcern, false) .bypassDocumentValidation(bypassDocumentValidation), getReadConcern()), getObjectCodec()); } catch (MongoBulkWriteException e) { throw BulkWriteHelper.translateBulkWriteException(e, MongoClient.getDefaultCodecRegistry().get(DBObject.class)); } }