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;
}
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;
}
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"));
}
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");
}
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");
}
Aggregations