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


Java JsonWriter类代码示例

本文整理汇总了Java中org.bson.json.JsonWriter的典型用法代码示例。如果您正苦于以下问题:Java JsonWriter类的具体用法?Java JsonWriter怎么用?Java JsonWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: testResilience

import org.bson.json.JsonWriter; //导入依赖的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.json.JsonWriter; //导入依赖的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.json.JsonWriter; //导入依赖的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.json.JsonWriter; //导入依赖的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: testCodec

import org.bson.json.JsonWriter; //导入依赖的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

示例6: shouldEncodeDocument

import org.bson.json.JsonWriter; //导入依赖的package包/类
private void shouldEncodeDocument()
    throws UnsupportedEncodingException, IOException, JSONException {
  Assume.assumeThat(javaObject, CoreMatchers.instanceOf(Foo.class));
  // given
  final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  final BsonWriter bsonWriter = new JsonWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  final EncoderContext context =
      EncoderContext.builder().isEncodingCollectibleDocument(true).build();
  // when
  new DocumentCodec<Foo>(Foo.class, DEFAULT_CODEC_REGISTRY).encode(bsonWriter, (Foo) javaObject,
      context);
  // then
  final String actualJson = IOUtils.toString(outputStream.toByteArray(), "UTF-8");
  LOGGER.debug("Comparing \nexpected: {} vs \nactual: {}", jsonValue, actualJson);
  JSONAssert.assertEquals(jsonValue, actualJson, true);
}
 
开发者ID:lambdamatic,项目名称:lambdamatic-project,代码行数:17,代码来源:DocumentCodecTest.java

示例7: shouldEncodeFilterExpression

import org.bson.json.JsonWriter; //导入依赖的package包/类
private void shouldEncodeFilterExpression(final String stringField, final int primitiveIntField,
    final EnumFoo enumFoo) throws UnsupportedEncodingException, IOException, JSONException {
  // given
  final SerializablePredicate<QFoo> expr = ((QFoo foo) -> foo.stringField.equals(stringField)
      || foo.primitiveIntField.equals(primitiveIntField) || foo.enumFoo.equals(enumFoo));
  final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  final BsonWriter bsonWriter = new JsonWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  final EncoderContext context =
      EncoderContext.builder().isEncodingCollectibleDocument(true).build();
  // when
  new FilterExpressionCodec().encode(bsonWriter, expr, context);
  // then
  final String actual = IOUtils.toString(outputStream.toByteArray(), "UTF-8");
  final String expected = "{$or: [{primitiveIntField: " + primitiveIntField + "}, {enumFoo: '"
      + enumFoo + "'}, {stringField: '" + stringField + "'}]}";
  LOGGER.debug("Comparing \n{} vs \n{}", expected, actual);
  JSONAssert.assertEquals(expected, actual, false);
}
 
开发者ID:lambdamatic,项目名称:lambdamatic-project,代码行数:19,代码来源:FilterExpressionCacheTest.java

示例8: basicDeEncoding

import org.bson.json.JsonWriter; //导入依赖的package包/类
/**
 * Tests if de/encoding a simple Entity with only primitive data types work
 */
@Test
public void basicDeEncoding()
{
    EntityCodec codec = new EntityCodec<>( db, EntityFactory.getProperties( PrimitiveEntity.class ) );

    PrimitiveEntity pe = instantiate( PrimitiveEntity.class );
    pe.setString( "value" );
    pe.setInteger( 123 );

    StringWriter swriter = new StringWriter();
    JsonWriter jwriter = new JsonWriter( swriter );

    codec.encode( jwriter, pe, null );

    assertJson( sameJSONAs( "{ \"Integer\" : 123, \"string\" : \"value\" }" ), swriter );

    JsonReader jreader = new JsonReader( swriter.toString() );

    PrimitiveEntity read = decode( codec, jreader, PrimitiveEntity.class );

    assertEquals( pe.getString(), read.getString() );
    assertEquals( pe.getInteger(), read.getInteger() );
}
 
开发者ID:cherimojava,项目名称:cherimodata,代码行数:27,代码来源:_DeEncoding.java

示例9: collectionDeEncoding

