Search in sources :

Example 1 with DecodingFailureException

use of com.couchbase.client.core.error.DecodingFailureException in project couchbase-jvm-clients by couchbase.

the class JsonStreamParser method feed.

/**
 * Consumes all readable bytes from the given buffer. Searches for values matching
 * the configured JSON pointers and invokes callbacks for any matches.
 * <p>
 * Call this method repeatedly as more input becomes available.
 *
 * @throws DecodingFailureException if malformed JSON is detected in this chunk of input
 *                                 or if a value consumer throws an exception.
 */
public void feed(ByteBuf input) throws DecodingFailureException {
    try {
        feedJackson(input);
        processTokens();
        collectGarbage();
    } catch (Throwable t) {
        throw new DecodingFailureException(t);
    }
}
Also used : DecodingFailureException(com.couchbase.client.core.error.DecodingFailureException)

Example 2 with DecodingFailureException

use of com.couchbase.client.core.error.DecodingFailureException in project couchbase-jvm-clients by couchbase.

the class SearchRow method fromResponse.

public static SearchRow fromResponse(final SearchChunkRow row, final JsonSerializer serializer) {
    try {
        JsonObject hit = JacksonTransformers.MAPPER.readValue(row.data(), JsonObject.class);
        String index = hit.getString("index");
        String id = hit.getString("id");
        double score = hit.getDouble("score");
        JsonObject explanationJson = defaultIfNull(hit.getObject("explanation"), JsonObject::create);
        Optional<SearchRowLocations> locations = Optional.ofNullable(hit.getObject("locations")).map(SearchRowLocations::from);
        JsonObject fragmentsJson = hit.getObject("fragments");
        Map<String, List<String>> fragments;
        if (fragmentsJson != null) {
            fragments = new HashMap<>(fragmentsJson.size());
            for (String field : fragmentsJson.getNames()) {
                List<String> fragment;
                JsonArray fragmentJson = fragmentsJson.getArray(field);
                if (fragmentJson != null) {
                    fragment = new ArrayList<>(fragmentJson.size());
                    for (int i = 0; i < fragmentJson.size(); i++) {
                        fragment.add(fragmentJson.getString(i));
                    }
                } else {
                    fragment = Collections.emptyList();
                }
                fragments.put(field, fragment);
            }
        } else {
            fragments = Collections.emptyMap();
        }
        byte[] fields = null;
        if (hit.containsKey("fields")) {
            // daschl: this is a bit wasteful and should be streamlined
            fields = JacksonTransformers.MAPPER.writeValueAsBytes(hit.getObject("fields").toMap());
        }
        return new SearchRow(index, id, score, explanationJson, locations, fragments, fields, serializer);
    } catch (IOException e) {
        throw new DecodingFailureException("Failed to decode row '" + new String(row.data(), UTF_8) + "'", e);
    }
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject) IOException(java.io.IOException) JsonArray(com.couchbase.client.java.json.JsonArray) ArrayList(java.util.ArrayList) List(java.util.List) DecodingFailureException(com.couchbase.client.core.error.DecodingFailureException)

Example 3 with DecodingFailureException

use of com.couchbase.client.core.error.DecodingFailureException in project couchbase-jvm-clients by couchbase.

the class KeyValueMessageHandler method decodeAndComplete.

/**
 * Tries to decode the response and succeed the request.
 * <p>
 * If decoding fails, will fail the underlying request as well.
 *
 * @param request the request to decode and complete.
 * @param response the raw response to decode.
 */
private void decodeAndComplete(final KeyValueRequest<Response> request, final ByteBuf response) {
    try {
        Response decoded = request.decode(response, channelContext);
        request.succeed(decoded);
    } catch (Throwable t) {
        request.fail(new DecodingFailureException(t));
    }
}
Also used : Response(com.couchbase.client.core.msg.Response) DecodingFailureException(com.couchbase.client.core.error.DecodingFailureException)

Example 4 with DecodingFailureException

use of com.couchbase.client.core.error.DecodingFailureException in project couchbase-jvm-clients by couchbase.

the class JsonStreamParser method endOfInput.

/**
 * Should be called after last chunk of data to parse has been fed.
 * After calling this method no more data can be fed and parser assumes
 * no more data will be available.
 *
 * @throws DecodingFailureException if malformed JSON is detected in this chunk of input.
 */
public void endOfInput() {
    try {
        feeder.endOfInput();
        processTokens();
    } catch (Throwable t) {
        throw new DecodingFailureException(t);
    }
}
Also used : DecodingFailureException(com.couchbase.client.core.error.DecodingFailureException)

Aggregations

DecodingFailureException (com.couchbase.client.core.error.DecodingFailureException)4 Response (com.couchbase.client.core.msg.Response)1 JsonArray (com.couchbase.client.java.json.JsonArray)1 JsonObject (com.couchbase.client.java.json.JsonObject)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1