/** * Creates a child context based on this and serializes the value with it to the writer. * * @param encoder the encoder to encode value with * @param writer the writer to encode to * @param value the value to encode * @param <T> the type of the value */ public <T> void encodeWithChildContext(final Encoder<T> encoder, final BsonWriter writer, final T value) { encoder.encode(writer, value, DEFAULT_CONTEXT); }
@Override public void encode(final BsonWriter writer, final DBObject value, final EncoderContext encoderContext) { encoder.encode(writer, value, encoderContext); }
/** * Gets a JSON representation of this document * * @param writerSettings the json writer settings to use when encoding * @param encoder the document codec instance to use to encode the document * @return a JSON representation of this document * @throws org.bson.codecs.configuration.CodecConfigurationException if the registry does not contain a codec for the document values. */ public String toJson(final JsonWriterSettings writerSettings, final Encoder<Document> encoder) { JsonWriter writer = new JsonWriter(new StringWriter(), writerSettings); encoder.encode(writer, this, EncoderContext.builder().isEncodingCollectibleDocument(true).build()); return writer.getWriter().toString(); }
/** * Gets a JSON representation of this document * * @param writerSettings the json writer settings to use when encoding * @param encoder the BasicDBObject codec instance to encode the document with * @return a JSON representation of this document * @throws org.bson.codecs.configuration.CodecConfigurationException if the registry does not contain a codec for the document values. */ public String toJson(final JsonWriterSettings writerSettings, final Encoder<BasicDBObject> encoder) { JsonWriter writer = new JsonWriter(new StringWriter(), writerSettings); encoder.encode(writer, this, EncoderContext.builder().isEncodingCollectibleDocument(true).build()); return writer.getWriter().toString(); }
@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()); } }
@Override @SuppressWarnings("unchecked") public void encode(final BsonWriter writer, final BsonDocumentWrapper value, final EncoderContext encoderContext) { if (value.isUnwrapped()) { bsonDocumentCodec.encode(writer, value, encoderContext); } else { Encoder encoder = value.getEncoder(); encoder.encode(writer, value.getWrappedDocument(), encoderContext); } }
private BsonDocument getUnwrapped() { if (encoder == null) { throw new BsonInvalidOperationException("Can not unwrap a BsonDocumentWrapper with no Encoder"); } if (unwrapped == null) { BsonDocument unwrapped = new BsonDocument(); BsonWriter writer = new BsonDocumentWriter(unwrapped); encoder.encode(writer, wrappedDocument, EncoderContext.builder().build()); this.unwrapped = unwrapped; } return unwrapped; }
@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(); }
@SuppressWarnings("unchecked") private <T> void addDocument(final T obj, final Encoder<T> encoder, final EncoderContext encoderContext, final BsonOutput bsonOutput, final FieldNameValidator validator, final int maxDocumentSize, final List<BsonElement> extraElements) { BsonBinaryWriter bsonBinaryWriter = new BsonBinaryWriter(new BsonWriterSettings(), new BsonBinaryWriterSettings(maxDocumentSize), bsonOutput, validator); BsonWriter bsonWriter = extraElements == null ? bsonBinaryWriter : new ElementExtendingBsonWriter(bsonBinaryWriter, extraElements); encoder.encode(bsonWriter, obj, encoderContext); } }
BsonValue toBson() { BsonDocument bson = new BsonDocument(); org.bson.BsonWriter writer = new BsonDocumentWriter(bson); // Bson doesn't allow to write directly scalars / primitives, they have to be embedded in a // document. writer.writeStartDocument(); writer.writeName("$"); encoder.encode(writer, value, EncoderContext.builder().build()); writer.writeEndDocument(); writer.flush(); return bson.get("$"); }
@Override public void encode(final BsonWriter writer, final DBObject value, final EncoderContext encoderContext) { encoder.encode(writer, value, encoderContext); }
/** * Gets a JSON representation of this document * * @param writerSettings the json writer settings to use when encoding * @param encoder the BasicDBObject codec instance to encode the document with * @return a JSON representation of this document * @throws org.bson.codecs.configuration.CodecConfigurationException if the registry does not contain a codec for the document values. */ public String toJson(final JsonWriterSettings writerSettings, final Encoder<BasicDBObject> encoder) { JsonWriter writer = new JsonWriter(new StringWriter(), writerSettings); encoder.encode(writer, this, EncoderContext.builder().isEncodingCollectibleDocument(true).build()); return writer.getWriter().toString(); }
@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()); } }
@SuppressWarnings("unchecked") private <T> void addDocument(final T obj, final Encoder<T> encoder, final EncoderContext encoderContext, final BsonOutput bsonOutput, final FieldNameValidator validator, final int maxDocumentSize, final List<BsonElement> extraElements) { BsonBinaryWriter bsonBinaryWriter = new BsonBinaryWriter(new BsonWriterSettings(), new BsonBinaryWriterSettings(maxDocumentSize), bsonOutput, validator); BsonWriter bsonWriter = extraElements == null ? bsonBinaryWriter : new ElementExtendingBsonWriter(bsonBinaryWriter, extraElements); encoder.encode(bsonWriter, obj, encoderContext); } }
@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(); }
BsonValue toBson() { BsonDocument bson = new BsonDocument(); org.bson.BsonWriter writer = new BsonDocumentWriter(bson); // Bson doesn't allow to write directly scalars / primitives, they have to be embedded in a // document. writer.writeStartDocument(); writer.writeName("$"); encoder.encode(writer, value, EncoderContext.builder().build()); writer.writeEndDocument(); writer.flush(); return bson.get("$"); }