/** * Returns the JSON representation of the specified content. * * @return the {@link JsonNode} parsed from the content * * @throws IllegalStateException if the content is {@code null} * @throws JsonParseException if failed to parse the content as JSON */ default JsonNode contentAsJson() throws JsonParseException { final T content = content(); if (content instanceof JsonNode) { return (JsonNode) content; } return Jackson.readTree(contentAsText()); }
/** * Returns the textual representation of the specified content. * * @throws IllegalStateException if the content is {@code null} */ default String contentAsText() { final T content = content(); if (content instanceof JsonNode) { try { return Jackson.writeValueAsString(content); } catch (JsonProcessingException e) { // Should never happen because it's a JSON tree already. throw new Error(e); } } else { return content.toString(); } }
/** * Returns the JSON representation of the specified content. * * @return the {@link JsonNode} parsed from the content * * @throws IllegalStateException if the content is {@code null} * @throws JsonParseException if failed to parse the content as JSON */ default JsonNode contentAsJson() throws JsonParseException { final T content = content(); if (content instanceof JsonNode) { return (JsonNode) content; } return Jackson.readTree(contentAsText()); }
/** * Returns the textual representation of the specified content. * * @throws IllegalStateException if the content is {@code null} */ default String contentAsText() { final T content = content(); if (content instanceof JsonNode) { try { return Jackson.writeValueAsString(content); } catch (JsonProcessingException e) { // Should never happen because it's a JSON tree already. throw new Error(e); } } else { return content.toString(); } }
/** * Returns the value converted from the JSON representation of the specified content. * * @return the value converted from the content * * @throws IllegalStateException if the content is {@code null} * @throws JsonParseException if failed to parse the content as JSON * @throws JsonMappingException if failed to convert the parsed JSON into {@code valueType} */ default <U> U contentAsJson(Class<U> valueType) throws JsonParseException, JsonMappingException { final T content = content(); if (content instanceof TreeNode) { return Jackson.treeToValue((TreeNode) content, valueType); } return Jackson.readValue(contentAsText(), valueType); } }
/** * Returns the prettified textual representation of the specified content. Only a {@link TreeNode} is * prettified currently. * * @throws IllegalStateException if the content is {@code null} */ default String contentAsPrettyText() { final T content = content(); if (content instanceof TreeNode) { try { return Jackson.writeValueAsPrettyString(content); } catch (JsonProcessingException e) { // Should never happen because it's a JSON tree already. throw new Error(e); } } else { return content.toString(); } }
/** * Returns the value converted from the JSON representation of the specified content. * * @return the value converted from the content * * @throws IllegalStateException if the content is {@code null} * @throws JsonParseException if failed to parse the content as JSON * @throws JsonMappingException if failed to convert the parsed JSON into {@code valueType} */ default <U> U contentAsJson(Class<U> valueType) throws JsonParseException, JsonMappingException { final T content = content(); if (content instanceof TreeNode) { return Jackson.treeToValue((TreeNode) content, valueType); } return Jackson.readValue(contentAsText(), valueType); } }
/** * Returns the prettified textual representation of the specified content. Only a {@link TreeNode} is * prettified currently. * * @throws IllegalStateException if the content is {@code null} */ default String contentAsPrettyText() { final T content = content(); if (content instanceof TreeNode) { try { return Jackson.writeValueAsPrettyString(content); } catch (JsonProcessingException e) { // Should never happen because it's a JSON tree already. throw new Error(e); } } else { return content.toString(); } }