play.libs.Json.fromJson()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(3.4k)|赞(0)|评价(0)|浏览(133)

本文整理了Java中play.libs.Json.fromJson()方法的一些代码示例,展示了Json.fromJson()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Json.fromJson()方法的具体详情如下:
包路径:play.libs.Json
类名称:Json
方法名:fromJson

Json.fromJson介绍

[英]Converts a JsonNode to a Java value
[中]将JsonNode转换为Java值

代码示例

代码示例来源:origin: com.typesafe.play/play_2.12

/**
 * 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();
}

代码示例来源:origin: com.typesafe.play/play_2.11

/**
 * 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();
}

代码示例来源:origin: com.typesafe.play/play

/**
 * 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();
}

代码示例来源:origin: com.commercetools.sunrise/common

/**
 * {@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();
        }
      });
}

代码示例来源:origin: uk.gov.hmrc/microservice-bootstrap-java

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;
      }
    }
  }
}

相关文章