Refine search
@Override protected JsonNode parse(Http.RequestHeader request, ByteString bytes) throws Exception { return play.libs.Json.parse(bytes.iterator().asInputStream()); } }
/** * 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(); }
private JsonNode error(JsonNode content) { ObjectNode result = Json.newObject(); result.set("error", content); return result; }
/** * Creates a new empty ObjectNode. * @return new empty ObjectNode. */ public static ObjectNode newObject() { return mapper().createObjectNode(); }
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); } }
/** * 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); } }
/** * 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); } }
/** * Creates a new empty ArrayNode. * @return a new empty ArrayNode. */ public static ArrayNode newArray() { return mapper().createArrayNode(); }
private Result listSessions() { List<String> list = new ArrayList<>(knownSessionIDs.keySet()); if (uploadedFileLines != null) { list.add(UPLOADED_FILE); } return Results.ok(Json.toJson(list)); }
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);
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; } } } }
/** * {@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(); } }); }
/** * Creates a new empty ObjectNode. * @return new empty ObjectNode. */ public static ObjectNode newObject() { return mapper().createObjectNode(); }
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); } }
/** * 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); } }
/** * 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); } }
/** * Creates a new empty ArrayNode. * @return a new empty ArrayNode. */ public static ArrayNode newArray() { return mapper().createArrayNode(); }