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


Java BsonDocument类代码示例

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


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

示例1: testPatchAppliedCleanly

import org.bson.BsonDocument; //导入依赖的package包/类
@Test
    public void testPatchAppliedCleanly() throws Exception {
        for (int i = 0; i < jsonNode.size(); i++) {
        	BsonDocument node = jsonNode.get(i).asDocument();
        	
            BsonValue first = node.get("first");
            BsonValue second = node.get("second");
            BsonArray patch = node.getArray("patch");
            String message = node.containsKey("message") ? node.getString("message").getValue() : "";

//            System.out.println("Test # " + i);
//            System.out.println(first);
//            System.out.println(second);
//            System.out.println(patch);

            BsonValue secondPrime = BsonPatch.apply(patch, first);
//            System.out.println(secondPrime);
            Assert.assertThat(message, secondPrime, equalTo(second));
        }

    }
 
开发者ID:eBay,项目名称:bsonpatch,代码行数:22,代码来源:JsonDiffTest2.java

示例2: testRenderedOperationsExceptMoveAndCopy

import org.bson.BsonDocument; //导入依赖的package包/类
@Test
    public void testRenderedOperationsExceptMoveAndCopy() throws Exception {
    	BsonDocument source = new BsonDocument();
    	source.put("age", new BsonInt32(10));
    	BsonDocument target = new BsonDocument();
    	target.put("height", new BsonInt32(10));

        EnumSet<DiffFlags> flags = DiffFlags.dontNormalizeOpIntoMoveAndCopy().clone(); //only have ADD, REMOVE, REPLACE, Don't normalize operations into MOVE & COPY

        BsonArray diff = BsonDiff.asBson(source, target, flags);

//        System.out.println(source);
//        System.out.println(target);
//        System.out.println(diff);

        for (BsonValue d : diff) {
            Assert.assertNotEquals(Operation.MOVE.rfcName(), d.asDocument().getString("op").getValue());
            Assert.assertNotEquals(Operation.COPY.rfcName(), d.asDocument().getString("op").getValue());
        }

        BsonValue targetPrime = BsonPatch.apply(diff, source);
//        System.out.println(targetPrime);
        Assert.assertTrue(target.equals(targetPrime));


    }
 
开发者ID:eBay,项目名称:bsonpatch,代码行数:27,代码来源:JsonDiffTest.java

示例3: convertBinaryUUID

import org.bson.BsonDocument; //导入依赖的package包/类
/**
 * Converts a binary uuid to a standard uuid
 *
 * @param json The json string (should contain "$binary" and "$type" or something like that)
 * @return The uuid
 */
private UUID convertBinaryUUID(String key, String json) {
    BsonDocument document = BsonDocument.parse(json);

    BsonValue value = document.get(key);
    if(!(value instanceof BsonBinary)) {
        return null;
    }
    byte[] bytes = ((BsonBinary) value).getData();
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    long l1 = bb.getLong();
    long l2 = bb.getLong();

    return new UUID(l1, l2);
}
 
开发者ID:Superioz,项目名称:MooProject,代码行数:22,代码来源:DbFilter.java

示例4: getBsonValueList

import org.bson.BsonDocument; //导入依赖的package包/类
private ArrayList<BsonValue> getBsonValueList(Field field, Collection values, BsonMapperConfig bsonMapperConfig, Class<?> componentType) {
    ArrayList<BsonValue> arrayList = new ArrayList<BsonValue>();
    for (Object o : values) {
        if (o == null) {
            continue;
        }
        Class<?> oClazz = o.getClass();
        if (Utils.isArrayType(oClazz)) {
            BsonArray innerBsonArray = new BsonArray();
            encode(innerBsonArray, field, o, bsonMapperConfig);
            arrayList.add(innerBsonArray);
        }
        if (componentType.isInstance(o)) {
            if (BsonValueConverterRepertory.isCanConverterValueType(componentType)) {
                arrayList.add(BsonValueConverterRepertory.getValueConverterByClazz(componentType).encode(o));
            } else {
                BsonDocument arrayEle = new BsonDocument();
                BsonValueConverterRepertory.getBsonDocumentConverter().encode(arrayEle, o, bsonMapperConfig);
                arrayList.add(arrayEle);
            }
        } else {
            throw new BsonMapperConverterException(String.format("array field has element which has different type with declaring componentType.field name: %s", field.getName()));
        }
    }
    return arrayList;
}
 
