/** * Construct a new database reference with the given id and type. The type must be annotated with * {@link MongoCollection}, so that the name can be worked out. * * @param id The id of the database reference to construct * @param type The type of the object * @throws MongoJsonMappingException If no MongoCollection annotation is found on the type */ public DBRef(K id, Class<T> type) throws MongoJsonMappingException { this.id = id; MongoCollection collection = type.getAnnotation(MongoCollection.class); if (collection == null) { throw new MongoJsonMappingException("Only types that have the @MongoCollection annotation on them can be used with this constructor"); } this.collectionName = collection.name(); }
/** * Construct a new database reference with the given id and type. The type must be annotated with * {@link MongoCollection}, so that the name can be worked out. * * @param id The id of the database reference to construct * @param type The type of the object * @throws MongoJsonMappingException If no MongoCollection annotation is found on the type */ public DBRef(K id, Class<T> type) throws MongoJsonMappingException { this.id = id; MongoCollection collection = type.getAnnotation(MongoCollection.class); if (collection == null) { throw new MongoJsonMappingException("Only types that have the @MongoCollection annotation on them can be used with this constructor"); } this.collectionName = collection.name(); }
<S> S convertFromDbObject(DBObject dbObject, Class<S> clazz) throws MongoException { if (dbObject == null) { return null; } if (dbObject instanceof JacksonDBObject) { return (S) ((JacksonDBObject) dbObject).getObject(); } try { return objectMapper.readValue(new BsonObjectTraversingParser(this, dbObject), clazz); } catch (JsonMappingException e) { throw new MongoJsonMappingException(e); } catch (IOException e) { // This shouldn't happen throw new MongoException("Unknown error occurred converting BSON to object", e); } }
DBObject convertToBasicDbObject(T object) throws MongoException { if (object == null) { return null; } BsonObjectGenerator generator = new BsonObjectGenerator(); try { objectMapper.writeValue(generator, object); } catch (JsonMappingException e) { throw new MongoJsonMappingException(e); } catch (IOException e) { // This shouldn't happen throw new MongoException("Unknown error occurred converting BSON to object", e); } return generator.getDBObject(); }
DBObject convertToBasicDbObject(T object) throws MongoException { if (object == null) { return null; } BsonObjectGenerator generator = new BsonObjectGenerator(); try { objectMapper.writeValue(generator, object); } catch (JsonMappingException e) { throw new MongoJsonMappingException(e); } catch (IOException e) { // This shouldn't happen throw new MongoException("Unknown error occurred converting BSON to object", e); } return generator.getDBObject(); }
T convertFromDbObject(DBObject dbObject) throws MongoException { if (dbObject == null) { return null; } if (dbObject instanceof JacksonDBObject) { return (T) ((JacksonDBObject) dbObject).getObject(); } try { return (T) objectMapper.readValue(new BsonObjectTraversingParser(this, dbObject), type); } catch (JsonMappingException e) { throw new MongoJsonMappingException(e); } catch (IOException e) { // This shouldn't happen throw new MongoException("Unknown error occurred converting BSON to object", e); } }
<S> S convertFromDbObject(DBObject dbObject, Class<S> clazz) throws MongoException { if (dbObject == null) { return null; } if (dbObject instanceof JacksonDBObject) { return (S) ((JacksonDBObject) dbObject).getObject(); } try { return objectMapper.readValue(new BsonObjectTraversingParser(this, dbObject), clazz); } catch (JsonMappingException e) { throw new MongoJsonMappingException(e); } catch (IOException e) { // This shouldn't happen throw new MongoException("Unknown error occurred converting BSON to object", e); } }
T convertFromDbObject(DBObject dbObject) throws MongoException { if (dbObject == null) { return null; } if (dbObject instanceof JacksonDBObject) { return (T) ((JacksonDBObject) dbObject).getObject(); } try { return (T) objectMapper.readValue(new BsonObjectTraversingParser(this, dbObject), type); } catch (JsonMappingException e) { throw new MongoJsonMappingException(e); } catch (IOException e) { // This shouldn't happen throw new MongoException("Unknown error occurred converting BSON to object", e); } }
objectMapper.writeValue(generator, value); } catch (JsonMappingException e) { throw new MongoJsonMappingException(e); } catch (IOException e) { throw new RuntimeException("Somehow got an IOException writing to memory", e);
objectMapper.writeValue(generator, value); } catch (JsonMappingException e) { throw new MongoJsonMappingException(e); } catch (IOException e) { throw new RuntimeException("Somehow got an IOException writing to memory", e);
DBObject convertToDbObject(T object) throws MongoException { if (object == null) { return null; } if (isEnabled(Feature.USE_STREAM_SERIALIZATION)) { return new JacksonDBObject<T>(object); } else { BsonObjectGenerator generator = new BsonObjectGenerator(); try { objectMapper.writeValue(generator, object); } catch (JsonMappingException e) { throw new MongoJsonMappingException(e); } catch (IOException e) { // This shouldn't happen throw new MongoException("Unknown error occurred converting BSON to object", e); } return generator.getDBObject(); } }
DBObject convertToDbObject(T object) throws MongoException { if (object == null) { return null; } if (isEnabled(Feature.USE_STREAM_SERIALIZATION)) { return new JacksonDBObject<T>(object); } else { BsonObjectGenerator generator = new BsonObjectGenerator(); try { objectMapper.writeValue(generator, object); } catch (JsonMappingException e) { throw new MongoJsonMappingException(e); } catch (IOException e) { // This shouldn't happen throw new MongoException("Unknown error occurred converting BSON to object", e); } return generator.getDBObject(); } }
protected JacksonDBCollection(DBCollection dbCollection, JavaType type, JavaType keyType, ObjectMapper objectMapper, Map<Feature, Boolean> features) { this.dbCollection = dbCollection; this.type = type; this.keyType = keyType; this.objectMapper = objectMapper; this.decoderFactory = new JacksonDecoderFactory<T>(this, objectMapper, type); // We want to find how we should serialize the ID, in case it is passed to us try { this.idHandler = (IdHandler) IdHandlerFactory.getIdHandlerForProperty(objectMapper, type.getRawClass(), keyType.getRawClass()); } catch (JsonMappingException e) { throw new MongoJsonMappingException("Unable to introspect class", e); } if (features == null) { this.features = new ConcurrentHashMap<Feature, Boolean>(); } else { this.features = features; } dbCollection.setDBEncoderFactory(new JacksonEncoderFactory(objectMapper, this)); }
protected JacksonDBCollection(DBCollection dbCollection, JavaType type, JavaType keyType, ObjectMapper objectMapper, Map<Feature, Boolean> features) { this.dbCollection = dbCollection; this.type = type; this.keyType = keyType; this.objectMapper = objectMapper; this.decoderFactory = new JacksonDecoderFactory<T>(this, objectMapper, type); // We want to find how we should serialize the ID, in case it is passed to us try { this.idHandler = (IdHandler) IdHandlerFactory.getIdHandlerForProperty(objectMapper, type.getRawClass(), keyType.getRawClass()); } catch (JsonMappingException e) { throw new MongoJsonMappingException("Unable to introspect class", e); } if (features == null) { this.features = new ConcurrentHashMap<Feature, Boolean>(); } else { this.features = features; } dbCollection.setDBEncoderFactory(new JacksonEncoderFactory(objectMapper, this)); }
public int writeObject(OutputBuffer buf, BSONObject object) { if (object instanceof JacksonDBObject) { Object actualObject = ((JacksonDBObject) object).getObject(); OutputBufferOutputStream stream = new OutputBufferOutputStream(buf); BsonGenerator generator = new DBEncoderBsonGenerator(JsonGenerator.Feature.collectDefaults(), stream); try { objectMapper.writeValue(generator, actualObject); // The generator buffers everything so that it can write the number of bytes to the stream generator.close(); } catch (JsonMappingException e) { throw new MongoJsonMappingException(e); } catch (IOException e) { throw new MongoException("Error writing object out", e); } return stream.getCount(); } else { return defaultDBEncoder.writeObject(buf, object); } } }
public int writeObject(OutputBuffer buf, BSONObject object) { if (object instanceof JacksonDBObject) { Object actualObject = ((JacksonDBObject) object).getObject(); OutputBufferOutputStream stream = new OutputBufferOutputStream(buf); BsonGenerator generator = new DBEncoderBsonGenerator(JsonGenerator.Feature.collectDefaults(), stream); try { objectMapper.writeValue(generator, actualObject); // The generator buffers everything so that it can write the number of bytes to the stream generator.close(); } catch (JsonMappingException e) { throw new MongoJsonMappingException(e); } catch (IOException e) { throw new MongoException("Error writing object out", e); } return stream.getCount(); } else { return defaultDBEncoder.writeObject(buf, object); } } }