/** * Converts a JSON request to a given class. Conversion is performed * with [[Json.fromJson(JsonNode,Class)]]. * * Will return Optional.empty() if the request body is not an instance of JsonNode. * If the JsonNode simply has missing fields, a valid reference with null fields is returne. * * @param <A> The type to convert the JSON value to. * @param clazz The class to convert the JSON value to. * @return The converted value if the request has a JSON body or an empty value if the request has an empty body or a body of a different type. */ public <A> Optional<A> parseJson(Class<A> clazz) { return (body instanceof JsonNode) ? Optional.of(Json.fromJson(asJson(), clazz)) : Optional.empty(); }
/** * Converts a JSON request to a given class. Conversion is performed * with [[Json.fromJson(JsonNode,Class)]]. * * Will return Optional.empty() if the request body is not an instance of JsonNode. * If the JsonNode simply has missing fields, a valid reference with null fields is returne. * * @param <A> The type to convert the JSON value to. * @param clazz The class to convert the JSON value to. * @return The converted value if the request has a JSON body or an empty value if the request has an empty body or a body of a different type. */ public <A> Optional<A> parseJson(Class<A> clazz) { return (body instanceof JsonNode) ? Optional.of(Json.fromJson(asJson(), clazz)) : Optional.empty(); }
/** * Converts a JSON request to a given class. Conversion is performed * with [[Json.fromJson(JsonNode,Class)]]. * * Will return Optional.empty() if the request body is not an instance of JsonNode. * If the JsonNode simply has missing fields, a valid reference with null fields is returne. * * @param <A> The type to convert the JSON value to. * @param clazz The class to convert the JSON value to. * @return The converted value if the request has a JSON body or an empty value if the request has an empty body or a body of a different type. */ public <A> Optional<A> parseJson(Class<A> clazz) { return (body instanceof JsonNode) ? Optional.of(Json.fromJson(asJson(), clazz)) : Optional.empty(); }
/** * {@inheritDoc} */ @Override public <U> Optional<U> findObjectByKey(final String key, final Class<U> clazz) { return findValueByKey(key) .flatMap(valueAsJson -> { try { final U value = Json.fromJson(Json.parse(valueAsJson), clazz); return Optional.of(value); } catch (RuntimeException e) { logger.error("Could not parse value in session key \"{}\" into type \"{}\"", key, clazz.getSimpleName(), e); return Optional.empty(); } }); }
protected <T> F.Promise<Result> withJsonBody(Class<T> klass, ToPromiseResult<T> f) { JsonNode jsonNode = request().body().asJson(); if (jsonNode == null) { return pure(badRequest("could not parse body to JSON")); } else { try { T obj = play.libs.Json.fromJson(jsonNode, klass); F.Tuple<Integer, Object> r = f.apply(obj); return pure(Results.status(r._1, toJson(r._2))); } catch (ConstraintViolationException ex) { return pure((badRequest(createJsonResponse(ex)))); } catch (RuntimeException e) { if (e.getCause() instanceof JsonProcessingException) { return pure(badRequest(handleProcessingException((JsonProcessingException) e.getCause()))); } else { throw e; } } } }