开发者ID:welkinbai,项目名称:BsonMapper,代码行数:27,代码来源:BsonArrayConverter.java

示例5: readFrom

import org.bson.BsonDocument; //导入依赖的package包/类
@Test
public void readFrom() throws Exception {
    BsonDocument bsonDocument1 = getBsonDocument();
    BsonMapper bsonMapper = DefaultBsonMapper.defaultBsonMapper();
    BsonTest bsonTest = bsonMapper.readFrom(bsonDocument1, BsonTest.class);
    System.out.println(bsonTest.getTestDouble());
    System.out.println(bsonTest.getTestString());
    System.out.println(bsonTest.getTestArray());
    System.out.println(Arrays.toString(bsonTest.getTestBinary().getData()));
    System.out.println(bsonTest.getTestObjectId());
    System.out.println(bsonTest.getTestStringObjectId());
    System.out.println(bsonTest.isTestBoolean());
    System.out.println(bsonTest.getTestDate());
    System.out.println(bsonTest.getTestNull());
    System.out.println(bsonTest.getTestInt());
    System.out.println(bsonTest.getTestLong());
    System.out.println(bsonTest);
}
 
开发者ID:welkinbai,项目名称:BsonMapper,代码行数:19,代码来源:DefaultBsonMapperTest.java

示例6: getBsonDocument

import org.bson.BsonDocument; //导入依赖的package包/类
public static BsonDocument getBsonDocument() {
    BsonDocument bsonObj = new BsonDocument().append("testDouble", new BsonDouble(20.777));
    List<BsonDocument> list = new ArrayList<BsonDocument>();
    list.add(bsonObj);
    list.add(bsonObj);
    byte[] bytes = new byte[3];
    bytes[0] = 3;
    bytes[1] = 2;
    bytes[2] = 1;
    BsonDocument bsonDocument = new BsonDocument().append("testDouble", new BsonDouble(20.99))
            .append("testString", new BsonString("testStringV"))
            .append("testArray", new BsonArray(list));
    return new BsonDocument().append("testDouble", new BsonDouble(20.99))
            .append("testString", new BsonString("testStringV"))
            .append("testArray", new BsonArray(list))
            .append("bson_test", bsonDocument)
            .append("testBinary", new BsonBinary(bytes))
            .append("testBsonUndefined", new BsonUndefined())
            .append("testObjectId", new BsonObjectId())
            .append("testStringObjectId", new BsonObjectId())
            .append("testBoolean", new BsonBoolean(true))
            .append("testDate", new BsonDateTime(time))
            .append("testNull", new BsonNull())
            .append("testInt", new BsonInt32(233))
            .append("testLong", new BsonInt64(233332));
}
 
开发者ID:welkinbai,项目名称:BsonMapper,代码行数:27,代码来源:TestUtil.java

示例7: testBsonDocumentDeSerialize

import org.bson.BsonDocument; //导入依赖的package包/类
@Test
public void testBsonDocumentDeSerialize() {
	BsonDocument document = new BsonDocument().append("a", new BsonString("MongoDB"))
               .append("b", new BsonArray(Arrays.asList(new BsonInt32(1), new BsonInt32(2))))
               .append("c", new BsonBoolean(true))
               .append("d", new BsonDateTime(0));

	String json = oson.useAttribute(false).setValueOnly(true).serialize(document);

	String expected = "{\"a\":\"MongoDB\",\"b\":[1,2],\"c\":true,\"d\":0}";

	assertEquals(expected, json);

	BsonDocument document2 = oson.deserialize(json, BsonDocument.class);

	assertEquals(expected, oson.serialize(document2));
}
 
开发者ID:osonus,项目名称:oson,代码行数:18,代码来源:DocumentTest.java

示例8: testRecursiveDocuments