import org.bson.json.JsonWriter; //导入依赖的package包/类
@Test
public void collectionDeEncoding()
{
    EntityCodec codec = new EntityCodec<>( db, EntityFactory.getProperties( CollectionEntity.class ) );

    CollectionEntity ce = instantiate( CollectionEntity.class );
    ce.setArrayStrings( new String[] { "one", "two" } );
    ce.setStrings( Lists.newArrayList( "three", "four" ) );

    StringWriter swriter = new StringWriter();
    JsonWriter jwriter = new JsonWriter( swriter );

    codec.encode( jwriter, ce, null );
    assertJson( sameJSONAs( "{ \"arrayStrings\": [\"one\",\"two\"],\"strings\":[\"three\",\"four\"] }" ), swriter );

    JsonReader jreader = new JsonReader( swriter.toString() );
    CollectionEntity ceRead = decode( codec, jreader, CollectionEntity.class );
    assertJson( sameJSONAs( "{ \"arrayStrings\": [\"one\",\"two\"],\"strings\":[\"three\",\"four\"] }" ), ceRead );
}
 
开发者ID:cherimojava,项目名称:cherimodata,代码行数:20,代码来源:_DeEncoding.java

示例10: collectionDeEncodingDB

import org.bson.json.JsonWriter; //导入依赖的package包/类
@Test
public void collectionDeEncodingDB()
{
    EntityCodec codec = new EntityCodec<>( db, EntityFactory.getProperties( Listed.class ) );
    Listed listed = factory.create( Listed.class );
    listed.setList( Lists.newArrayList( factory.create( PrimitiveEntity.class ).setString( "nested" ) ) );
    StringWriter swriter = new StringWriter();
    JsonWriter jwriter = new JsonWriter( swriter );

    codec.encode( jwriter, listed, null );
    assertJson( sameJSONAs( "{ \"list\": [{\"string\": \"nested\"}]}" ), swriter );
    JsonReader jsonReader = new JsonReader( swriter.toString() );
    Listed read = decode( codec, jsonReader, Listed.class );
    assertNotNull( read.getList() );
    assertEquals( listed.getList().get( 0 ), read.getList().get( 0 ) );

    listed.save();
    Listed r = factory.load( Listed.class, listed.get( ID ) );
    assertEquals( listed, r );
}
 
开发者ID:cherimojava,项目名称:cherimodata,代码行数:21,代码来源:_DeEncoding.java

示例11: enumDeEncoding

import org.bson.json.JsonWriter; //导入依赖的package包/类
@Test
public void enumDeEncoding()
{
    EntityCodec codec = new EntityCodec<>( db, EntityFactory.getProperties( EnumEntity.class ) );
    EnumEntity ee = instantiate( EnumEntity.class );
    ee.setCategory( EnumEntity.Category.Misc );

    StringWriter swriter = new StringWriter();
    JsonWriter jwriter = new JsonWriter( swriter );

    codec.encode( jwriter, ee, null );
    assertJson( sameJSONAs( "{ \"category\": \"Misc\" }" ), swriter );
    JsonReader jreader = new JsonReader( swriter.toString() );
    EnumEntity eeRead = decode( codec, jreader, EnumEntity.class );
    assertEquals( ee, eeRead );
}
 
开发者ID:cherimojava,项目名称:cherimodata,代码行数:17,代码来源:_DeEncoding.java

示例12: testDifferentTypes

import org.bson.json.JsonWriter; //导入依赖的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

示例13: basicTest

import org.bson.json.JsonWriter; //导入依赖的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

示例14: genericTest

import org.bson.json.JsonWriter; //导入依赖的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

示例15: writeAsJsonStr

import org.bson.json.JsonWriter; //导入依赖的package包/类
@Override
public String writeAsJsonStr(Object object) {
    if (object == null) {
        return null;
    }
    StringWriter writer = new StringWriter();
    JsonWriterSettings jsonWriterSettings = bsonMapperConfig.getJsonWriterSettings();
    BsonWriter bsonWriter = new JsonWriter(writer, jsonWriterSettings == null ? new JsonWriterSettings() : jsonWriterSettings);
    BsonValueConverterRepertory.getBsonDocumentConverter().encode(bsonWriter, object);
    return writer.toString();
}
 
开发者ID:welkinbai,项目名称:BsonMapper,代码行数:12,代码来源:DefaultBsonMapper.java


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