play.libs.Json类的使用及代码示例

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

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

Json介绍

[英]Helper functions to handle JsonNode values.
[中]用于处理JsonNode值的助手函数。

代码示例

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

@Override
  protected JsonNode parse(Http.RequestHeader request, ByteString bytes) throws Exception {
    return play.libs.Json.parse(bytes.iterator().asInputStream());
  }
}

代码示例来源: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

private JsonNode error(JsonNode content) {
  ObjectNode result = Json.newObject();
  result.set("error", content);
  return result;
}

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

/**
 * Parses a byte array representing a json, and return it as a JsonNode.
 * @param src    the JSON input bytes.
 * @return the JSON node.
 */
public static JsonNode parse(byte[] src) {
 try {
  return mapper().readTree(src);
 } catch(Throwable t) {
  throw new RuntimeException(t);
 }
}

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

/**
 * Creates a new empty ObjectNode.
 * @return new empty ObjectNode.
 */
public static ObjectNode newObject() {
 return mapper().createObjectNode();
}

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

private static String generateJson(Object o, boolean prettyPrint, boolean escapeNonASCII) {
 try {
  ObjectWriter writer = mapper().writer();
  if (prettyPrint) {
   writer = writer.with(SerializationFeature.INDENT_OUTPUT);
  }
  if (escapeNonASCII) {
   writer = writer.with(Feature.ESCAPE_NON_ASCII);
  }
  return writer.writeValueAsString(o);
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

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

/**
 * Converts an object to JsonNode.
 *
 * @param data Value to convert in Json.
 * @return the JSON node.
 */
public static JsonNode toJson(final Object data) {
 try {
  return mapper().valueToTree(data);
 } catch(Exception e) {
  throw new RuntimeException(e);
 }
}

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

/**
 * Converts a JsonNode to a Java value
 *
 * @param <A> the type of the return value.
 * @param json Json value to convert.
 * @param clazz Expected Java value type.
 * @return the return value.
 */
public static <A> A fromJson(JsonNode json, Class<A> clazz) {
 try {
  return mapper().treeToValue(json, clazz);
 } catch(Exception e) {
  throw new RuntimeException(e);
 }
}

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

/**
 * Creates a new empty ArrayNode.
 * @return a new empty ArrayNode.
 */
public static ArrayNode newArray() {
 return mapper().createArrayNode();
}

代码示例来源:origin: org.deeplearning4j/deeplearning4j-play

private Result listSessions() {
  List<String> list = new ArrayList<>(knownSessionIDs.keySet());
  if (uploadedFileLines != null) {
    list.add(UPLOADED_FILE);
  }
  return Results.ok(Json.toJson(list));
}

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

return ((RawBuffer) body).asBytes();
} else if (body instanceof JsonNode) {
  return ByteString.fromString(Json.stringify((JsonNode) body));
} else if (body instanceof Document) {
  return XML.toBytes((Document) body);

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

/**
 * {@inheritDoc}
 */
@Override
public <U> void overwriteObjectByKey(final String key, final U object) {
  final JsonNode jsonNode = Json.toJson(object);
  final String valueAsJson = Json.stringify(jsonNode);
  overwriteValueByKey(key, valueAsJson);
}

代码示例来源: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;
      }
    }
  }
}

代码示例来源: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: com.typesafe.play/play

/**
 * Parses a InputStream representing a json, and return it as a JsonNode.
 * @param src    the JSON input stream.
 * @return the JSON node.
 */
public static JsonNode parse(java.io.InputStream src) {
 try {
  return mapper().readTree(src);
 } catch(Throwable t) {
  throw new RuntimeException(t);
 }
}

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

/**
 * Creates a new empty ObjectNode.
 * @return new empty ObjectNode.
 */
public static ObjectNode newObject() {
 return mapper().createObjectNode();
}

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

private static String generateJson(Object o, boolean prettyPrint, boolean escapeNonASCII) {
 try {
  ObjectWriter writer = mapper().writer();
  if (prettyPrint) {
   writer = writer.with(SerializationFeature.INDENT_OUTPUT);
  }
  if (escapeNonASCII) {
   writer = writer.with(Feature.ESCAPE_NON_ASCII);
  }
  return writer.writeValueAsString(o);
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

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

/**
 * Converts an object to JsonNode.
 *
 * @param data Value to convert in Json.
 * @return the JSON node.
 */
public static JsonNode toJson(final Object data) {
 try {
  return mapper().valueToTree(data);
 } catch(Exception e) {
  throw new RuntimeException(e);
 }
}

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

/**
 * Converts a JsonNode to a Java value
 *
 * @param <A> the type of the return value.
 * @param json Json value to convert.
 * @param clazz Expected Java value type.
 * @return the return value.
 */
public static <A> A fromJson(JsonNode json, Class<A> clazz) {
 try {
  return mapper().treeToValue(json, clazz);
 } catch(Exception e) {
  throw new RuntimeException(e);
 }
}

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

/**
 * Creates a new empty ArrayNode.
 * @return a new empty ArrayNode.
 */
public static ArrayNode newArray() {
 return mapper().createArrayNode();
}

相关文章