Class JsonUtils
java.lang.Object
com.github.jsonldjava.utils.JsonUtils
Functions used to make loading, parsing, and serializing JSON easy using
Jackson.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringAn HTTP Accept header that prefers JSONLD.private static org.apache.http.impl.client.CloseableHttpClientprivate static final com.fasterxml.jackson.core.JsonFactoryprivate static final com.fasterxml.jackson.databind.ObjectMapperstatic final StringThe user agent used by the defaultCloseableHttpClient.private static final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static URLalternateLink(URL url, org.apache.http.client.methods.CloseableHttpResponse response) static org.apache.http.impl.client.cache.CacheConfigstatic org.apache.http.impl.client.CloseableHttpClientstatic 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 ObjectfromInputStream(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 ObjectfromInputStream(InputStream input, String enc) Parses a JSON-LD document from the givenInputStreamto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.static ObjectfromInputStream(InputStream input, Charset enc) Parses a JSON-LD document from the givenInputStreamto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.private static ObjectfromJsonLdViaHttpUri(URL url, org.apache.http.impl.client.CloseableHttpClient httpClient, int linksFollowed) static 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 ObjectfromReader(Reader reader) Parses a JSON-LD document from the givenReaderto an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.static ObjectfromString(String jsonString) Parses a JSON-LD document from a string to an object that can be used as input for theJsonLdApiandJsonLdProcessormethods.static ObjectParses 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 ObjectfromURLJavaNet(URL url) Fallback method directly using theHttpURLConnectionclass for cases where servers do not interoperate correctly with Apache HTTPClient.static org.apache.http.impl.client.CloseableHttpClientstatic StringtoPrettyString(Object jsonObject) Writes the given JSON-LD Object out to a String, using indentation and new lines to improve readability.static StringWrites the given JSON-LD Object out to a String.static voidWrites the given JSON-LD Object out to the given Writer.static voidwritePrettyPrint(Writer writer, Object jsonObject) Writes the given JSON-LD Object out to the given Writer, using indentation and new lines to improve readability.
-
Field Details
-
ACCEPT_HEADER
-
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:
-
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:
-
-
Constructor Details
-
JsonUtils
private JsonUtils()
-
-
Method Details
-
fromInputStream
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.- 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.IOException- If there was an IO error during parsing.
-
fromInputStream
Parses 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.IOException- If there was an IO error during parsing.
-
fromInputStream
Parses 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.IOException- If there was an IO error during parsing.
-
fromReader
Parses 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.IOException- If there was an IO error during parsing.
-
fromJsonParser
Parses 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.IOException- If there was an IO error during parsing.
-
fromString
public static Object fromString(String jsonString) throws com.fasterxml.jackson.core.JsonParseException, IOException Parses 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.IOException- If there was an IO error during parsing.
-
toPrettyString
public static String toPrettyString(Object jsonObject) throws com.fasterxml.jackson.core.JsonGenerationException, IOException Writes 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.IOException- If there is an IO error during serialization.
-
toString
public static String toString(Object jsonObject) throws com.fasterxml.jackson.core.JsonGenerationException, IOException Writes 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.IOException- If there is an IO error during serialization.
-
write
public static void write(Writer writer, Object jsonObject) throws com.fasterxml.jackson.core.JsonGenerationException, IOException Writes 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.IOException- If there is an IO error during serialization.
-
writePrettyPrint
public static void writePrettyPrint(Writer writer, Object jsonObject) throws com.fasterxml.jackson.core.JsonGenerationException, IOException Writes 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.IOException- If there is an IO error during serialization.
-
fromURL
public static Object fromURL(URL url, org.apache.http.impl.client.CloseableHttpClient httpClient) throws com.fasterxml.jackson.core.JsonParseException, IOException 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.- 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.IOException- If there was an IO error during parsing.
-
fromJsonLdViaHttpUri
private static Object fromJsonLdViaHttpUri(URL url, org.apache.http.impl.client.CloseableHttpClient httpClient, int linksFollowed) throws IOException - Throws:
IOException
-
alternateLink
private static URL alternateLink(URL url, org.apache.http.client.methods.CloseableHttpResponse response) throws MalformedURLException - Throws:
MalformedURLException
-
fromURLJavaNet
public static Object fromURLJavaNet(URL url) throws com.fasterxml.jackson.core.JsonParseException, IOException Fallback 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.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)
-