Class JsonUtils


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

      Fields 
      Modifier and Type Field Description
      static java.lang.String ACCEPT_HEADER
      An HTTP Accept header that prefers JSONLD.
      private static org.apache.http.impl.client.CloseableHttpClient DEFAULT_HTTP_CLIENT  
      private static com.fasterxml.jackson.core.JsonFactory JSON_FACTORY  
      private static com.fasterxml.jackson.databind.ObjectMapper JSON_MAPPER  
      static java.lang.String JSONLD_JAVA_USER_AGENT
      The user agent used by the default CloseableHttpClient.
      private static int MAX_LINKS_FOLLOW  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private JsonUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.net.URL alternateLink​(java.net.URL url, org.apache.http.client.methods.CloseableHttpResponse response)  
      static org.apache.http.impl.client.cache.CacheConfig createDefaultCacheConfig()  
      static org.apache.http.impl.client.CloseableHttpClient createDefaultHttpClient()  
      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 java.lang.Object fromInputStream​(java.io.InputStream input)
      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.
      static java.lang.Object fromInputStream​(java.io.InputStream input, java.lang.String enc)
      Parses a JSON-LD document from the given InputStream to an object that can be used as input for the JsonLdApi and JsonLdProcessor methods.
      static java.lang.Object fromInputStream​(java.io.InputStream input, java.nio.charset.Charset enc)
      Parses a JSON-LD document from the given InputStream to an object that can be used as input for the JsonLdApi and JsonLdProcessor methods.
      private static java.lang.Object fromJsonLdViaHttpUri​(java.net.URL url, org.apache.http.impl.client.CloseableHttpClient httpClient, int linksFollowed)  
      static java.lang.Object fromJsonParser​(com.fasterxml.jackson.core.JsonParser jp)
      Parses a JSON-LD document from the given JsonParser to an object that can be used as input for the JsonLdApi and JsonLdProcessor methods.
      static java.lang.Object fromReader​(java.io.Reader reader)
      Parses a JSON-LD document from the given Reader to an object that can be used as input for the JsonLdApi and JsonLdProcessor methods.
      static java.lang.Object fromString​(java.lang.String jsonString)
      Parses a JSON-LD document from a string to an object that can be used as input for the JsonLdApi and JsonLdProcessor methods.
      static java.lang.Object fromURL​(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 the JsonLdApi and JsonLdProcessor methods.
      static java.lang.Object fromURLJavaNet​(java.net.URL url)
      Fallback method directly using the HttpURLConnection class for cases where servers do not interoperate correctly with Apache HTTPClient.
      static org.apache.http.impl.client.CloseableHttpClient getDefaultHttpClient()  
      static java.lang.String toPrettyString​(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.String toString​(java.lang.Object jsonObject)
      Writes the given JSON-LD Object out to a String.
      static void write​(java.io.Writer writer, java.lang.Object jsonObject)
      Writes the given JSON-LD Object out to the given Writer.
      static void writePrettyPrint​(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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ACCEPT_HEADER

        public static final java.lang.String ACCEPT_HEADER
        An HTTP Accept header that prefers JSONLD.
        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
    • Constructor Detail

      • JsonUtils

        private JsonUtils()
    • Method Detail

      • fromInputStream

        public static java.lang.Object fromInputStream​(java.io.InputStream input)
                                                throws java.io.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.
        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.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.
        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.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.
        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.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.
        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.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.
        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.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.
        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.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.
        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.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.
        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.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.
        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.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.
        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.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.
        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.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.
        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)