当前位置: 首页>>代码示例>>Java>>正文


Java Codec.encode方法代码示例

本文整理汇总了Java中org.bson.codecs.Codec.encode方法的典型用法代码示例。如果您正苦于以下问题:Java Codec.encode方法的具体用法?Java Codec.encode怎么用?Java Codec.encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.bson.codecs.Codec的用法示例。


在下文中一共展示了Codec.encode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testResilience

import org.bson.codecs.Codec; //导入方法依赖的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));
}
 
开发者ID:axelspringer,项目名称:polymorphia,代码行数:26,代码来源:ListTypeCodecTest.java

示例2: reorderingDocumentTest

import org.bson.codecs.Codec; //导入方法依赖的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);

}
 
开发者ID:axelspringer,项目名称:polymorphia,代码行数:22,代码来源:CodecResolverTest.java

示例3: basicTest

import org.bson.codecs.Codec; //导入方法依赖的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);
}
 
开发者ID:axelspringer,项目名称:polymorphia,代码行数:18,代码来源:PreSaveHookTest.java

示例4: testEnums

import org.bson.codecs.Codec; //导入方法依赖的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));

}
 
开发者ID:axelspringer,项目名称:polymorphia,代码行数:23,代码来源:TestEnums.java

示例5: toBson

import org.bson.codecs.Codec; //导入方法依赖的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);
}
 
开发者ID:JPDSousa,项目名称:mongo-obj-framework,代码行数:19,代码来源:SmofParser.java

示例6: writeReadCompare

import org.bson.codecs.Codec; //导入方法依赖的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);
}
 
开发者ID:ralscha,项目名称:bsoncodec,代码行数:19,代码来源:AbstractTest.java

示例7: testCodec

import org.bson.codecs.Codec; //导入方法依赖的package包/类
@Test
public void testCodec() throws IOException {
    Codec<DefaultDomain> codec = DefaultDomainCodec.INSTANCE;
    DefaultDomain domain = new DefaultDomain();
    domain.addPlayer(UUID.randomUUID());
    domain.addGroup("test_group");

    DefaultDomain other;
    try (StringWriter sw = new StringWriter()) {
        codec.encode(new JsonWriter(sw), domain, EncoderContext.builder().build());
        other = codec.decode(new JsonReader(sw.toString()), DecoderContext.builder().build());
    }

    Assert.assertEquals(domain.getPlayers(), other.getPlayers());
    Assert.assertEquals(domain.getUniqueIds(), other.getUniqueIds());
    Assert.assertEquals(domain.getGroups(), other.getGroups());
}
 
开发者ID:maxikg,项目名称:mongowg,代码行数:18,代码来源:DefaultDomainCodecTest.java

示例8: encodeValue

import org.bson.codecs.Codec; //导入方法依赖的package包/类
private <T> void encodeValue(BsonOutput output, Codec<T> codec, T value) {
    try (BsonBinaryWriter writer = new BsonBinaryWriter(output)) {

        writer.writeStartDocument();
        writer.writeName("value");

        codec.encode(writer, value, EncoderContext.builder().build());

        writer.writeEndDocument();
        writer.close();
    }
}
 
开发者ID:cbartosiak,项目名称:bson-codecs-jsr310,代码行数:13,代码来源:CodecsTests.java

示例9: testDifferentTypes

import org.bson.codecs.Codec; //导入方法依赖的package包/类
@Test
public void testDifferentTypes() {
    Codec<Pojo> pojoCodec = codecRegistry.get(Pojo.class);

    CustomType<String> customTypeString = new CustomType("A custom string type");
    String[] strings = {"a", "nice", "list", "of", "strings"};
    customTypeString.addAll(Arrays.asList(strings));
    customTypeString.setInnerType(new InnerType<>("String value"));


    CustomType<Integer> customTypeInteger = new CustomType("A custom integer type");
    Integer[] integers = {1, 42, 66, 89};
    customTypeInteger.addAll(Arrays.asList(integers));
    customTypeInteger.setInnerType(new InnerType<>(11234567));


    String[] stringsForSet = {"Tee", "Brot", "Butter"};
    Pojo pojo = Pojo.builder()
            .customTypeString(customTypeString)
            .customTypeInteger(customTypeInteger)
            .name("aName")
            .strings(new HashSet<>(Arrays.asList(stringsForSet)))
            .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());

    System.out.println(decodedPojo);

    Assert.assertNotNull(decodedPojo);
    MatcherAssert.assertThat(decodedPojo.getCustomTypeString(), CoreMatchers.hasItems(strings));
    MatcherAssert.assertThat(decodedPojo.getCustomTypeInteger(), CoreMatchers.hasItems(integers));
    MatcherAssert.assertThat(decodedPojo.getStrings(), CoreMatchers.hasItems(stringsForSet));
}
 
开发者ID:axelspringer,项目名称:polymorphia,代码行数:39,代码来源:TypeCodecProviderTest.java