import org.bson.BsonDocument; //导入依赖的package包/类
@Test
public void testRecursiveDocuments() throws IOException {
  BsonDocument topDoc = new BsonDocument();
  final int count = 3;
  for (int i = 0; i < count; ++i) {
    BsonDocument bsonDoc = new BsonDocument();
    BsonWriter bw = new BsonDocumentWriter(bsonDoc);
    bw.writeStartDocument();
    bw.writeName("k1" + i);
    bw.writeString("drillMongo1" + i);
    bw.writeName("k2" + i);
    bw.writeString("drillMongo2" + i);
    bw.writeEndDocument();
    bw.flush();
    topDoc.append("doc" + i, bsonDoc);
  }
  writer.reset();
  bsonReader.write(writer, new BsonDocumentReader(topDoc));
  SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
  for (int i = 0; i < count; ++i) {
    SingleMapReaderImpl reader = (SingleMapReaderImpl) mapReader.reader("doc" + i);
    assertEquals("drillMongo1" + i, reader.reader("k1" + i).readText().toString());
    assertEquals("drillMongo2" + i, reader.reader("k2" + i).readText().toString());
  }
}
 
开发者ID:axbaretto,项目名称:drill,代码行数:26,代码来源:TestBsonRecordReader.java

示例9: fromGridRef

import org.bson.BsonDocument; //导入依赖的package包/类
private BsonValue fromGridRef(SmofGridRef fileRef, PrimaryField fieldOpts) {
	if(fileRef.isEmpty()) {
		return new BsonNull();
	}
	if(fileRef.getId() == null) {
		final SmofObject annotation = fieldOpts.getSmofAnnotationAs(SmofObject.class);
		if(fileRef.getBucketName() == null) {
			fileRef.setBucketName(annotation.bucketName());
		}
		//TODO test if upload file adds id to fileRef
		if(!annotation.preInsert() && !fieldOpts.isForcePreInsert()) {
			return new BsonLazyObjectId(fieldOpts.getName(), fileRef);
		}
		fieldOpts.setForcePreInsert(false);
		dispatcher.insert(fileRef);
	}
	final BsonDocument bsonRef = new BsonDocument("id", new BsonObjectId(fileRef.getId()))
			.append("bucket", new BsonString(fileRef.getBucketName()));
	return bsonRef;
}
 
开发者ID:JPDSousa,项目名称:mongo-obj-framework,代码行数:21,代码来源:ObjectParser.java

示例10: fromObject

import org.bson.BsonDocument; //导入依赖的package包/类
private BsonDocument fromObject(Object value) {
	final BsonDocument document = new BsonDocument();
	final TypeParser<?> metadata = getTypeParser(value.getClass());
	final BsonArray lazyStack = new BsonArray();

	for(PrimaryField field : metadata.getAllFields()) {
		final Object fieldValue = extractValue(value, field);
		final BsonValue parsedValue;

		checkRequired(field, fieldValue);
		parsedValue = topParser.toBson(fieldValue, field);
		if(parsedValue instanceof BsonLazyObjectId) {
			lazyStack.add(parsedValue);
		}
		else {
			document.put(field.getName(), parsedValue);
		}
	}
	document.append(SmofParser.ON_INSERT, lazyStack);
	return document;
}
 
开发者ID:JPDSousa,项目名称:mongo-obj-framework,代码行数:22,代码来源:ObjectParser.java

示例11: replace

import org.bson.BsonDocument; //导入依赖的package包/类
private SmofInsertResult replace(T element, SmofOpOptions options) {
	final SmofInsertResult result = new SmofInsertResultImpl();
	result.setSuccess(true);
	options.upsert(true);
	if(options.isBypassCache() || !cache.asMap().containsValue(element)) {
		final BsonDocument document = parser.toBson(element);
		final Bson query = createUniquenessQuery(document);
		result.setPostInserts(BsonUtils.extrackPosInsertions(document));
		options.setReturnDocument(ReturnDocument.AFTER);
		document.remove(Element.ID);
		final BsonDocument resDoc = collection.findOneAndReplace(query, document, options.toFindOneAndReplace());
		element.setId(resDoc.get(Element.ID).asObjectId().getValue());
		cache.put(element.getId(), element);
	}
	return result;
}
 
开发者ID:JPDSousa,项目名称:mongo-obj-framework,代码行数:17,代码来源:SmofCollectionImpl.java

示例12: filterValue

