Search in sources :

Example 1 with JsonReader

use of org.bson.json.JsonReader in project camel by apache.

the class MongoDbBasicConverters method fromInputStreamToDocument.

@Converter
public static Document fromInputStreamToDocument(InputStream is, Exchange exchange) {
    Document answer = null;
    try {
        byte[] input = IOConverter.toBytes(is);
        if (isBson(input)) {
            JsonReader reader = new JsonReader(new String(input));
            DocumentCodec documentReader = new DocumentCodec();
            answer = documentReader.decode(reader, DecoderContext.builder().build());
        } else {
            answer = Document.parse(IOConverter.toString(input, exchange));
        }
    } catch (Exception e) {
        LOG.warn("String -> Document conversion selected, but the following exception occurred. Returning null.", e);
    } finally {
        // we need to make sure to close the input stream
        IOHelper.close(is, "InputStream", LOG);
    }
    return answer;
}
Also used : DocumentCodec(org.bson.codecs.DocumentCodec) JsonReader(org.bson.json.JsonReader) Document(org.bson.Document) FileNotFoundException(java.io.FileNotFoundException) IOConverter(org.apache.camel.converter.IOConverter) Converter(org.apache.camel.Converter)

Example 2 with JsonReader

use of org.bson.json.JsonReader 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)

Example 3 with JsonReader

use of org.bson.json.JsonReader in project core-ng-project by neowu.

the class EntityDecoderBuilderTest method decode.

@Test
void decode() {
    String entityJSON = ClasspathResources.text("mongo-test/entity.json");
    TestEntity entity = decoder.decode(new JsonReader(entityJSON));
    assertEquals(new ObjectId("5627b47d54b92d03adb9e9cf"), entity.id);
    assertEquals("string", entity.stringField);
    assertEquals(Long.valueOf(325), entity.longField);
    assertEquals(ZonedDateTime.of(LocalDateTime.of(2016, 9, 1, 11, 0, 0), ZoneId.of("America/New_York")).toInstant(), entity.zonedDateTimeField.toInstant());
    assertEquals(TestEntityChild.TestEnum.ITEM1, entity.child.enumField);
    assertEquals(2, entity.listField.size());
    assertEquals("V1", entity.listField.get(0));
    assertEquals("V2", entity.listField.get(1));
    assertNull(entity.nullChild);
    assertEquals("V1", entity.mapField.get("K1"));
    assertEquals("V2", entity.mapField.get("K2"));
}
Also used : ObjectId(org.bson.types.ObjectId) JsonReader(org.bson.json.JsonReader) Test(org.junit.jupiter.api.Test)

Example 4 with JsonReader

use of org.bson.json.JsonReader in project mongo-java-driver by mongodb.

the class StringCodecTest method testDecodeOnObjectIdWithObjectIdRep.

@Test
public void testDecodeOnObjectIdWithObjectIdRep() {
    BsonReader reader = new JsonReader("{'_id':  ObjectId('5f5a6cc03237b5e06d6b887b'), 'name': 'Brian'}");
    reader.readStartDocument();
    reader.readName();
    String stringId = child.decode(reader, decoderContext);
    assertEquals(stringId, "5f5a6cc03237b5e06d6b887b");
}
Also used : BsonReader(org.bson.BsonReader) JsonReader(org.bson.json.JsonReader) Test(org.junit.Test)

Example 5 with JsonReader

use of org.bson.json.JsonReader in project mongo-java-driver by mongodb.

the class StringCodecTest method testDecodeOnStringWithStringRep.

@Test
public void testDecodeOnStringWithStringRep() {
    BsonReader reader = new JsonReader("{'name': 'Brian'");
    reader.readStartDocument();
    reader.readName();
    assertEquals(parent.decode(reader, decoderContext), "Brian");
}
Also used : BsonReader(org.bson.BsonReader) JsonReader(org.bson.json.JsonReader) Test(org.junit.Test)

Aggregations

JsonReader (org.bson.json.JsonReader)18 BsonDocument (org.bson.BsonDocument)5 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)5 BsonReader (org.bson.BsonReader)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 MongoDatabase (com.mongodb.client.MongoDatabase)2 InsertManyOptions (com.mongodb.client.model.InsertManyOptions)2 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Converter (org.apache.camel.Converter)2 IOConverter (org.apache.camel.converter.IOConverter)2 BsonArray (org.bson.BsonArray)2 BsonBinaryWriter (org.bson.BsonBinaryWriter)2 ByteBufNIO (org.bson.ByteBufNIO)2 Document (org.bson.Document)2 RawBsonDocument (org.bson.RawBsonDocument)2 RawBsonDocumentCodec (org.bson.codecs.RawBsonDocumentCodec)2 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)2