/** * Create a BSON object with a single field from a pair key/value * * @param key the key which corresponds to the field id in the object * @param value the value which corresponds to the field value in the object * @return the new BSON object */ public static Document fieldMapToBson(String key, Object value) { return fieldMapToBson(Collections.singletonMap(key, value)); }
/** * Cast an object according to its instance * * @param value the object to transform * @return the BSON object */ public static Object valueToBson(Object value) { if (value instanceof Map) { @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) value; return fieldMapToBson(map); } else if (value instanceof List) { @SuppressWarnings("unchecked") List<Object> values = (List<Object>) value; return listToBson(values); } else if (value instanceof Object[]) { return listToBson(Arrays.asList((Object[]) value)); } else { return serializableToBson(value); } }