/** * Gets this value as a BsonNumber if it is one, otherwise throws exception * * @return a BsonNumber * @throws org.bson.BsonInvalidOperationException if this value is not of the expected type */ public BsonNumber asNumber() { if (getBsonType() != BsonType.INT32 && getBsonType() != BsonType.INT64 && getBsonType() != BsonType.DOUBLE) { throw new BsonInvalidOperationException(format("Value expected to be of a numerical BSON type is of unexpected type %s", getBsonType())); } return (BsonNumber) this; }
private void throwIfInvalidType(final BsonType expectedType) { if (getBsonType() != expectedType) { throw new BsonInvalidOperationException(format("Value expected to be of type %s is of unexpected type %s", expectedType, getBsonType())); } } }
@Override public BsonDocument clone() { BsonDocument to = new BsonDocument(); for (Entry<String, BsonValue> cur : entrySet()) { switch (cur.getValue().getBsonType()) { case DOCUMENT: to.put(cur.getKey(), cur.getValue().asDocument().clone()); break; case ARRAY: to.put(cur.getKey(), cur.getValue().asArray().clone()); break; case BINARY: to.put(cur.getKey(), BsonBinary.clone(cur.getValue().asBinary())); break; case JAVASCRIPT_WITH_SCOPE: to.put(cur.getKey(), BsonJavaScriptWithScope.clone(cur.getValue().asJavaScriptWithScope())); break; default: to.put(cur.getKey(), cur.getValue()); } } return to; }
@Override protected void doReadStartDocument() { BsonDocument document; if (currentValue.getBsonType() == BsonType.JAVASCRIPT_WITH_SCOPE) { document = currentValue.asJavaScriptWithScope().getScope(); } else { document = currentValue.asDocument(); } setContext(new Context(getContext(), BsonContextType.DOCUMENT, document)); }
@Test public void write() throws Exception { JsonObject obj = new JsonObject(); BigInteger bigInteger = new BigInteger(Long.toString(Long.MAX_VALUE)).multiply(new BigInteger("128")); obj.addProperty("bigInteger", bigInteger); BigDecimal bigDecimal = new BigDecimal(Long.MAX_VALUE).multiply(new BigDecimal(1024)); obj.addProperty("bigDecimal", bigDecimal); BsonDocument doc = Jsons.toBson(obj); check(doc.get("bigInteger").getBsonType()).is(BsonType.DECIMAL128); check(doc.get("bigInteger").asDecimal128().decimal128Value().bigDecimalValue().toBigInteger()).is(bigInteger); check(doc.get("bigDecimal").getBsonType()).is(BsonType.DECIMAL128); check(doc.get("bigDecimal").asDecimal128().decimal128Value().bigDecimalValue()).is(bigDecimal); }
public void convertFieldValue(Entry<String, BsonValue> keyvalueforStruct, Struct struct, Schema schema) { Object colValue = null; String key = keyvalueforStruct.getKey(); BsonType type = keyvalueforStruct.getValue().getBsonType(); switch (arrayEncoding) { case ARRAY: BsonType valueType = keyvalueforStruct.getValue().asArray().get(0).getBsonType(); List<BsonValue> arrValues = keyvalueforStruct.getValue().asArray().getValues(); ArrayList<Object> list = new ArrayList<>(); if (arrValue.getBsonType() == BsonType.STRING && valueType == BsonType.STRING) { String temp = arrValue.asString().getValue(); list.add(temp); } else if (arrValue.getBsonType() == BsonType.JAVASCRIPT && valueType == BsonType.JAVASCRIPT) { String temp = arrValue.asJavaScript().getCode(); list.add(temp); } else if (arrValue.getBsonType() == BsonType.OBJECT_ID && valueType == BsonType.OBJECT_ID) { String temp = arrValue.asObjectId().getValue().toString(); list.add(temp); } else if (arrValue.getBsonType() == BsonType.DOUBLE && valueType == BsonType.DOUBLE) { double temp = arrValue.asDouble().getValue(); list.add(temp); } else if (arrValue.getBsonType() == BsonType.BINARY && valueType == BsonType.BINARY) { byte[] temp = arrValue.asBinary().getData(); list.add(temp); } else if (arrValue.getBsonType() == BsonType.INT32 && valueType == BsonType.INT32) { int temp = arrValue.asInt32().getValue(); list.add(temp); } else if (arrValue.getBsonType() == BsonType.INT64 && valueType == BsonType.INT64) {
@Test public void bigNumbers() throws Exception { JsonObject obj = new JsonObject(); BigInteger bigInteger = new BigInteger(Long.toString(Long.MAX_VALUE)).multiply(new BigInteger("128")); obj.addProperty("bigInteger", bigInteger); BigDecimal bigDecimal = new BigDecimal(Long.MAX_VALUE).multiply(new BigDecimal(1024)); obj.addProperty("bigDecimal", bigDecimal); BsonDocument bson = Jsons.toBson(obj); check(bson.get("bigInteger").getBsonType()).is(BsonType.DECIMAL128); check(bson.get("bigInteger").asDecimal128().decimal128Value().bigDecimalValue().toBigInteger()).is(bigInteger); check(bson.get("bigDecimal").getBsonType()).is(BsonType.DECIMAL128); check(bson.get("bigDecimal").asDecimal128().decimal128Value().bigDecimalValue()).is(bigDecimal); check(Jsons.toGson(bson)).is(obj); } }
@Test public void basicNumbers() throws Exception { JsonObject obj = new JsonObject(); obj.addProperty("boolean", true); obj.addProperty("byte", (byte) 1); obj.addProperty("short", (short) 4); obj.addProperty("int", Integer.MAX_VALUE); obj.addProperty("long", Long.MAX_VALUE); obj.addProperty("float", 55F); obj.addProperty("double", 128D); BsonDocument doc = Jsons.toBson(obj); check(doc.get("boolean").getBsonType()).is(BsonType.BOOLEAN); check(doc.get("boolean").asBoolean()); check(doc.get("byte").getBsonType()).is(BsonType.INT32); check(doc.get("byte").asInt32().getValue()).is(1); check(doc.get("short").getBsonType()).is(BsonType.INT32); check(doc.get("short").asInt32().getValue()).is(4); check(doc.get("int").getBsonType()).is(BsonType.INT32); check(doc.get("int").asInt32().getValue()).is(Integer.MAX_VALUE); check(doc.get("long").getBsonType()).is(BsonType.INT64); check(doc.get("long").asInt64().getValue()).is(Long.MAX_VALUE); check(doc.get("float").getBsonType()).is(BsonType.DOUBLE); check(doc.get("float").asDouble().getValue()).is(55D); check(doc.get("double").getBsonType()).is(BsonType.DOUBLE); check(doc.get("double").asDouble().getValue()).is(128D); }
setCurrentBsonType(currentValue.getBsonType()); return getCurrentBsonType();
public void addFieldSchema(Entry<String, BsonValue> keyValuesforSchema, SchemaBuilder builder) { String key = keyValuesforSchema.getKey(); BsonType type = keyValuesforSchema.getValue().getBsonType(); switch (arrayEncoding) { case ARRAY: BsonType valueType = keyValuesforSchema.getValue().asArray().get(0).getBsonType(); for (BsonValue element: keyValuesforSchema.getValue().asArray()) { if (element.getBsonType() != valueType) { throw new ConnectException("Field " + key + " of schema " + builder.name() + " is not a homogenous array.\n" + "Check option 'struct' of parameter 'array.encoding'"); final BsonDocument arrayDocs = element.asDocument(); for (Entry<String, BsonValue> arrayDoc: arrayDocs.entrySet()) { final BsonType prevType = union.putIfAbsent(arrayDoc.getKey(), arrayDoc.getValue().getBsonType()); if (prevType == null) { addFieldSchema(arrayDoc, documentSchemaBuilder); else if (prevType != arrayDoc.getValue().getBsonType()) { throw new ConnectException("Field " + arrayDoc.getKey() + " of schema " + documentSchemaBuilder.name() + " is not the same type for all documents in the array.\n" + "Check option 'struct' of parameter 'array.encoding'");
check(doc.get("boolean").getBsonType()).is(BsonType.BOOLEAN); check(doc.get("boolean").asBoolean()); check(doc.get("int32").getBsonType()).is(BsonType.INT32); check(doc.get("int32").asInt32().getValue()).is(32); check(doc.get("int64").getBsonType()).is(BsonType.INT64); check(doc.get("int64").asInt64().getValue()).is(64L); check(doc.get("double").getBsonType()).is(BsonType.DOUBLE); check(doc.get("double").asDouble().getValue()).is(42.42D); check(doc.get("string").getBsonType()).is(BsonType.STRING); check(doc.get("string").asString().getValue()).is("foo"); check(doc.get("null").getBsonType()).is(BsonType.NULL); check(doc.get("null").isNull()); check(doc.get("array").getBsonType()).is(BsonType.ARRAY); check(doc.get("array").asArray()).isEmpty(); check(doc.get("object").getBsonType()).is(BsonType.DOCUMENT); check(doc.get("object").asDocument().keySet()).isEmpty();
@Override public BsonArray clone() { BsonArray to = new BsonArray(); for (BsonValue cur : this) { switch (cur.getBsonType()) { case DOCUMENT: to.add(cur.asDocument().clone()); break; case ARRAY: to.add(cur.asArray().clone()); break; case BINARY: to.add(BsonBinary.clone(cur.asBinary())); break; case JAVASCRIPT_WITH_SCOPE: to.add(BsonJavaScriptWithScope.clone(cur.asJavaScriptWithScope())); break; default: to.add(cur); } } return to; } }
@Test public void dateCodec() throws IOException { TypeAdapter<Date> adapter = GsonCodecs.typeAdapterFromCodec(new DateCodec()); Date date = new Date(); BsonDocument doc = new BsonDocument(); BsonDocumentWriter writer = new BsonDocumentWriter(doc); writer.writeStartDocument(); writer.writeName("$date"); adapter.write(new BsonWriter(writer), date); writer.writeEndDocument(); check(doc.keySet()).hasSize(1); check(doc.get("$date").getBsonType()).is(BsonType.DATE_TIME); check(doc.get("$date").asDateTime().getValue()).is(date.getTime()); } }
switch (value.getBsonType()) { case INT32: return value.asInt32().getValue();
private void pipeValue(final BsonValue value) { switch (value.getBsonType()) { case DOCUMENT: pipeDocument(value.asDocument()); break; default: throw new IllegalArgumentException("unhandled BSON type: " + value.getBsonType());
switch (value.getBsonType()) { case INT32: return value.asInt32().getValue();
private boolean check( HttpServerExchange exchange, RequestContext context) throws InvalidMetadataException { if (context.getContent() != null && !context.getContent().isDocument()) { throw new RuntimeException( "this hanlder only supports content of type json object; " + "content type: " + context .getContent() .getBsonType() .name()); } return checkers.stream().allMatch(checker -> checker.check(exchange, context, context.getContent().asDocument(), null)); } }
public QueryResult<String> distinct(String key, Bson query) { long start = startQuery(); List<String> l = new ArrayList<>(); MongoCursor<BsonValue> iterator = mongoDBNativeQuery.distinct(key, query, BsonValue.class).iterator(); while (iterator.hasNext()) { BsonValue value = iterator.next(); if (value == null || value.isNull()) { l.add(null); } else if (value.isString()) { l.add(value.asString().getValue()); } else { throw new IllegalArgumentException("Found result with BsonType != " + BsonType.STRING + " : " + value.getBsonType()); } } return endQuery(l, start); }
private static void embeddedDocuments( List<BsonDocument> embeddedData, String requestPath, Representation rep, boolean isHalFull) { embeddedData.stream().forEach((d) -> { BsonValue _id = d.get("_id"); if (_id != null && (_id.isString() || _id.isObjectId())) { Representation nrep = new Representation(); if (isHalFull) { nrep.addProperty("_type", new BsonString(RequestContext.TYPE.INDEX.name())); } nrep.addProperties(d); rep.addRepresentation("rh:index", nrep); } else { rep.addWarning("index with _id " + _id + (_id == null ? " " : " of type " + _id.getBsonType().name()) + "filtered out. Indexes can only " + "have ids of type String"); LOGGER.debug("index missing string _id field", d); } }); }
public static Object extractValueEx(BsonValue value) { switch (value.getBsonType()) { case DOUBLE: return value.asDouble().getValue(); case STRING: return value.asString().getValue(); case OBJECT_ID: return value.asObjectId().getValue(); case INT32: return value.asInt32().getValue(); case INT64: return value.asInt64().getValue(); case DECIMAL128: return value.asDecimal128().getValue(); case NULL: return null; } throw new IllegalArgumentException("Unsupported ID type: " + value.getClass()); }