Search in sources :

Example 1 with JsonParseOpts

use of ratpack.jackson.JsonParseOpts in project ratpack by ratpack.

the class JsonParser method parse.

@Override
public <T> T parse(Context context, TypedData body, Parse<T, JsonParseOpts> parse) throws IOException {
    if (!body.getContentType().isJson()) {
        return null;
    }
    JsonParseOpts opts = parse.getOpts().orElse(DefaultJsonParseOpts.INSTANCE);
    TypeToken<T> type = parse.getType();
    ObjectMapper objectMapper = opts.getObjectMapper().orElseGet(() -> context.get(ObjectMapper.class));
    InputStream inputStream = body.getInputStream();
    if (type.equals(JSON_NODE_TYPE)) {
        return cast(objectMapper.readTree(inputStream));
    } else {
        return objectMapper.readValue(inputStream, toJavaType(type, objectMapper));
    }
}
Also used : InputStream(java.io.InputStream) JsonParseOpts(ratpack.jackson.JsonParseOpts) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 InputStream (java.io.InputStream)1 JsonParseOpts (ratpack.jackson.JsonParseOpts)1