Class JsonUtils

java.lang.Object
com.github.jsonldjava.utils.JsonUtils

public class JsonUtils extends Object
Functions used to make loading, parsing, and serializing JSON easy using Jackson.
  • Field Details

    • ACCEPT_HEADER

      public static final String ACCEPT_HEADER
      An HTTP Accept header that prefers JSONLD.
      See Also:
    • JSONLD_JAVA_USER_AGENT

      public static final String JSONLD_JAVA_USER_AGENT
      The user agent used by the default CloseableHttpClient. This will not be used if DocumentLoader.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
  • Constructor Details

    • JsonUtils

      private JsonUtils()
  • Method Details

    • fromInputStream

      public static Object fromInputStream(InputStream input) throws IOException
      Parses a JSON-LD document from the given InputStream to an object that can be used as input for the JsonLdApi and JsonLdProcessor 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

      public static Object fromInputStream(InputStream input, String enc) throws IOException
      Parses a JSON-LD document from the given InputStream to an object that can be used as input for the JsonLdApi and JsonLdProcessor 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

      public static Object fromInputStream(InputStream input, Charset enc) throws IOException
      Parses a JSON-LD document from the given InputStream to an object that can be used as input for the JsonLdApi and JsonLdProcessor 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

      public static Object fromReader(Reader reader) throws IOException
      Parses a JSON-LD document from the given Reader to an object that can be used as input for the JsonLdApi and JsonLdProcessor 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

      public static Object fromJsonParser(com.fasterxml.jackson.core.JsonParser jp) throws IOException
      Parses a JSON-LD document from the given JsonParser to an object that can be used as input for the JsonLdApi and JsonLdProcessor methods.
      Parameters:
      jp - The JSON-LD document in a JsonParser.
      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 the JsonLdApi and JsonLdProcessor 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 the JsonLdApi and JsonLdProcessor methods.
      Parameters:
      url - The JsonLdUrl to resolve
      httpClient - The CloseableHttpClient 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 the HttpURLConnection 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)