本文整理汇总了Java中org.bson.BsonValue类的典型用法代码示例。如果您正苦于以下问题:Java BsonValue类的具体用法?Java BsonValue怎么用?Java BsonValue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BsonValue类属于org.bson包,在下文中一共展示了BsonValue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCustomizedBsonConverter
import org.bson.BsonValue; //导入依赖的package包/类
@Test
public void testCustomizedBsonConverter() throws Exception {
BsonValueConverterRepertory.registerCustomizedBsonConverter(String.class, new AbstractBsonConverter<String, BsonString>() {
@Override
public String decode(BsonReader bsonReader) {
return "replace string";
}
@Override
public void encode(BsonWriter bsonWriter, String value) {
}
@Override
public String decode(BsonValue bsonValue) {
return "replace string";
}
@Override
public BsonString encode(String object) {
return new BsonString("replace string");
}
});
readFrom();
}
示例2: getPath
import org.bson.BsonValue; //导入依赖的package包/类
static List<String> getPath(BsonValue path) {
List<String> result = new ArrayList<String>();
StringBuilder builder = new StringBuilder();
String cleanPath = path.asString().getValue().replaceAll("\"", "");
for (int index = 0; index < cleanPath.length(); index++) {
char c = cleanPath.charAt(index);
if (c == '/') {
result.add(decodePath(builder.toString()));
builder.delete(0, builder.length());
} else {
builder.append(c);
}
}
result.add(decodePath(builder.toString()));
return result;
}
示例3: add
import org.bson.BsonValue; //导入依赖的package包/类
@Override
public void add(List<String> path, BsonValue value) {
if (path.isEmpty()) {
error(Operation.ADD, "path is empty , path : ");
} else {
BsonValue parentNode = getParentNode(path, Operation.ADD);
String fieldToReplace = path.get(path.size() - 1).replaceAll("\"", "");
if (fieldToReplace.equals("") && path.size() == 1)
target = value;
else if (!parentNode.isDocument() && !parentNode.isArray())
error(Operation.ADD, "parent is not a container in source, path provided : " + PathUtils.getPathRepresentation(path) + " | node : " + parentNode);
else if (parentNode.isArray())
addToArray(path, value, parentNode);
else
addToObject(path, parentNode, value);
}
}
示例4: replace
import org.bson.BsonValue; //导入依赖的package包/类
@Override
public void replace(List<String> path, BsonValue value) {
if (path.isEmpty()) {
error(Operation.REPLACE, "path is empty");
} else {
BsonValue parentNode = getParentNode(path, Operation.REPLACE);
String fieldToReplace = path.get(path.size() - 1).replaceAll("\"", "");
if (isNullOrEmpty(fieldToReplace) && path.size() == 1)
target = value;
else if (parentNode.isDocument())
parentNode.asDocument().put(fieldToReplace, value);
else if (parentNode.isArray())
parentNode.asArray().set(arrayIndex(fieldToReplace, parentNode.asArray().size() - 1, false), value);
else
error(Operation.REPLACE, "noSuchPath in source, path provided : " + PathUtils.getPathRepresentation(path));
}
}
示例5: remove
import org.bson.BsonValue; //导入依赖的package包/类
@Override
public void remove(List<String> path) {
if (path.isEmpty()) {
error(Operation.REMOVE, "path is empty");
} else {
BsonValue parentNode = getParentNode(path, Operation.REMOVE);
String fieldToRemove = path.get(path.size() - 1).replaceAll("\"", "");
if (parentNode.isDocument())
parentNode.asDocument().remove(fieldToRemove);
else if (parentNode.isArray()) {
// If path specifies a non-existent array element and the REMOVE_NONE_EXISTING_ARRAY_ELEMENT flag is not set, then
// arrayIndex will throw an error.
int i = arrayIndex(fieldToRemove, parentNode.asArray().size() - 1, flags.contains(CompatibilityFlags.REMOVE_NONE_EXISTING_ARRAY_ELEMENT));
// However, BsonArray.remove(int) is not very forgiving, so we need to avoid making the call if the index is past the end
// otherwise, we'll get an IndexArrayOutOfBounds error
if (i < parentNode.asArray().size()) {
parentNode.asArray().remove(i);
}
} else
error(Operation.REMOVE, "noSuchPath in source, path provided : " + PathUtils.getPathRepresentation(path));
}
}
示例6: asBson
import org.bson.BsonValue; //导入依赖的package包/类
public static BsonArray asBson(final BsonValue source, final BsonValue target, EnumSet<DiffFlags> flags) {
final List<Diff> diffs = new ArrayList<Diff>();
List<Object> path = new ArrayList<Object>(0);
// generating diffs in the order of their occurrence
generateDiffs(diffs, path, source, target);
if (!flags.contains(DiffFlags.OMIT_MOVE_OPERATION)) {
// Merging remove & add to move operation
compactDiffs(diffs);
}
if (!flags.contains(DiffFlags.OMIT_COPY_OPERATION)) {
// Introduce copy operation
introduceCopyOperation(source, target, diffs);
}
return getBsonNodes(diffs, flags);
}
示例7: computeUnchangedValues
import org.bson.BsonValue; //导入依赖的package包/类
private static void computeUnchangedValues(Map<BsonValue, List<Object>> unchangedValues, List<Object> path, BsonValue source, BsonValue target) {
if (source.equals(target)) {
if (!unchangedValues.containsKey(target)) {
unchangedValues.put(target, path);
}
return;
}
if (source.getBsonType().equals(target.getBsonType())) {
switch (source.getBsonType()) {
case DOCUMENT:
computeDocument(unchangedValues, path, source, target);
break;
case ARRAY:
computeArray(unchangedValues, path, source, target);
default:
/* nothing */
}
}
}
示例8: testPatchAppliedCleanly
import org.bson.BsonValue; //导入依赖的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));
}
}
示例9: testSampleJsonDiff
import org.bson.BsonValue; //导入依赖的package包/类
@Test
public void testSampleJsonDiff() throws Exception {
for (int i = 0; i < jsonNode.size(); i++) {
BsonValue first = jsonNode.get(i).asDocument().get("first");
BsonValue second = jsonNode.get(i).asDocument().get("second");
// System.out.println("Test # " + i);
// System.out.println(first);
// System.out.println(second);
BsonArray actualPatch = BsonDiff.asBson(first, second);
// System.out.println(actualPatch);
BsonValue secondPrime = BsonPatch.apply(actualPatch, first);
// System.out.println(secondPrime);
Assert.assertTrue(second.equals(secondPrime));
}
}
示例10: testRenderedOperationsExceptMoveAndCopy
import org.bson.BsonValue; //导入依赖的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));
}
示例11: convertBinaryUUID
import org.bson.BsonValue; //导入依赖的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);
}
示例12: getBsonValueList
import org.bson.BsonValue; //导入依赖的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;
}
示例13: handleArrayForBsonArray
import org.bson.BsonValue; //导入依赖的package包/类
private Object handleArrayForBsonArray(BsonArray bsonArray, Field field, BsonMapperConfig bsonMapperConfig) {
ArrayList<Object> arrayList = new ArrayList<Object>();
Class<?> fieldClazz = field.getType();
for (BsonValue bsonValue : bsonArray) {
if (bsonValue == null) {
continue;
}
if (bsonValue.isArray()) {
arrayList.add(decode(bsonValue.asArray(), field, bsonMapperConfig));
} else {
Object javaValue;
if (bsonValue.isDocument()) {
javaValue = BsonValueConverterRepertory.getBsonDocumentConverter().decode(bsonValue.asDocument(), fieldClazz.getComponentType(), bsonMapperConfig);
} else {
javaValue = BsonValueConverterRepertory.getValueConverterByBsonType(bsonValue.getBsonType()).decode(bsonValue);
}
arrayList.add(javaValue);
}
}
return arrayList.toArray((Object[]) Array.newInstance(fieldClazz.getComponentType(), 0));
}
示例14: toBson
import org.bson.BsonValue; //导入依赖的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("$");
}
示例15: move
import org.bson.BsonValue; //导入依赖的package包/类
@Override
public void move(List<String> fromPath, List<String> toPath) {
BsonValue parentNode = getParentNode(fromPath, Operation.MOVE);
String field = fromPath.get(fromPath.size() - 1).replaceAll("\"", "");
BsonValue valueNode = parentNode.isArray() ? parentNode.asArray().get(Integer.parseInt(field)) : parentNode.asDocument().get(field);
remove(fromPath);
add(toPath, valueNode);
}