private CodecSerializer(Codec<T> codec) { super(codec.getEncoderClass()); this.codec = codec; }
CommandResultCallback(final SingleResultCallback<T> callback, final Decoder<T> decoder, final long requestId, final ServerAddress serverAddress) { super(new BsonDocumentCodec(), requestId, serverAddress); this.callback = callback; this.decoder = decoder; }
/** * Construct a new instance with a default {@code BsonTypeClassMap} and the given {@code Transformer}. The transformer is used by the * IterableCodec as a last step when decoding values. * * @param valueTransformer the value transformer for decoded values */ public IterableCodecProvider(final Transformer valueTransformer) { this(new BsonTypeClassMap(), valueTransformer); }
/** * 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 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()); }
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 @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); } }
/** * 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 @SuppressWarnings("unchecked") public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) { if (clazz == UUID.class) { return (Codec<T>) (new UuidCodec(uuidRepresentation)); } return null; } }
/** * Creates a new instance initialised with the given codec registry. * * @param codecRegistry the {@code CodecRegistry} to use to look up the codecs for encoding and decoding to/from BSON */ public BsonDocumentCodec(final CodecRegistry codecRegistry) { if (codecRegistry == null) { throw new IllegalArgumentException("Codec registry can not be null"); } this.codecRegistry = codecRegistry; this.bsonTypeCodecMap = new BsonTypeCodecMap(getBsonTypeClassMap(), codecRegistry); }
/** * Build an instance of {@code DecoderContext}. * @return the decoder context */ public DecoderContext build() { return new DecoderContext(this); } }
/** * Get the {@code BsonValue} subclass associated with the given {@code BsonType}. * @param bsonType the BsonType * @return the class associated with the given type */ @SuppressWarnings("unchecked") public static Class<? extends BsonValue> getClassForBsonType(final BsonType bsonType) { return (Class<? extends BsonValue>) DEFAULT_BSON_TYPE_CLASS_MAP.get(bsonType); }
/** * Create a builder. * * @return the builder */ public static Builder builder() { return new Builder(); }
static BsonDocument toBson(JsonObject gson) throws IOException { return new BsonDocumentCodec().decode(asBsonReader(gson), DecoderContext.builder().build()); }
/** * Construct a new instance with a default {@code BsonTypeClassMap} and the given {@code Transformer}. The transformer is used by the * DocumentCodec as a last step when decoding values. * * @param valueTransformer the value transformer for decoded values * @see org.bson.codecs.DocumentCodec#DocumentCodec(org.bson.codecs.configuration.CodecRegistry, BsonTypeClassMap, org.bson.Transformer) */ public DocumentCodecProvider(final Transformer valueTransformer) { this(new BsonTypeClassMap(), valueTransformer); }
private UUIDDeserializer(UuidRepresentation uuidRepresentation) { super(UUID.class); uuidCodec = new UuidCodec(uuidRepresentation); }