@Override public DBObject decode(final BsonReader reader, final DecoderContext decoderContext) { return decoder.decode(reader, decoderContext); }
/** * Creates a child context and then deserializes using the reader. * * @param decoder the decoder to decode with * @param reader the reader to decode to * @param <T> the type of the decoder * @return the decoded value * @since 3.5 */ public <T> T decodeWithChildContext(final Decoder<T> decoder, final BsonReader reader) { return decoder.decode(reader, DEFAULT_CONTEXT); }
@Override protected BsonValue readValue(final BsonReader reader, final DecoderContext decoderContext) { if (reader.getCurrentBsonType() == DOCUMENT) { return new BsonDocumentWrapper<T>(decoder.decode(reader, decoderContext), null); } else { return super.readValue(reader, decoderContext); } } }
/** * Parses a string in MongoDB Extended JSON format to a {@code BasicDBObject}. * * @param json the JSON string * @param decoder the decoder to use to decode the BasicDBObject instance * @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, final Decoder<BasicDBObject> decoder) { return decoder.decode(new JsonReader(json), DecoderContext.builder().build()); }
private List<T> projectFromFullNamespaceToCollectionName(final List<BsonDocument> unstripped) { if (unstripped == null) { return null; } List<T> stripped = new ArrayList<T>(unstripped.size()); String prefix = databaseName + "."; for (BsonDocument cur : unstripped) { String name = cur.getString("name").getValue(); String collectionName = name.substring(prefix.length()); cur.put("name", new BsonString(collectionName)); stripped.add(decoder.decode(new BsonDocumentReader(cur), DecoderContext.builder().build())); } return stripped; } }
@Override public BsonArray decode(final BsonReader reader, final DecoderContext decoderContext) { reader.readStartArray(); List<T> list = new ArrayList<T>(); while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) { if (reader.getCurrentBsonType() == BsonType.NULL) { reader.readNull(); list.add(null); } else { list.add(decoder.decode(reader, decoderContext)); } } reader.readEndArray(); return new BsonArrayWrapper<T>(list); }
/** * Parses a string in MongoDB Extended JSON format to a {@code Document} * * @param json the JSON string * @param decoder the {@code Decoder} to use to parse the JSON string into a {@code Document} * @return a corresponding {@code Document} object * @see org.bson.json.JsonReader * @mongodb.driver.manual reference/mongodb-extended-json/ MongoDB Extended JSON */ public static Document parse(final String json, final Decoder<Document> decoder) { notNull("codec", decoder); JsonReader bsonReader = new JsonReader(json); return decoder.decode(bsonReader, DecoderContext.builder().build()); }
/** * Decode this into a document. * * @param decoder the decoder to facilitate the transformation * @param <T> the BSON type that the codec encodes/decodes * @return the decoded document * @since 3.6 */ public <T> T decode(final Decoder<T> decoder) { BsonBinaryReader reader = createReader(); try { return decoder.decode(reader, DecoderContext.builder().build()); } finally { reader.close(); } }
private QueryResult<T> documentToQueryResult(final BsonDocument result, final ServerAddress serverAddress) { QueryResult<T> queryResult; if (isExplain()) { T decodedDocument = decoder.decode(new BsonDocumentReader(result), DecoderContext.builder().build()); queryResult = new QueryResult<T>(getNamespace(), Collections.singletonList(decodedDocument), 0, serverAddress); } else { queryResult = cursorDocumentToQueryResult(result.getDocument("cursor"), serverAddress); } return queryResult; }
@Override protected BsonValue readValue(final BsonReader reader, final DecoderContext decoderContext) { if (fieldsContainingPayload.contains(reader.getCurrentName())) { if (reader.getCurrentBsonType() == BsonType.DOCUMENT) { return new BsonDocumentWrapper<T>(payloadDecoder.decode(reader, decoderContext), null); } else if (reader.getCurrentBsonType() == BsonType.ARRAY) { return new CommandResultArrayCodec<T>(getCodecRegistry(), payloadDecoder).decode(reader, decoderContext); } } return super.readValue(reader, decoderContext); } }
private static Object getUpsertedId(final com.mongodb.bulk.BulkWriteUpsert cur, final Decoder<DBObject> decoder) { return decoder.decode(new BsonDocumentReader(new BsonDocument("_id", cur.getId())), DecoderContext.builder().build()).get("_id"); }
public ReplyMessage(final ResponseBuffers responseBuffers, final Decoder<T> decoder, final long requestId) { this(responseBuffers.getReplyHeader(), requestId); if (replyHeader.getNumberReturned() > 0) { BsonInput bsonInput = new ByteBufferBsonInput(responseBuffers.getBodyByteBuffer().duplicate()); try { while (documents.size() < replyHeader.getNumberReturned()) { BsonBinaryReader reader = new BsonBinaryReader(bsonInput); try { documents.add(decoder.decode(reader, DecoderContext.builder().build())); } finally { reader.close(); } } } finally { bsonInput.close(); responseBuffers.reset(); } } }
@Override protected void callCallback(final BsonDocument response, final Throwable t) { if (t != null) { callback.onResult(null, t); } else { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Command execution completed with status " + ProtocolHelper.isCommandOk(response)); } if (!ProtocolHelper.isCommandOk(response)) { callback.onResult(null, ProtocolHelper.getCommandFailureException(response, getServerAddress())); } else { try { callback.onResult(decoder.decode(new BsonDocumentReader(response), DecoderContext.builder().build()), null); } catch (Throwable t1) { callback.onResult(null, t1); } } } } }
@Override public DBObject decode(final BsonReader reader, final DecoderContext decoderContext) { return decoder.decode(reader, decoderContext); }
@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; } }
/** * Parses a string in MongoDB Extended JSON format to a {@code BasicDBObject}. * * @param json the JSON string * @param decoder the decoder to use to decode the BasicDBObject instance * @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, final Decoder<BasicDBObject> decoder) { return decoder.decode(new JsonReader(json), DecoderContext.builder().build()); }
@Override protected BsonValue readValue(final BsonReader reader, final DecoderContext decoderContext) { if (reader.getCurrentBsonType() == DOCUMENT) { return new BsonDocumentWrapper<T>(decoder.decode(reader, decoderContext), null); } else { return super.readValue(reader, decoderContext); } } }
private QueryResult<T> documentToQueryResult(final BsonDocument result, final ServerAddress serverAddress) { QueryResult<T> queryResult; if (isExplain()) { T decodedDocument = decoder.decode(new BsonDocumentReader(result), DecoderContext.builder().build()); queryResult = new QueryResult<T>(getNamespace(), Collections.singletonList(decodedDocument), 0, serverAddress); } else { queryResult = cursorDocumentToQueryResult(result.getDocument("cursor"), serverAddress); } return queryResult; }
@Override protected BsonValue readValue(final BsonReader reader, final DecoderContext decoderContext) { if (fieldsContainingPayload.contains(reader.getCurrentName())) { if (reader.getCurrentBsonType() == BsonType.DOCUMENT) { return new BsonDocumentWrapper<T>(payloadDecoder.decode(reader, decoderContext), null); } else if (reader.getCurrentBsonType() == BsonType.ARRAY) { return new CommandResultArrayCodec<T>(getCodecRegistry(), payloadDecoder).decode(reader, decoderContext); } } return super.readValue(reader, decoderContext); } }
private static Object getUpsertedId(final com.mongodb.bulk.BulkWriteUpsert cur, final Decoder<DBObject> decoder) { return decoder.decode(new BsonDocumentReader(new BsonDocument("_id", cur.getId())), DecoderContext.builder().build()).get("_id"); }