本文整理汇总了Java中org.bson.codecs.EncoderContext类的典型用法代码示例。如果您正苦于以下问题:Java EncoderContext类的具体用法?Java EncoderContext怎么用?Java EncoderContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
EncoderContext类属于org.bson.codecs包,在下文中一共展示了EncoderContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testResilience
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
/**
* Testing if List<String> can be decoded into a
*/
@Test
public void testResilience() {
Codec<EncodingPojo> encodingPojoCodec = codecRegistry.get(EncodingPojo.class);
Codec<DecodingPojo> decodingPojoCodec = codecRegistry.get(DecodingPojo.class);
EncodingPojo encodingPojo = new EncodingPojo();
encodingPojo.someList = new ArrayList<>();
encodingPojo.someList.add("string1");
encodingPojo.someList.add("string2");
encodingPojo.someList.add("string3");
StringWriter stringWriter = new StringWriter();
JsonWriter writer = new JsonWriter(stringWriter, new JsonWriterSettings(true));
encodingPojoCodec.encode(writer, encodingPojo, EncoderContext.builder().build());
System.out.println(stringWriter.toString());
DecodingPojo decodingPojo = decodingPojoCodec.decode(new JsonReader(stringWriter.toString()), DecoderContext.builder().build());
Assert.assertNotNull(decodingPojo.someList);
assertThat(decodingPojo.someList, instanceOf(ListOfStrings.class));
}
示例2: encoderFor
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
/**
* Creates "writer" which can serialize existing immutable instance into <a href="http://bsonspec.org/">BSON format</a>
* consumed by mongo server.
*/
public static <T> Encoder<T> encoderFor(final Class<T> type, final TypeAdapter<T> adapter) {
checkNotNull(type, "type");
checkNotNull(adapter, "adapter");
return new Encoder<T>() {
@Override
public void encode(org.bson.BsonWriter writer, T value, EncoderContext encoderContext) {
try {
adapter.write(new org.immutables.mongo.repository.internal.BsonWriter(writer), value);
} catch (IOException e) {
throw new RuntimeException(String.format("Couldn't write value of class %s: %s", type.getName(), value),e);
}
}
@Override
public Class<T> getEncoderClass() {
return type;
}
};
}
示例3: encode
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
@Override
public void encode(BsonWriter writer, SyncError error, EncoderContext encoderContext) {
Document document= new Document();
if(error.getMessage()!=null && !error.getMessage().isEmpty()){
document.append(SyncAttrs.ERROR_MESSAGE, error.getMessage());
}
String fullStackTrace = ExceptionUtils.getStackTrace(error);
if(fullStackTrace!=null && !fullStackTrace.isEmpty()){
document.append(SyncAttrs.TRACE, fullStackTrace);
}
if(error.getThreadName()!=null && !error.getThreadName().isEmpty()){
document.append(SyncAttrs.THREAD_NAME, error.getThreadName());
}
documentCodec.encode(writer, document, encoderContext);
}
示例4: encode
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
@Override
public void encode(BsonWriter writer, SyncUser user, EncoderContext encoderContext) {
logger.info("Start of Encode method");
Document document = new Document();
if (user.getUserid()!=null && !user.getUserid().isEmpty()){
document.append(String.valueOf(UserDetailAttributes._id), user.getUserid());
}
if(user.getSourceDbMap()!=null && !user.getSourceDbMap().isEmpty()){
document.append(String.valueOf(UserDetailAttributes.sourceDbMap), user.getSourceDbMap());
}
if(user.getTargetDbMap()!=null && !user.getTargetDbMap().isEmpty()){
document.append(String.valueOf(UserDetailAttributes.targetDbMap), user.getTargetDbMap());
}
if(user.getUserRoles()!=null && !user.getUserRoles().isEmpty()){
document.append("roles", user.getUserRoles());
}
documentCodec.encode(writer, document, encoderContext);
logger.info("Encoder completed. Document formed is \n"+document);
}
示例5: encode
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
@Override
public void encode(BsonWriter writer, SyncUserSession session, EncoderContext encoderContext) {
Document document = new Document();
if(session.getSessionId()!=null && !session.getSessionId().isEmpty() ){
document.append(String.valueOf(SessionAttributes._id), session.getSessionId());
}
if(session.getUserid()!=null && !session.getUserid().isEmpty()){
document.append(String.valueOf(SessionAttributes.userid), session.getUserid());
}
if(session.getLoginTime()!=null){
document.append(String.valueOf(SessionAttributes.loginTime), session.getLoginTime());
}
if(session.getClientIPAddress()!=null && !session.getClientIPAddress().isEmpty()){
document.append(String.valueOf(SessionAttributes.clientIPAddress), session.getClientIPAddress());
}
if(session.getClientHostName()!=null && !session.getClientHostName().isEmpty()){
document.append(String.valueOf(SessionAttributes.clientHostName), session.getClientHostName());
}
if(session.getClientAgent()!=null && !session.getClientAgent().isEmpty()){
document.append(String.valueOf(SessionAttributes.clientAgent), session.getClientAgent());
}
documentCodec.encode(writer, document, encoderContext);
}
示例6: encode
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
@Override
public void encode(BsonWriter writer, SyncMarker marker, EncoderContext encoderContext) {
Document document = new Document();
if(marker.getStartTime()!=null){
document.append(SyncAttrs.START_TIME, marker.getStartTime());
}
document.append(SyncAttrs.ROWS_READ, marker.getRowsRead());
if(marker.isAllRowsFetchedFromDb()){
document.append(SyncAttrs.ALL_ROWS_FETCHED, marker.isAllRowsFetchedFromDb());
}
document.append(SyncAttrs.ROWS_DUMPED, marker.getRowsDumped());
document.append(SyncAttrs.TOTAL_ROWS, marker.getTotalRows());
if(marker.getEndTime()!=null){
document.append(SyncAttrs.END_TIME, marker.getEndTime());
}
documentCodec.encode(writer, document, encoderContext);
}
示例7: encode
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
@Override
public void encode(BsonWriter writer, T value, EncoderContext encoderContext) {
if (value == null) {
writer.writeNull();
}
else {
writer.writeStartDocument();
PolymorphicCodec<T> codecForValue = getCodecForClass(value.getClass());
if (codecForValue != null) {
writer.writeName(discriminatorKeys.get(codecForValue.getEncoderClass()));
writer.writeString(mainDiscriminators.get(codecForValue.getEncoderClass()));
codecForValue.encodeFields(writer, value, encoderContext);
} else {
LOGGER.warn("The value to be encoded has the wrong type {}. This codec can only handle {}", value.getClass(), discriminatorToCodec);
}
writer.writeEndDocument();
}
}
示例8: encodeFields
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
@Override
public void encodeFields(BsonWriter writer, T instance, EncoderContext encoderContext) {
// first persist meta document
documentMetaCodec.encodeFields(writer, instance.getMeta(), encoderContext);
// only persist data property if and only if properties exist
Map<String, MappedField> persistenceFields = getPersistenceFields();
if (!persistenceFields.isEmpty()) {
writer.writeName("data");
writer.writeStartDocument();
for (MappedField persistenceField : persistenceFields.values()) {
if (!"meta".equals(persistenceField.getMappedFieldName())) {
persistenceField.encode(writer, instance, encoderContext);
}
}
writer.writeEndDocument();
}
}
示例9: reorderingDocumentTest
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
@Test
public void reorderingDocumentTest() {
Document document = new Document();
document.stringProperty = "a nice string";
document.datProperty = new Date();
MongoCollection<Document> documentMongoCollection = mongoClient.getDatabase("test").getCollection("documents").withDocumentClass(Document.class);
documentMongoCollection.insertOne(document);
Document readDocument = documentMongoCollection.find(Filters.eq("_id", document.getMeta().getId())).first();
Assert.assertEquals(document, readDocument);
Codec<Document> documentCodec = codecRegistry.get(Document.class);
StringWriter stringWriter = new StringWriter();
JsonWriter writer = new JsonWriter(stringWriter, new JsonWriterSettings(true));
documentCodec.encode(writer, document, EncoderContext.builder().build());
LOGGER.info("The encoded json looks like: {}", stringWriter);
}
示例10: basicTest
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
@Test
public void basicTest() {
BasePojo basePojo = new BasePojo();
basePojo.aString = STRING;
Codec<BasePojo> primitivePojoCodec = codecRegistry.get(BasePojo.class);
StringWriter stringWriter = new StringWriter();
JsonWriter writer = new JsonWriter(stringWriter, new JsonWriterSettings(true));
primitivePojoCodec.encode(writer, basePojo, EncoderContext.builder().build());
LOGGER.info("The encoded json looks like: {}", stringWriter);
BasePojo readBasePojo = primitivePojoCodec.decode(new JsonReader(stringWriter.toString()), DecoderContext.builder().build());
// assert that the modified version was actually written to the database
Assert.assertEquals(basePojo, readBasePojo);
Assert.assertEquals(MODIFIED_STRING, readBasePojo.aString);
}
示例11: testEnums
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
@Test
public void testEnums() {
Codec<Pojo> pojoCodec = codecRegistry.get(Pojo.class);
LovelyDisplayable lovelyDisplayable = LovelyDisplayable.builder().identityProperty("foo").build();
Pojo pojo = Pojo.builder()
.simpleEnumProperty(EnumA.TYPE1)
.displayable(Arrays.asList(EnumB.TYPE1, EnumA.TYPE1, EnumA.TYPE3, lovelyDisplayable))
.build();
StringWriter stringWriter = new StringWriter();
JsonWriter writer = new JsonWriter(stringWriter, new JsonWriterSettings(true));
pojoCodec.encode(writer, pojo, EncoderContext.builder().build());
System.out.println(stringWriter.toString());
Pojo decodedPojo = pojoCodec.decode(new JsonReader(stringWriter.toString()), DecoderContext.builder().build());
MatcherAssert.assertThat(decodedPojo.getDisplayable(),
IsIterableContainingInOrder.contains(EnumB.TYPE1, EnumA.TYPE1, EnumA.TYPE3, lovelyDisplayable));
}
示例12: encode
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
@Override
public void encode(BsonWriter writer, Participant value, EncoderContext encoderContext) {
writer.writeStartDocument();
writer.writeInt64("recruitmentNumber", value.getRecruitmentNumber());
writer.writeString("name", value.getName());
writer.writeString("sex", value.getSex().toString());
writer.writeStartDocument("birthdate");
writer.writeString("objectType", value.getBirthdate().getObjectType());
writer.writeString("value", value.getBirthdate().getFormattedValue());
writer.writeEndDocument();
writer.writeStartDocument("fieldCenter");
writer.writeString("acronym", value.getFieldCenter().getAcronym());
writer.writeEndDocument();
writer.writeEndDocument();
}
示例13: toBson
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public <T> BsonValue toBson(Object value, Class<T> clazz) {
if(value == null) {
return new BsonNull();
}
if(value instanceof BsonValue) {
return (BsonValue) value;
}
final Codec<T> codec = registry.get(clazz);
final String key = "value";
final BsonDocument document = new BsonDocument();
final BsonWriter writer = new BsonDocumentWriter(document);
writer.writeStartDocument();
writer.writeName(key);
codec.encode(writer, (T) value, EncoderContext.builder().build());
writer.writeEndDocument();
return document.get(key);
}
示例14: BsonCodecGenerator
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
public BsonCodecGenerator(Models models) {
this.models = models;
Elements elems = models.getElemsUtil();
// Reusable things for multiple types
objectIdTypeMirror = elems.getTypeElement(ObjectId.class.getCanonicalName()).asType();
bsonBinaryTypeMirror = elems.getTypeElement(BsonBinary.class.getCanonicalName()).asType();
dateTypeMirror = elems.getTypeElement(Date.class.getCanonicalName()).asType();
instantTypeMirror = elems.getTypeElement(Instant.class.getCanonicalName()).asType();
stringTypeMirror = elems.getTypeElement(String.class.getCanonicalName()).asType();
longTypeMirror = elems.getTypeElement(Long.class.getCanonicalName()).asType();
classClassName = ClassName.get(Class.class);
bsonWriterParamSpec = ParameterSpec.builder(BsonWriter.class, "writer").addModifiers(Modifier.FINAL).build();
bsonReaderParamSpec = ParameterSpec.builder(BsonReader.class, "reader").addModifiers(Modifier.FINAL).build();
encoderCtxParamSpec = ParameterSpec.builder(EncoderContext.class, "ctx").addModifiers(Modifier.FINAL).build();
decoderCtxParamSpec = ParameterSpec.builder(DecoderContext.class, "ctx").addModifiers(Modifier.FINAL).build();
}
示例15: writeReadCompare
import org.bson.codecs.EncoderContext; //导入依赖的package包/类
@SuppressWarnings("resource")
protected <T> void writeReadCompare(T source, Codec<T> codec) {
BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
BsonBinaryWriter writer = new BsonBinaryWriter(bsonOutput);
writer.writeStartDocument();
writer.writeName("name");
codec.encode(writer, source, EncoderContext.builder().build());
writer.writeEndDocument();
writer.close();
BsonBinaryReader reader = new BsonBinaryReader(
ByteBuffer.wrap(bsonOutput.toByteArray()));
reader.readStartDocument();
assertThat(reader.readName()).isEqualTo("name");
T readNow = codec.decode(reader, DecoderContext.builder().build());
assertThat(readNow).isEqualTo(source);
}