Package com.github.jsonldjava.utils
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 String
An HTTP Accept header that prefers JSONLD.private static org.apache.http.impl.client.CloseableHttpClient
private static final com.fasterxml.jackson.core.JsonFactory
private static final com.fasterxml.jackson.databind.ObjectMapper
static final String
The user agent used by the defaultCloseableHttpClient
.private static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static URL
alternateLink
(URL url, org.apache.http.client.methods.CloseableHttpResponse response) static org.apache.http.impl.client.cache.CacheConfig
static org.apache.http.impl.client.CloseableHttpClient
static org.apache.http.impl.client.CloseableHttpClient
createDefaultHttpClient
(org.apache.http.impl.client.cache.CacheConfig cacheConfig) static org.apache.http.impl.client.HttpClientBuilder
createDefaultHttpClientBuilder
(org.apache.http.impl.client.cache.CacheConfig cacheConfig) static Object
fromInputStream
(InputStream input) Parses a JSON-LD document from the givenInputStream
to an object that can be used as input for theJsonLdApi
andJsonLdProcessor
methods.
Uses UTF-8 as the character encoding when decoding the InputStream.static Object
fromInputStream
(InputStream input, String enc) Parses a JSON-LD document from the givenInputStream
to an object that can be used as input for theJsonLdApi
andJsonLdProcessor
methods.static Object
fromInputStream
(InputStream input, Charset enc) Parses a JSON-LD document from the givenInputStream
to an object that can be used as input for theJsonLdApi
andJsonLdProcessor
methods.private static Object
fromJsonLdViaHttpUri
(URL url, org.apache.http.impl.client.CloseableHttpClient httpClient, int linksFollowed) static Object
fromJsonParser
(com.fasterxml.jackson.core.JsonParser jp) Parses a JSON-LD document from the givenJsonParser
to an object that can be used as input for theJsonLdApi
andJsonLdProcessor
methods.static Object
fromReader
(Reader reader) Parses a JSON-LD document from the givenReader
to an object that can be used as input for theJsonLdApi
andJsonLdProcessor
methods.static Object
fromString
(String jsonString) Parses a JSON-LD document from a string to an object that can be used as input for theJsonLdApi
andJsonLdProcessor
methods.static Object
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 theJsonLdApi
andJsonLdProcessor
methods.static Object
fromURLJavaNet
(URL url) Fallback method directly using theHttpURLConnection
class for cases where servers do not interoperate correctly with Apache HTTPClient.static org.apache.http.impl.client.CloseableHttpClient
static String
toPrettyString
(Object jsonObject) Writes the given JSON-LD Object out to a String, using indentation and new lines to improve readability.static String
Writes the given JSON-LD Object out to a String.static void
Writes the given JSON-LD Object out to the given Writer.static void
writePrettyPrint
(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
An HTTP Accept header that prefers JSONLD.- See Also:
-
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 givenInputStream
to an object that can be used as input for theJsonLdApi
andJsonLdProcessor
methods.
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 givenInputStream
to an object that can be used as input for theJsonLdApi
andJsonLdProcessor
methods.- 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 givenInputStream
to an object that can be used as input for theJsonLdApi
andJsonLdProcessor
methods.- 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 givenReader
to an object that can be used as input for theJsonLdApi
andJsonLdProcessor
methods.- 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 givenJsonParser
to an object that can be used as input for theJsonLdApi
andJsonLdProcessor
methods.- 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 theJsonLdApi
andJsonLdProcessor
methods.- 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 theJsonLdApi
andJsonLdProcessor
methods.- Parameters:
url
- The JsonLdUrl to resolvehttpClient
- TheCloseableHttpClient
to 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 theHttpURLConnection
class 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)
-