Refine search
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"); }
@Override public BsonType readBsonType() { if (getState() == State.INITIAL || getState() == State.SCOPE_DOCUMENT) { setCurrentBsonType(BsonType.DOCUMENT); setState(State.VALUE); return getCurrentBsonType(); if (getState() != State.TYPE) { throwInvalidState("ReadBSONType", State.TYPE); switch (getContext().getContextType()) { case ARRAY: currentValue = getContext().getNextValue(); if (currentValue == null) { setState(State.END_OF_ARRAY); return BsonType.END_OF_DOCUMENT; setState(State.VALUE); break; case DOCUMENT: Map.Entry<String, BsonValue> currentElement = getContext().getNextElement(); if (currentElement == null) { setState(State.END_OF_DOCUMENT); return BsonType.END_OF_DOCUMENT; setCurrentName(currentElement.getKey()); currentValue = currentElement.getValue(); setState(State.NAME); break;
@Override protected void doReadEndDocument() { setContext(getContext().getParentContext()); switch (getContext().getContextType()) { case ARRAY: case DOCUMENT: setState(State.TYPE); break; case TOP_LEVEL: setState(State.DONE); break; default: throw new BSONException("Unexpected ContextType."); } }
@Override protected void doReadEndArray() { setContext(getContext().getParentContext()); }
/** * Reading from BSON to GSON */ @Test public void bsonToGson() throws Exception { BsonDocument document = new BsonDocument(); document.append("boolean", new BsonBoolean(true)); document.append("int32", new BsonInt32(32)); document.append("int64", new BsonInt64(64)); document.append("double", new BsonDouble(42.42D)); document.append("string", new BsonString("foo")); document.append("null", new BsonNull()); document.append("array", new BsonArray()); document.append("object", new BsonDocument()); JsonElement element = TypeAdapters.JSON_ELEMENT.read(new BsonReader(new BsonDocumentReader(document))); check(element.isJsonObject()); check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().isBoolean()); check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().getAsBoolean()); check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().getAsNumber().intValue()).is(32); check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().getAsNumber().longValue()).is(64L); check(element.getAsJsonObject().get("double").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("double").getAsJsonPrimitive().getAsNumber().doubleValue()).is(42.42D); check(element.getAsJsonObject().get("string").getAsJsonPrimitive().isString()); check(element.getAsJsonObject().get("string").getAsJsonPrimitive().getAsString()).is("foo"); check(element.getAsJsonObject().get("null").isJsonNull()); check(element.getAsJsonObject().get("array").isJsonArray()); check(element.getAsJsonObject().get("object").isJsonObject()); }
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; } }
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; }
@Nullable private Document asDocumentOrNull(final BsonDocument bsonDocument) { if (bsonDocument.isEmpty()) { return null; } else { BsonDocumentReader reader = new BsonDocumentReader(bsonDocument); return documentCodec.decode(reader, DecoderContext.builder().build()); } } }
public static DBObject toDBObject(final BsonDocument document) { return MongoClient.getDefaultCodecRegistry().get(DBObject.class).decode(new BsonDocumentReader(document), DecoderContext.builder().build()); }
/** * Gets the first value in the document * * @return the first value in the document * @throws java.util.NoSuchElementException if the document is empty * @since 3.7 */ public BsonReader asBsonReader() { return new BsonDocumentReader(this); }
/** * Construct a new instance. * * @param document the document to read from */ public BsonDocumentReader(final BsonDocument document) { super(); setContext(new Context(null, BsonContextType.TOP_LEVEL, document)); currentValue = document; }
protected Mark() { super(); currentValue = BsonDocumentReader.this.currentValue; context = BsonDocumentReader.this.getContext(); context.mark(); }
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; } }
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; }
@Nullable private Document asDocumentOrNull(final BsonDocument bsonDocument) { if (bsonDocument.isEmpty()) { return null; } else { BsonDocumentReader reader = new BsonDocumentReader(bsonDocument); return documentCodec.decode(reader, DecoderContext.builder().build()); } } }
@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); } } } } }
DBObject obj = getDefaultDBObjectCodec().decode(new BsonDocumentReader(document), DecoderContext.builder().build()); return obj.get("value");
@Override protected void doReadStartArray() { BsonArray array = currentValue.asArray(); setContext(new Context(getContext(), BsonContextType.ARRAY, array)); }
public void reset() { super.reset(); BsonDocumentReader.this.currentValue = currentValue; BsonDocumentReader.this.setContext(context); context.reset(); } }
private WriteResult translateWriteResult(final int count, final boolean isUpdateOfExisting, @Nullable final BsonValue upsertedId) { Object newUpsertedId = upsertedId == null ? null : getObjectCodec().decode(new BsonDocumentReader(new BsonDocument("_id", upsertedId)), DecoderContext.builder().build()) .get("_id"); return new WriteResult(count, isUpdateOfExisting, newUpsertedId); }