Package com.github.jsonldjava.utils
Class JsonUtils
- java.lang.Object
-
- com.github.jsonldjava.utils.JsonUtils
-
public class JsonUtils extends java.lang.ObjectFunctions used to make loading, parsing, and serializing JSON easy using Jackson.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringACCEPT_HEADERAn HTTP Accept header that prefers JSONLD.private static org.apache.http.impl.client.CloseableHttpClientDEFAULT_HTTP_CLIENTprivate static com.fasterxml.jackson.core.JsonFactoryJSON_FACTORYprivate static com.fasterxml.jackson.databind.ObjectMapperJSON_MAPPERstatic java.lang.StringJSONLD_JAVA_USER_AGENTThe user agent used by the defaultCloseableHttpClient.private static intMAX_LINKS_FOLLOW
-
Constructor Summary
Constructors Modifier Constructor Description privateJsonUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static java.net.URLalternateLink(java.net.URL url, org.apache.http.client.methods.CloseableHttpResponse response)static org.apache.http.impl.client.cache.CacheConfigcreateDefaultCacheConfig()static org.apache.http.impl.client.CloseableHttpClientcreateDefaultHttpClient()static org.apache.http.impl.client.CloseableHttpClientcreateDefaultHttpClient(org.apache.http.impl.client.cache.CacheConfig cacheConfig)static org.apache.http.impl.client.HttpClientBuildercreateDefaultHttpClientBuilder(org.apache.http.impl.client.cache.CacheConfig cacheConfig)static java.lang.ObjectfromInputStream(java.io.InputStream input)Parses a JSON-LD document from the givenInputStreamto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.
Uses UTF-8 as the character encoding when decoding the InputStream.static java.lang.ObjectfromInputStream(java.io.InputStream input, java.lang.String enc)Parses a JSON-LD document from the givenInputStreamto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.static java.lang.ObjectfromInputStream(java.io.InputStream input, java.nio.charset.Charset enc)Parses a JSON-LD document from the givenInputStreamto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.private static java.lang.ObjectfromJsonLdViaHttpUri(java.net.URL url, org.apache.http.impl.client.CloseableHttpClient httpClient, int linksFollowed)static java.lang.ObjectfromJsonParser(com.fasterxml.jackson.core.JsonParser jp)Parses a JSON-LD document from the givenJsonParserto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.static java.lang.ObjectfromReader(java.io.Reader reader)Parses a JSON-LD document from the givenReaderto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.static java.lang.ObjectfromString(java.lang.String jsonString)Parses a JSON-LD document from a string to an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.static java.lang.ObjectfromURL(java.net.URL url, org.apache.http.impl.client.CloseableHttpClient httpClient)Parses a JSON-LD document, from the contents of the JSON resource resolved from the JsonLdUrl, to an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.static java.lang.ObjectfromURLJavaNet(java.net.URL url)Fallback method directly using theHttpURLConnectionclass for cases where servers do not interoperate correctly with Apache HTTPClient.static org.apache.http.impl.client.CloseableHttpClientgetDefaultHttpClient()static java.lang.StringtoPrettyString(java.lang.Object jsonObject)Writes the given JSON-LD Object out to a String, using indentation and new lines to improve readability.static java.lang.StringtoString(java.lang.Object jsonObject)Writes the given JSON-LD Object out to a String.static voidwrite(java.io.Writer writer, java.lang.Object jsonObject)Writes the given JSON-LD Object out to the given Writer.static voidwritePrettyPrint(java.io.Writer writer, java.lang.Object jsonObject)Writes the given JSON-LD Object out to the given Writer, using indentation and new lines to improve readability.
-
-
-
Field Detail
-
ACCEPT_HEADER
public static final java.lang.String ACCEPT_HEADER
An HTTP Accept header that prefers JSONLD.- See Also:
- Constant Field Values
-
JSONLD_JAVA_USER_AGENT
public static final java.lang.String JSONLD_JAVA_USER_AGENT
The user agent used by the defaultCloseableHttpClient. This will not be used ifDocumentLoader.setHttpClient(CloseableHttpClient)is called with a custom client.- See Also:
- Constant Field Values
-
JSON_MAPPER
private static final com.fasterxml.jackson.databind.ObjectMapper JSON_MAPPER
-
JSON_FACTORY
private static final com.fasterxml.jackson.core.JsonFactory JSON_FACTORY
-
DEFAULT_HTTP_CLIENT
private static volatile org.apache.http.impl.client.CloseableHttpClient DEFAULT_HTTP_CLIENT
-
MAX_LINKS_FOLLOW
private static final int MAX_LINKS_FOLLOW
- See Also:
- Constant Field Values
-
-
Method Detail
-
fromInputStream
public static java.lang.Object fromInputStream(java.io.InputStream input) throws java.io.IOExceptionParses a JSON-LD document from the givenInputStreamto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.
Uses UTF-8 as the character encoding when decoding the InputStream.- Parameters:
input- The JSON-LD document in an InputStream.- Returns:
- A JSON Object.
- Throws:
com.fasterxml.jackson.core.JsonParseException- If there was a JSON related error during parsing.java.io.IOException- If there was an IO error during parsing.
-
fromInputStream
public static java.lang.Object fromInputStream(java.io.InputStream input, java.lang.String enc) throws java.io.IOExceptionParses a JSON-LD document from the givenInputStreamto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.- Parameters:
input- The JSON-LD document in an InputStream.enc- The character encoding to use when interpreting the characters in the InputStream.- Returns:
- A JSON Object.
- Throws:
com.fasterxml.jackson.core.JsonParseException- If there was a JSON related error during parsing.java.io.IOException- If there was an IO error during parsing.
-
fromInputStream
public static java.lang.Object fromInputStream(java.io.InputStream input, java.nio.charset.Charset enc) throws java.io.IOExceptionParses a JSON-LD document from the givenInputStreamto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.- Parameters:
input- The JSON-LD document in an InputStream.enc- The character encoding to use when interpreting the characters in the InputStream.- Returns:
- A JSON Object.
- Throws:
com.fasterxml.jackson.core.JsonParseException- If there was a JSON related error during parsing.java.io.IOException- If there was an IO error during parsing.
-
fromReader
public static java.lang.Object fromReader(java.io.Reader reader) throws java.io.IOExceptionParses a JSON-LD document from the givenReaderto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.- Parameters:
reader- The JSON-LD document in a Reader.- Returns:
- A JSON Object.
- Throws:
com.fasterxml.jackson.core.JsonParseException- If there was a JSON related error during parsing.java.io.IOException- If there was an IO error during parsing.
-
fromJsonParser
public static java.lang.Object fromJsonParser(com.fasterxml.jackson.core.JsonParser jp) throws java.io.IOExceptionParses a JSON-LD document from the givenJsonParserto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.- Parameters:
jp- The JSON-LD document in aJsonParser.- Returns:
- A JSON Object.
- Throws:
com.fasterxml.jackson.core.JsonParseException- If there was a JSON related error during parsing.java.io.IOException- If there was an IO error during parsing.
-
fromString
public static java.lang.Object fromString(java.lang.String jsonString) throws com.fasterxml.jackson.core.JsonParseException, java.io.IOExceptionParses a JSON-LD document from a string to an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.- Parameters:
jsonString- The JSON-LD document as a string.- Returns:
- A JSON Object.
- Throws:
com.fasterxml.jackson.core.JsonParseException- If there was a JSON related error during parsing.java.io.IOException- If there was an IO error during parsing.
-
toPrettyString
public static java.lang.String toPrettyString(java.lang.Object jsonObject) throws com.fasterxml.jackson.core.JsonGenerationException, java.io.IOExceptionWrites the given JSON-LD Object out to a String, using indentation and new lines to improve readability.- Parameters:
jsonObject- The JSON-LD Object to serialize.- Returns:
- A JSON document serialised to a String.
- Throws:
com.fasterxml.jackson.core.JsonGenerationException- If there is a JSON error during serialization.java.io.IOException- If there is an IO error during serialization.
-
toString
public static java.lang.String toString(java.lang.Object jsonObject) throws com.fasterxml.jackson.core.JsonGenerationException, java.io.IOExceptionWrites the given JSON-LD Object out to a String.- Parameters:
jsonObject- The JSON-LD Object to serialize.- Returns:
- A JSON document serialised to a String.
- Throws:
com.fasterxml.jackson.core.JsonGenerationException- If there is a JSON error during serialization.java.io.IOException- If there is an IO error during serialization.
-
write
public static void write(java.io.Writer writer, java.lang.Object jsonObject) throws com.fasterxml.jackson.core.JsonGenerationException, java.io.IOExceptionWrites the given JSON-LD Object out to the given Writer.- Parameters:
writer- The writer that is to receive the serialized JSON-LD object.jsonObject- The JSON-LD Object to serialize.- Throws:
com.fasterxml.jackson.core.JsonGenerationException- If there is a JSON error during serialization.java.io.IOException- If there is an IO error during serialization.
-
writePrettyPrint
public static void writePrettyPrint(java.io.Writer writer, java.lang.Object jsonObject) throws com.fasterxml.jackson.core.JsonGenerationException, java.io.IOExceptionWrites the given JSON-LD Object out to the given Writer, using indentation and new lines to improve readability.- Parameters:
writer- The writer that is to receive the serialized JSON-LD object.jsonObject- The JSON-LD Object to serialize.- Throws:
com.fasterxml.jackson.core.JsonGenerationException- If there is a JSON error during serialization.java.io.IOException- If there is an IO error during serialization.
-
fromURL
public static java.lang.Object fromURL(java.net.URL url, org.apache.http.impl.client.CloseableHttpClient httpClient) throws com.fasterxml.jackson.core.JsonParseException, java.io.IOExceptionParses a JSON-LD document, from the contents of the JSON resource resolved from the JsonLdUrl, to an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.- Parameters:
url- The JsonLdUrl to resolvehttpClient- TheCloseableHttpClientto use to resolve the URL.- Returns:
- A JSON Object.
- Throws:
com.fasterxml.jackson.core.JsonParseException- If there was a JSON related error during parsing.java.io.IOException- If there was an IO error during parsing.
-
fromJsonLdViaHttpUri
private static java.lang.Object fromJsonLdViaHttpUri(java.net.URL url, org.apache.http.impl.client.CloseableHttpClient httpClient, int linksFollowed) throws java.io.IOException- Throws:
java.io.IOException
-
alternateLink
private static java.net.URL alternateLink(java.net.URL url, org.apache.http.client.methods.CloseableHttpResponse response) throws java.net.MalformedURLException- Throws:
java.net.MalformedURLException
-
fromURLJavaNet
public static java.lang.Object fromURLJavaNet(java.net.URL url) throws com.fasterxml.jackson.core.JsonParseException, java.io.IOExceptionFallback method directly using theHttpURLConnectionclass for cases where servers do not interoperate correctly with Apache HTTPClient.- Parameters:
url- The URL to access.- Returns:
- The result, after conversion from JSON to a Java Object.
- Throws:
com.fasterxml.jackson.core.JsonParseException- If there was a JSON related error during parsing.java.io.IOException- If there was an IO error during parsing.
-
getDefaultHttpClient
public static org.apache.http.impl.client.CloseableHttpClient getDefaultHttpClient()
-
createDefaultHttpClient
public static org.apache.http.impl.client.CloseableHttpClient createDefaultHttpClient()
-
createDefaultCacheConfig
public static org.apache.http.impl.client.cache.CacheConfig createDefaultCacheConfig()
-
createDefaultHttpClient
public static org.apache.http.impl.client.CloseableHttpClient createDefaultHttpClient(org.apache.http.impl.client.cache.CacheConfig cacheConfig)
-
createDefaultHttpClientBuilder
public static org.apache.http.impl.client.HttpClientBuilder createDefaultHttpClientBuilder(org.apache.http.impl.client.cache.CacheConfig cacheConfig)
-
-