Package io.goobi.viewer.controller
Class JsonTools
java.lang.Object
io.goobi.viewer.controller.JsonTools
Utility class providing JSON serialisation and deserialisation helpers.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic org.json.JSONObjectcreateJsonObjectFromSolrDoc(org.apache.solr.common.SolrDocument doc, List<Map<String, String>> fields) static StringformatVersionString(String json) static Stringstatic org.json.JSONObjectstatic <T> TgetAsObject(String json, Class<T> clazz) static ObjectgetAsObjectForJson(Object value) static org.json.JSONArraygetDateCentricRecordJsonArray(org.apache.solr.common.SolrDocumentList result, HttpServletRequest request) JSON array of records grouped by their import date.static StringgetGitRevision(String json) static StringgetNestedValue(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.static org.json.JSONArraygetRecordJsonArray(org.apache.solr.common.SolrDocumentList result, Map<String, org.apache.solr.common.SolrDocumentList> expanded, HttpServletRequest request, String languageToTranslate) Returns aJSONArraycontaining JSON objects for everySolrDocumentin the given result.static org.json.JSONObjectgetRecordJsonObject(org.apache.solr.common.SolrDocument doc, String rootUrl, ThumbnailHandler thumbs) Creates a singleJSONObjectwith metadata for the given recordSolrDocument.static org.json.JSONObjectgetRecordJsonObject(org.apache.solr.common.SolrDocument doc, String rootUrl, String language, ThumbnailHandler thumbs) Creates a singleJSONObjectwith metadata for the given recordSolrDocument.static StringgetVersion(String json) static Stringstatic org.json.JSONObjecttranslateJSONObject(Locale locale, org.json.JSONObject object)
-
Field Details
-
KEY_MESSAGE
- See Also:
-
KEY_STATUS
- See Also:
-
-
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, ViewerConfigurationExceptionReturns aJSONArraycontaining JSON objects for everySolrDocumentin the given result. Order remains the same as in the result list.- Parameters:
result- Solr result list to serialize as JSONexpanded- map of expanded child documents keyed by PIrequest- HTTP request used for access permission checkslanguageToTranslate- 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 serializelocale- 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
- Type Parameters:
T- target type to deserialize into- Parameters:
json- JSON string to deserializeclazz- target class to deserialize into- Returns:
- T
- Throws:
IOException
-
getAsObjectForJson
- Parameters:
value- object to convert to a JSON-compatible type- Returns:
Object
-
translateJSONObject
- Parameters:
locale- locale to translate field names and string values toobject- 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 daterequest- 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 singleJSONObjectwith metadata for the given recordSolrDocument.- Parameters:
doc- Solr document containing record metadatarootUrl- base URL used to construct the record page URLthumbs- 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 singleJSONObjectwith metadata for the given recordSolrDocument.- Parameters:
doc- Solr document containing record metadatarootUrl- base URL used to construct the record page URLlanguage- language code for title field selectionthumbs- handler for generating thumbnail URLs- Returns:
- a JSON object containing metadata for the given Solr document
-
formatVersionString
- Parameters:
json- JSON string- Returns:
- Version information as a single line string
-
shortFormatVersionString
- Parameters:
json- JSON string- Returns:
- Only version number and git hash as a single line string
-
getVersion
- Parameters:
json- JSON version string from a component endpoint- Returns:
Stringcontaining value of "version" from json
-
getGitRevision
- Parameters:
json- JSON version string from a component endpoint- Returns:
Stringcontaining value of "git-revision" from json
-
getNestedValue
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 parsekey- 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 metadatafields- Field mappings- Returns:
JSONObject
-