Search in sources :

Example 1 with BsonArrayCodec

use of org.bson.codecs.BsonArrayCodec in project camel by apache.

the class MongoDbBasicConverters method fromStringToList.

@Converter
public static List<Bson> fromStringToList(String value) {
    final CodecRegistry codecRegistry = CodecRegistries.fromProviders(Arrays.asList(new ValueCodecProvider(), new BsonValueCodecProvider(), new DocumentCodecProvider()));
    JsonReader reader = new JsonReader(value);
    BsonArrayCodec arrayReader = new BsonArrayCodec(codecRegistry);
    BsonArray docArray = arrayReader.decode(reader, DecoderContext.builder().build());
    List<Bson> answer = new ArrayList<>(docArray.size());
    for (BsonValue doc : docArray) {
        answer.add(doc.asDocument());
    }
    return answer;
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) BsonArray(org.bson.BsonArray) BsonArrayCodec(org.bson.codecs.BsonArrayCodec) ArrayList(java.util.ArrayList) JsonReader(org.bson.json.JsonReader) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Bson(org.bson.conversions.Bson) BsonValue(org.bson.BsonValue) IOConverter(org.apache.camel.converter.IOConverter) Converter(org.apache.camel.Converter)

Aggregations

ArrayList (java.util.ArrayList)1 Converter (org.apache.camel.Converter)1 IOConverter (org.apache.camel.converter.IOConverter)1 BsonArray (org.bson.BsonArray)1 BsonValue (org.bson.BsonValue)1 BsonArrayCodec (org.bson.codecs.BsonArrayCodec)1 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)1 DocumentCodecProvider (org.bson.codecs.DocumentCodecProvider)1 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)1 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)1 Bson (org.bson.conversions.Bson)1 JsonReader (org.bson.json.JsonReader)1