示例10: basicTest

import org.bson.codecs.Codec; //导入方法依赖的package包/类
@Test
public void basicTest() throws JSONException {
    BasePojo basePojo = new BasePojo();

    basePojo.encodeNullsFalseDecodeUndefined_CODEC = null; // encode to undefined
    basePojo.encodeNullsFalseDecodeUndefined_KEEP_POJO_DEFAULT = null; // encode with null value set
    basePojo.encodeNullsShouldDecodeToNull = null; // encode with null value set

    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());

    JSONAssert.assertEquals(stringWriter.toString(), "{\n" +
            "  \"encodeNullsTrue\" : null,\n" +
            "  \"encodeNullHandlingStrategy_CODEC\" : [],\n" +
            "  \"encodeNullsShouldDecodeToNull\" : null\n" +
            "}", true);


    Assert.assertNull(readBasePojo.encodeNullsFalse);
    Assert.assertNull(readBasePojo.aString);
    Assert.assertNull(readBasePojo.encodeNullsTrue);

    MatcherAssert.assertThat(readBasePojo.encodeNullHandlingStrategy_CODEC, empty());
    MatcherAssert.assertThat(readBasePojo.encodeNullsFalseDecodeUndefined_CODEC, empty());
    Assert.assertEquals(readBasePojo.encodeNullsFalseDecodeUndefined_KEEP_POJO_DEFAULT, Arrays.asList(new PojoProperty()));
    Assert.assertNull(readBasePojo.encodeNullsShouldDecodeToNull);
}
 
开发者ID:axelspringer,项目名称:polymorphia,代码行数:34,代码来源:NullHandlingTest.java

示例11: genericTest

import org.bson.codecs.Codec; //导入方法依赖的package包/类
@Test
public void genericTest() {
    IntegerType integerType = new IntegerType();
    integerType.anyType = INTEGER;

    Codec<IntegerType> integerTypeCodec = codecRegistry.get(IntegerType.class);
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter, new JsonWriterSettings(true));
    integerTypeCodec.encode(writer, integerType, EncoderContext.builder().build());
    LOGGER.info("The encoded json looks like: {}", stringWriter);
    IntegerType readIntegerType = integerTypeCodec.decode(new JsonReader(stringWriter.toString()), DecoderContext.builder().build());

    Assert.assertEquals(integerType, readIntegerType);

}
 
开发者ID:axelspringer,项目名称:polymorphia,代码行数:16,代码来源:BaseTest.java

示例12: serializeWithCodec

import org.bson.codecs.Codec; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
protected final <T> BsonValue serializeWithCodec(Codec<T> codec, Object value) {
	checkArgument(codec != null, "Cannot find a valid codec to serialize: " + value);
	final BsonDocument document = new BsonDocument();
	final String name = "result";
	final BsonDocumentWriter writer = new BsonDocumentWriter(document);
	writer.writeStartDocument();
	writer.writeName(name);
	codec.encode(writer, (T) value, EncoderContext.builder().build());
	writer.writeEndDocument();
	return document.get(name);
}
 
开发者ID:JPDSousa,项目名称:mongo-obj-framework,代码行数:13,代码来源:AbstractBsonParser.java

示例13: encodeObject

import org.bson.codecs.Codec; //导入方法依赖的package包/类
protected <V> void encodeObject(final BsonWriter writer, V value, final EncoderContext ctx, Class<V> objType) {
    Codec<V> codec = reg.get(objType);
    if (codec == null) {
        throw new BSONException("No codec for class " + objType);
    }
    codec.encode(writer, value, ctx);
}
 
开发者ID:egopulse,项目名称:java-code-gen,代码行数:8,代码来源:BeanCodec.java

示例14: encodeGenericList

import org.bson.codecs.Codec; //导入方法依赖的package包/类
protected <I> void encodeGenericList(final BsonWriter writer, List<I> value, final EncoderContext ctx, Class<I> itemType) {
    Codec<I> codec = reg.get(itemType);
    writer.writeStartArray();
    for (I item : value) {
        codec.encode(writer, item, ctx);
    }
    writer.writeEndArray();
}
 
开发者ID:egopulse,项目名称:java-code-gen,代码行数:9,代码来源:BeanCodec.java

示例15: encodeGenericSet

import org.bson.codecs.Codec; //导入方法依赖的package包/类
protected <I> void encodeGenericSet(final BsonWriter writer, Set<I> value, final EncoderContext ctx, Class<I> itemType) {
    Codec<I> codec = reg.get(itemType);
    writer.writeStartArray();
    for (I item : value) {
        codec.encode(writer, item, ctx);
    }
    writer.writeEndArray();
}
 
开发者ID:egopulse,项目名称:java-code-gen,代码行数:9,代码来源:BeanCodec.java


注:本文中的org.bson.codecs.Codec.encode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。