Search in sources :

Example 1 with JSONParseException

use of com.mongodb.util.JSONParseException in project spring-data-mongodb by spring-projects.

the class MongoTemplate method toDocument.

/**
 * @param objectToSave
 * @param writer
 * @return
 */
private <T> Document toDocument(T objectToSave, MongoWriter<T> writer) {
    if (objectToSave instanceof Document) {
        return (Document) objectToSave;
    }
    if (!(objectToSave instanceof String)) {
        Document dbDoc = new Document();
        writer.write(objectToSave, dbDoc);
        if (dbDoc.containsKey(ID_FIELD) && dbDoc.get(ID_FIELD) == null) {
            dbDoc.remove(ID_FIELD);
        }
        return dbDoc;
    } else {
        try {
            return Document.parse((String) objectToSave);
        } catch (JSONParseException e) {
            throw new MappingException("Could not parse given String to save into a JSON document!", e);
        } catch (org.bson.json.JsonParseException e) {
            throw new MappingException("Could not parse given String to save into a JSON document!", e);
        }
    }
}
Also used : JSONParseException(com.mongodb.util.JSONParseException) Document(org.bson.Document) MappingException(org.springframework.data.mapping.MappingException)

Example 2 with JSONParseException

use of com.mongodb.util.JSONParseException in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method toDbObject.

/**
 * @param objectToSave
 * @param writer
 * @return
 */
private <T> Document toDbObject(T objectToSave, MongoWriter<T> writer) {
    if (objectToSave instanceof Document) {
        return (Document) objectToSave;
    }
    if (!(objectToSave instanceof String)) {
        Document dbDoc = new Document();
        writer.write(objectToSave, dbDoc);
        if (dbDoc.containsKey(ID_FIELD) && dbDoc.get(ID_FIELD) == null) {
            dbDoc.remove(ID_FIELD);
        }
        return dbDoc;
    } else {
        try {
            return Document.parse((String) objectToSave);
        } catch (JSONParseException | org.bson.json.JsonParseException e) {
            throw new MappingException("Could not parse given String to save into a JSON document!", e);
        }
    }
}
Also used : JSONParseException(com.mongodb.util.JSONParseException) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) MappingException(org.springframework.data.mapping.MappingException)

Example 3 with JSONParseException

use of com.mongodb.util.JSONParseException in project spring-data-mongodb by spring-projects.

the class PartTreeMongoQuery method createQuery.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.repository.query.AbstractMongoQuery#createQuery(org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor, boolean)
	 */
@Override
protected Query createQuery(ConvertingParameterAccessor accessor) {
    MongoQueryCreator creator = new MongoQueryCreator(tree, accessor, context, isGeoNearQuery);
    Query query = creator.createQuery();
    if (tree.isLimiting()) {
        query.limit(tree.getMaxResults());
    }
    TextCriteria textCriteria = accessor.getFullText();
    if (textCriteria != null) {
        query.addCriteria(textCriteria);
    }
    String fieldSpec = this.getQueryMethod().getFieldSpecification();
    if (!StringUtils.hasText(fieldSpec)) {
        ReturnedType returnedType = processor.withDynamicProjection(accessor).getReturnedType();
        if (returnedType.needsCustomConstruction()) {
            Field fields = query.fields();
            for (String field : returnedType.getInputProperties()) {
                fields.include(field);
            }
        }
        return query;
    }
    try {
        BasicQuery result = new BasicQuery(query.getQueryObject(), new Document((BasicDBObject) JSON.parse(fieldSpec)));
        result.setSortObject(query.getSortObject());
        return result;
    } catch (JSONParseException o_O) {
        throw new IllegalStateException(String.format("Invalid query or field specification in %s!", getQueryMethod()), o_O);
    }
}
Also used : JSONParseException(com.mongodb.util.JSONParseException) Field(org.springframework.data.mongodb.core.query.Field) BasicDBObject(com.mongodb.BasicDBObject) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) RepositoryQuery(org.springframework.data.repository.query.RepositoryQuery) TextCriteria(org.springframework.data.mongodb.core.query.TextCriteria) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) ReturnedType(org.springframework.data.repository.query.ReturnedType) Document(org.bson.Document)

Example 4 with JSONParseException

use of com.mongodb.util.JSONParseException in project spring-data-mongodb by spring-projects.

the class ReactivePartTreeMongoQuery method createQuery.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.repository.query.AbstractMongoQuery#createQuery(org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor, boolean)
	 */
@Override
protected Query createQuery(ConvertingParameterAccessor accessor) {
    MongoQueryCreator creator = new MongoQueryCreator(tree, accessor, context, isGeoNearQuery);
    Query query = creator.createQuery();
    if (tree.isLimiting()) {
        query.limit(tree.getMaxResults());
    }
    TextCriteria textCriteria = accessor.getFullText();
    if (textCriteria != null) {
        query.addCriteria(textCriteria);
    }
    String fieldSpec = getQueryMethod().getFieldSpecification();
    if (!StringUtils.hasText(fieldSpec)) {
        ReturnedType returnedType = processor.withDynamicProjection(accessor).getReturnedType();
        if (returnedType.isProjecting()) {
            returnedType.getInputProperties().forEach(query.fields()::include);
        }
        return query;
    }
    try {
        BasicQuery result = new BasicQuery(query.getQueryObject(), Document.parse(fieldSpec));
        result.setSortObject(query.getSortObject());
        return result;
    } catch (JSONParseException o_O) {
        throw new IllegalStateException(String.format("Invalid query or field specification in %s!", getQueryMethod()), o_O);
    }
}
Also used : JSONParseException(com.mongodb.util.JSONParseException) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) RepositoryQuery(org.springframework.data.repository.query.RepositoryQuery) TextCriteria(org.springframework.data.mongodb.core.query.TextCriteria) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) ReturnedType(org.springframework.data.repository.query.ReturnedType)

Aggregations

JSONParseException (com.mongodb.util.JSONParseException)4 Document (org.bson.Document)3 MappingException (org.springframework.data.mapping.MappingException)2 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)2 Query (org.springframework.data.mongodb.core.query.Query)2 TextCriteria (org.springframework.data.mongodb.core.query.TextCriteria)2 RepositoryQuery (org.springframework.data.repository.query.RepositoryQuery)2 ReturnedType (org.springframework.data.repository.query.ReturnedType)2 BasicDBObject (com.mongodb.BasicDBObject)1 ReturnDocument (com.mongodb.client.model.ReturnDocument)1 FullDocument (com.mongodb.client.model.changestream.FullDocument)1 Field (org.springframework.data.mongodb.core.query.Field)1