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);
}
}
}
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);
}
}
}
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);
}
}
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);
}
}
Aggregations