Search in sources :

Example 1 with JsonParseException

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SystemException(com.torodb.core.exceptions.SystemException) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParseException(org.bson.json.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with JsonParseException

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);
    }
}
Also used : 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) JsonParseException(org.bson.json.JsonParseException)

Example 3 with JsonParseException

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);
    }
}
Also used : Field(org.springframework.data.mongodb.core.query.Field) 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) JsonParseException(org.bson.json.JsonParseException)

Example 4 with JsonParseException

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:
        }
    }
}
Also used : BsonRegularExpression(org.bson.BsonRegularExpression) JsonParseException(org.bson.json.JsonParseException)

Example 5 with JsonParseException

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;
}
Also used : BsonInvalidOperationException(org.bson.BsonInvalidOperationException) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) JsonParseException(org.bson.json.JsonParseException) Map(java.util.Map) BsonInvalidOperationException(org.bson.BsonInvalidOperationException) JsonParseException(org.bson.json.JsonParseException)

Aggregations

JsonParseException (org.bson.json.JsonParseException)26 String (java.lang.String)19 Date (java.util.Date)4 SimpleDateFormat (java.text.SimpleDateFormat)3 DateFormat (java.text.DateFormat)2 ParsePosition (java.text.ParsePosition)2 Decimal128 (org.bson.types.Decimal128)2 MaxKey (org.bson.types.MaxKey)2 MinKey (org.bson.types.MinKey)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 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 SystemException (com.torodb.core.exceptions.SystemException)1 Calendar (java.util.Calendar)1 Map (java.util.Map)1