Class JsonTools

java.lang.Object
io.goobi.viewer.controller.JsonTools

public final class JsonTools extends Object
Utility class providing JSON serialisation and deserialisation helpers.
  • Field Details

  • Method Details

    • getRecordJsonArray

      public static org.json.JSONArray getRecordJsonArray(org.apache.solr.common.SolrDocumentList result, Map<String,org.apache.solr.common.SolrDocumentList> expanded, HttpServletRequest request, String languageToTranslate) throws IndexUnreachableException, PresentationException, DAOException, ViewerConfigurationException
      Returns a JSONArray containing JSON objects for every SolrDocument in the given result. Order remains the same as in the result list.
      Parameters:
      result - Solr result list to serialize as JSON
      expanded - map of expanded child documents keyed by PI
      request - HTTP request used for access permission checks
      languageToTranslate - BCP 47 language tag for translating field names and values
      Returns:
      a JSON array of record objects built from the Solr result list
      Throws:
      IndexUnreachableException - if any.
      PresentationException - if any.
      DAOException - if any.
      ViewerConfigurationException - if any.
    • getAsJson

      public static org.json.JSONObject getAsJson(org.apache.solr.common.SolrDocument doc, Locale locale) throws com.fasterxml.jackson.core.JsonProcessingException
      Parameters:
      doc - Solr document to serialize
      locale - locale for translating field names and values, or null for no translation
      Returns:
      Given Solr doc as JSONObject
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException
    • getAsJson

      public static String getAsJson(Object object) throws com.fasterxml.jackson.core.JsonProcessingException
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException
    • getAsObject

      public static <T> T getAsObject(String json, Class<T> clazz) throws IOException
      Type Parameters:
      T - target type to deserialize into
      Parameters:
      json - JSON string to deserialize
      clazz - target class to deserialize into
      Returns:
      T
      Throws:
      IOException
    • getAsObjectForJson

      public static Object getAsObjectForJson(Object value)
      Parameters:
      value - object to convert to a JSON-compatible type
      Returns:
      Object
    • translateJSONObject

      public static org.json.JSONObject translateJSONObject(Locale locale, org.json.JSONObject object)
      Parameters:
      locale - locale to translate field names and string values to
      object - JSON object whose keys and string values to translate
      Returns:
      object translated to locale
    • getDateCentricRecordJsonArray

      public static org.json.JSONArray getDateCentricRecordJsonArray(org.apache.solr.common.SolrDocumentList result, HttpServletRequest request) throws IndexUnreachableException, PresentationException, DAOException, ViewerConfigurationException
      JSON array of records grouped by their import date.
      Parameters:
      result - Solr result list to group by import date
      request - HTTP request used for access permission checks
      Returns:
      a JSON array of record objects grouped by import date
      Throws:
      IndexUnreachableException - if any.
      PresentationException - if any.
      DAOException - if any.
      ViewerConfigurationException - if any.
    • getRecordJsonObject

      public static org.json.JSONObject getRecordJsonObject(org.apache.solr.common.SolrDocument doc, String rootUrl, ThumbnailHandler thumbs) throws ViewerConfigurationException
      Creates a single JSONObject with metadata for the given record SolrDocument.
      Parameters:
      doc - Solr document containing record metadata
      rootUrl - base URL used to construct the record page URL
      thumbs - handler for generating thumbnail URLs
      Returns:
      a JSON object containing metadata for the given Solr document
      Throws:
      ViewerConfigurationException - if any.
    • getRecordJsonObject

      public static org.json.JSONObject getRecordJsonObject(org.apache.solr.common.SolrDocument doc, String rootUrl, String language, ThumbnailHandler thumbs)
      Creates a single JSONObject with metadata for the given record SolrDocument.
      Parameters:
      doc - Solr document containing record metadata
      rootUrl - base URL used to construct the record page URL
      language - language code for title field selection
      thumbs - handler for generating thumbnail URLs
      Returns:
      a JSON object containing metadata for the given Solr document
    • formatVersionString

      public static String formatVersionString(String json)
      Parameters:
      json - JSON string
      Returns:
      Version information as a single line string
    • shortFormatVersionString

      public static String shortFormatVersionString(String json)
      Parameters:
      json - JSON string
      Returns:
      Only version number and git hash as a single line string
    • getVersion

      public static String getVersion(String json)
      Parameters:
      json - JSON version string from a component endpoint
      Returns:
      String containing value of "version" from json
    • getGitRevision

      public static String getGitRevision(String json)
      Parameters:
      json - JSON version string from a component endpoint
      Returns:
      String containing value of "git-revision" from json
    • getNestedValue

      public static String getNestedValue(org.json.JSONObject json, String key)
      Returns the string value of the given key in the json object or within another json object nested somewhere within the original object. If the key is not found anywhere within the object, an empty string is returned. If the value to the key is anything other than a string, a stringified version of the object is returned. This method cannot parse arrays within array. If the key is within that, an empty string is also returned
      Parameters:
      json - The json object to parse
      key - the key to find
      Returns:
      a String. May be empty if the key is not found
    • createJsonObjectFromSolrDoc

      public static org.json.JSONObject createJsonObjectFromSolrDoc(org.apache.solr.common.SolrDocument doc, List<Map<String,String>> fields)
      Parameters:
      doc - Solr doc containing the source metadata
      fields - Field mappings
      Returns:
      JSONObject