import org.bson.BsonDocument; //导入依赖的package包/类
public static BsonValue filterValue(BsonValue value)
{
    BsonValue returnedValue = QUESTION_MARK_BSON;
    if (value instanceof BsonDocument)
    {
        returnedValue = filterParameters((BsonDocument) value);
    }
    else if (value instanceof BsonArray)
    {
        BsonArray array = (BsonArray) value;
        array = array.clone();
        returnedValue = array;
        int length = array.size();
        for (int i = 0; i < length; ++i)
        {
            BsonValue bsonValue = array.get(i);
            array.set(i, filterValue(bsonValue));
        }
    }
    return returnedValue;
}
 
开发者ID:dd00f,项目名称:ibm-performance-monitor,代码行数:22,代码来源:MongoUtilities.java

示例13: testArrayOfDocumentType

import org.bson.BsonDocument; //导入依赖的package包/类
@Test
public void testArrayOfDocumentType() throws IOException {
  BsonDocument bsonDoc = new BsonDocument();
  BsonWriter bw = new BsonDocumentWriter(bsonDoc);
  bw.writeStartDocument();
  bw.writeName("a");
  bw.writeString("MongoDB");
  bw.writeName("b");
  bw.writeStartArray();
  bw.writeStartDocument();
  bw.writeName("c");
  bw.writeInt32(1);
  bw.writeEndDocument();
  bw.writeEndArray();
  bw.writeEndDocument();
  bw.flush();
  writer.reset();
  bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
  FieldReader reader = writer.getMapVector().getReader();
  SingleMapReaderImpl mapReader = (SingleMapReaderImpl) reader;
  FieldReader reader3 = mapReader.reader("b");
  assertEquals("MongoDB", mapReader.reader("a").readText().toString());
}
 
开发者ID:axbaretto,项目名称:drill,代码行数:24,代码来源:TestBsonRecordReader.java

示例14: toBson

import org.bson.BsonDocument; //导入依赖的package包/类
BsonValue toBson() {
  @SuppressWarnings("unchecked")
  Encoder<T> encoder = BsonEncoding.encoderFor((Class<T>) value.getClass(), adapter);
  BsonDocument bson = new BsonDocument();
  org.bson.BsonWriter writer = new BsonDocumentWriter(bson);
  // Bson doesn't allow to write directly scalars / primitives, they have to be embedded in a document.
  writer.writeStartDocument();
  writer.writeName("$");
  encoder.encode(writer, value, EncoderContext.builder().build());
  writer.writeEndDocument();
  writer.flush();
  return bson.get("$");
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:14,代码来源:Support.java

示例15: bsonToGson

import org.bson.BsonDocument; //导入依赖的package包/类
/**
 * Reading from BSON to GSON
 */
@Test
public void bsonToGson() throws Exception {
    BsonDocument document = new BsonDocument();
    document.append("boolean", new BsonBoolean(true));
    document.append("int32", new BsonInt32(32));
    document.append("int64", new BsonInt64(64));
    document.append("double", new BsonDouble(42.42D));
    document.append("string", new BsonString("foo"));
    document.append("null", new BsonNull());
    document.append("array", new BsonArray());
    document.append("object", new BsonDocument());

    JsonElement element = TypeAdapters.JSON_ELEMENT.read(new BsonReader(new BsonDocumentReader(document)));
    check(element.isJsonObject());

    check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().isBoolean());
    check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().getAsBoolean());

    check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().isNumber());
    check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().getAsNumber().intValue()).is(32);

    check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().isNumber());
    check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().getAsNumber().longValue()).is(64L);

    check(element.getAsJsonObject().get("double").getAsJsonPrimitive().isNumber());
    check(element.getAsJsonObject().get("double").getAsJsonPrimitive().getAsNumber().doubleValue()).is(42.42D);

    check(element.getAsJsonObject().get("string").getAsJsonPrimitive().isString());
    check(element.getAsJsonObject().get("string").getAsJsonPrimitive().getAsString()).is("foo");

    check(element.getAsJsonObject().get("null").isJsonNull());
    check(element.getAsJsonObject().get("array").isJsonArray());
    check(element.getAsJsonObject().get("object").isJsonObject());
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:38,代码来源:BsonReaderTest.java


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