private Object readResolve() { return new BsonDocumentCodec().decode(new BsonBinaryReader(ByteBuffer.wrap(bytes) .order(ByteOrder.LITTLE_ENDIAN)), DecoderContext.builder().build()); } }
@Override public T read(JsonReader in) throws IOException { BsonReader reader = (BsonReader) in; org.bson.BsonReader delegate = reader.unwrap(); return codec.decode(delegate, DecoderContext.builder().build()); } };
/** * 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; } }
/** * Parses a string in MongoDB Extended JSON format to a {@code BsonDocument} * * @param json the JSON string * @return a corresponding {@code BsonDocument} object * @see org.bson.json.JsonReader * @mongodb.driver.manual reference/mongodb-extended-json/ MongoDB Extended JSON */ public static BsonDocument parse(final String json) { return new BsonDocumentCodec().decode(new JsonReader(json), DecoderContext.builder().build()); }
/** * Parses a string in MongoDB Extended JSON format to a {@code BsonArray} * * @param json the JSON string * @return a corresponding {@code BsonArray} object * @see org.bson.json.JsonReader * @mongodb.driver.manual reference/mongodb-extended-json/ MongoDB Extended JSON * * @since 3.4 */ public static BsonArray parse(final String json) { return new BsonArrayCodec().decode(new JsonReader(json), DecoderContext.builder().build()); }
/** * 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 BsonDocument toBsonDocument() { BsonBinaryReader bsonReader = createReader(); try { return new BsonDocumentCodec().decode(bsonReader, DecoderContext.builder().build()); } finally { bsonReader.close(); } }
@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()); }
/** * Parses a string in MongoDB Extended JSON format to a {@code RawBsonDocument} * * @param json the JSON string * @return a corresponding {@code RawBsonDocument} object * @see org.bson.json.JsonReader * @mongodb.driver.manual reference/mongodb-extended-json/ MongoDB Extended JSON * @since 3.3 */ public static RawBsonDocument parse(final String json) { notNull("json", json); return new RawBsonDocumentCodec().decode(new JsonReader(json), DecoderContext.builder().build()); }
private BsonValue deserializeBsonValue(final BsonReader bsonReader) { return REGISTRY.get(getClassForBsonType(bsonReader.getCurrentBsonType())).decode(bsonReader, DecoderContext.builder().build()); }
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; }
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 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"); }
static BsonDocument toBson(JsonObject gson) throws IOException { return new BsonDocumentCodec().decode(asBsonReader(gson), DecoderContext.builder().build()); }
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); }
BsonDocument toBsonDocument() { ByteBuf duplicateByteBuf = byteBuf.duplicate(); BsonBinaryReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(duplicateByteBuf)); try { return new BsonDocumentCodec().decode(bsonReader, DecoderContext.builder().build()); } finally { duplicateByteBuf.release(); bsonReader.close(); } }
@Override public T decode(final BsonReader reader, final DecoderContext decoderContext) { if (decoderContext.hasCheckedDiscriminator()) { if (!specialized) { throw new CodecConfigurationException(format("%s contains generic types that have not been specialised.%n" + "Top level classes with generic types are not supported by the PojoCodec.", classModel.getName())); } InstanceCreator<T> instanceCreator = classModel.getInstanceCreator(); decodeProperties(reader, decoderContext, instanceCreator); return instanceCreator.getInstance(); } else { return getCodecFromDocument(reader, classModel.useDiscriminator(), classModel.getDiscriminatorKey(), registry, discriminatorLookup, this).decode(reader, DecoderContext.builder().checkedDiscriminator(true).build()); } }