use of org.bson.json.JsonParseException in project torodb by torodb.
the class AbstractBackendDeserializer method deserialize.
@Override
public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
T backend = backendProvider.get();
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
ObjectNode node = (ObjectNode) jp.getCodec().readTree(jp);
JsonNode fieldNode = null;
Class<? extends BackendImplementation> backendImplementationClass = null;
Iterator<String> fieldNamesIterator = node.fieldNames();
while (fieldNamesIterator.hasNext()) {
String fieldName = fieldNamesIterator.next();
if (backendImplementationClass != null) {
throw new JsonParseException("Found multiples backend implementations but only one is " + "allowed");
}
fieldNode = node.get(fieldName);
if (backend.hasBackendImplementation(fieldName)) {
backendImplementationClass = backend.getBackendImplementationClass(fieldName);
} else if (setterMap.containsKey(fieldName)) {
Object value = mapper.treeToValue(fieldNode, setterMap.get(fieldName).v1());
setterMap.get(fieldName).v2().accept(backend, value);
} else {
throw new SystemException("AbstractBackend " + node.fields().next() + " is not valid.");
}
}
if (backendImplementationClass != null) {
backend.setBackendImplementation(jp.getCodec().treeToValue(fieldNode, backendImplementationClass));
}
return backend;
}
use of org.bson.json.JsonParseException in project spring-data-mongodb by spring-projects.
the class ReactivePartTreeMongoQuery method createQueryInternal.
private Query createQueryInternal(ConvertingParameterAccessor accessor, boolean isCountQuery) {
MongoQueryCreator creator = new MongoQueryCreator(tree, accessor, context, isCountQuery ? false : isGeoNearQuery);
Query query = creator.createQuery();
if (isCountQuery) {
return query;
}
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);
}
}
use of org.bson.json.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(), 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);
}
}
use of org.bson.json.JsonParseException in project spring-data-mongodb by spring-projects.
the class JsonScanner method scanRegularExpression.
/**
* Reads {@code RegularExpressionToken} from source. The following variants of lexemes are possible:
*
* <pre>
* /pattern/
* /\(pattern\)/
* /pattern/ims
* </pre>
*
* Options can include 'i','m','x','s'
*
* @return The regular expression token.
* @throws JsonParseException if regular expression representation is not valid.
*/
private JsonToken scanRegularExpression() {
int start = buffer.getPosition() - 1;
int options = -1;
RegularExpressionState state = RegularExpressionState.IN_PATTERN;
while (true) {
int c = buffer.read();
switch(state) {
case IN_PATTERN:
switch(c) {
case -1:
state = RegularExpressionState.INVALID;
break;
case '/':
state = RegularExpressionState.IN_OPTIONS;
options = buffer.getPosition();
break;
case '\\':
state = RegularExpressionState.IN_ESCAPE_SEQUENCE;
break;
default:
state = RegularExpressionState.IN_PATTERN;
break;
}
break;
case IN_ESCAPE_SEQUENCE:
state = RegularExpressionState.IN_PATTERN;
break;
case IN_OPTIONS:
switch(c) {
case 'i':
case 'm':
case 'x':
case 's':
state = RegularExpressionState.IN_OPTIONS;
break;
case ',':
case '}':
case ']':
case ')':
case -1:
state = RegularExpressionState.DONE;
break;
default:
if (Character.isWhitespace(c)) {
state = RegularExpressionState.DONE;
} else {
state = RegularExpressionState.INVALID;
}
break;
}
break;
default:
break;
}
switch(state) {
case DONE:
buffer.unread(c);
int end = buffer.getPosition();
BsonRegularExpression regex = new BsonRegularExpression(buffer.substring(start + 1, options - 1), buffer.substring(options, end));
return new JsonToken(JsonTokenType.REGULAR_EXPRESSION, regex);
case INVALID:
throw new JsonParseException("Invalid JSON regular expression. Position: %d.", buffer.getPosition());
default:
}
}
}
use of org.bson.json.JsonParseException in project spring-data-mongodb by spring-projects.
the class ParameterBindingDocumentCodec method decode.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Document decode(final BsonReader reader, final DecoderContext decoderContext) {
if (reader instanceof ParameterBindingJsonReader) {
ParameterBindingJsonReader bindingReader = (ParameterBindingJsonReader) reader;
// binds just placeholder queries like: `@Query(?0)`
if (bindingReader.currentValue instanceof org.bson.Document) {
return (Document) bindingReader.currentValue;
} else if (bindingReader.currentValue instanceof String) {
try {
return decode((String) bindingReader.currentValue, new Object[0]);
} catch (JsonParseException jsonParseException) {
throw new IllegalArgumentException("Expression result is not a valid json document!", jsonParseException);
}
} else if (bindingReader.currentValue instanceof Map) {
return new Document((Map) bindingReader.currentValue);
}
}
Document document = new Document();
try {
reader.readStartDocument();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String fieldName = reader.readName();
Object value = readValue(reader, decoderContext);
document.put(fieldName, value);
}
reader.readEndDocument();
} catch (JsonParseException | BsonInvalidOperationException e) {
try {
Object value = readValue(reader, decoderContext);
if (value instanceof Map<?, ?>) {
if (!((Map) value).isEmpty()) {
return new Document((Map<String, Object>) value);
}
}
} catch (Exception ex) {
e.addSuppressed(ex);
throw e;
}
}
return document;
}
Aggregations