Package utils.common
Class JsonUtils
- java.lang.Object
-
- utils.common.JsonUtils
-
@Singleton public class JsonUtils extends java.lang.ObjectUtility class that handles everything around JSON, like marshaling and unmarshaling. Uses a custom JSON object mapper defined inJsonObjectMapper.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJsonUtils.JsonForApistatic classJsonUtils.JsonForIOHelper class for selectively marshaling an Object to JSON.static classJsonUtils.JsonForPublixHelper class for selectively marshaling an Object to JSON.
-
Constructor Summary
Constructors Constructor Description JsonUtils()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description com.fasterxml.jackson.databind.JsonNodeallBatchesByStudyForUI(java.util.List<Batch> batchList, java.util.List<java.lang.Integer> resultCountList, java.util.List<java.lang.Integer> groupCountList)Returns a JSON string of all batches that belong to the given study.com.fasterxml.jackson.databind.JsonNodeallComponentsForUI(java.util.List<Component> componentList, java.util.List<java.lang.Integer> resultCountList)Returns a JSON string of all components in the given list.com.fasterxml.jackson.databind.JsonNodeallGroupResults(java.util.List<GroupResult> groupResultList)Returns all GroupResults as a JSON string intended for GUI.static java.lang.StringasJson(java.lang.Object obj)Generic JSON marshaller.java.lang.StringasJsonForPublix(java.lang.Object obj)Marshalling an Object into an JSON string.static com.fasterxml.jackson.databind.JsonNodeasJsonNode(java.lang.Object obj)Generic JSON marshaller.com.fasterxml.jackson.databind.JsonNodeasJsonWithStrictViewInclusion(java.lang.Object obj)static com.fasterxml.jackson.databind.node.ObjectNodeasObjectNode(java.lang.Object obj)static java.lang.StringasStringForDB(java.lang.String json)Formats a JSON string into a minimized form suitable for storing into a DB.com.fasterxml.jackson.databind.JsonNodebatchAsJsonForApi(Batch batch)Returns JSON of a batch intended for the JATOS APIcom.fasterxml.jackson.databind.JsonNodecomponentAsJsonForApi(Component component)Returns JSON of a component intended for the JATOS APIcom.fasterxml.jackson.databind.JsonNodecomponentResultAsJsonNode(ComponentResult cr)Returns an ObjectNode of the given ComponentResult.com.fasterxml.jackson.databind.node.ObjectNodecomponentResultMetadata(ComponentResult cr)com.fasterxml.jackson.databind.node.ObjectNodegetBatchByStudyForUI(Batch batch, java.lang.Integer resultCount, java.lang.Integer groupCount)Returns a JSON string of one batch.com.fasterxml.jackson.databind.JsonNodegetComponentResultsByStudyResult(StudyResult studyResult)com.fasterxml.jackson.databind.node.ObjectNodegetSingleUserData(User user)com.fasterxml.jackson.databind.JsonNodegetStudyLinkData(StudyLink studyLink)com.fasterxml.jackson.databind.JsonNodeinitData(Batch batch, StudyResult studyResult, Study study, Component component)Returns init data that are requested during initialisation of each component run: Marshals the study properties and the component properties and puts them together with the session data (stored in StudyResult) into a new JSON object.static booleanisValid(java.lang.String json)Checks whether the given string is a valid JSON string.com.fasterxml.jackson.databind.JsonNodememberUserArrayOfStudy(java.util.List<User> userList, Study study)Returns JsonNode with all users of this study.com.fasterxml.jackson.databind.node.ObjectNodememberUserOfStudy(User user, Study study)Returns JsonNode with the given user.com.fasterxml.jackson.databind.JsonNodesidebarData(java.util.List<Study> studyList)Returns the JSON data for the sidebar (study title, ID and components)com.fasterxml.jackson.databind.JsonNodestudyAsJsonForApi(Study study, java.lang.Boolean withComponentProperties, java.lang.Boolean withBatchProperties)Returns JSON of a study intended for the JATOS APIvoidstudyAsJsonForIO(Study study, java.nio.file.Path file)Marshals the given study into JSON, adds the current study serial version, and saves it into the given File.com.fasterxml.jackson.databind.JsonNodestudyLinksData(java.util.List<StudyLink> studyLinkList)com.fasterxml.jackson.databind.JsonNodestudyLinksSetupData(Batch batch, java.util.Map<java.lang.String,java.lang.Integer> studyResultCountsPerWorker, java.lang.Integer personalSingleLinkCount, java.lang.Integer personalMultipleLinkCount)com.fasterxml.jackson.databind.node.ObjectNodestudyResultAsJsonNode(StudyResult sr, java.lang.Integer componentResultCount)Returns ObjectNode of the given StudyResult.com.fasterxml.jackson.databind.node.ObjectNodestudyResultMetadata(StudyResult sr)static com.fasterxml.jackson.databind.JsonNodewrapAsDataEnvelope(com.fasterxml.jackson.databind.JsonNode jsonNode, java.util.Map<java.lang.String,java.lang.Object> fields)Wraps the given JSON payload into a new top-level object and adds the provided metadata fields.
-
-
-
Field Detail
-
DATA
public static final java.lang.String DATA
- See Also:
- Constant Field Values
-
VERSION
public static final java.lang.String VERSION
- See Also:
- Constant Field Values
-
-
Method Detail
-
asJsonForPublix
public java.lang.String asJsonForPublix(java.lang.Object obj) throws com.fasterxml.jackson.core.JsonProcessingExceptionMarshalling an Object into an JSON string. It only considers fields that are annotated with 'JsonForPublix'.- Throws:
com.fasterxml.jackson.core.JsonProcessingException
-
asJsonWithStrictViewInclusion
public com.fasterxml.jackson.databind.JsonNode asJsonWithStrictViewInclusion(java.lang.Object obj) throws java.io.IOException- Throws:
java.io.IOException
-
asStringForDB
public static java.lang.String asStringForDB(java.lang.String json)
Formats a JSON string into a minimized form suitable for storing into a DB.
-
isValid
public static boolean isValid(java.lang.String json)
Checks whether the given string is a valid JSON string. An empty string, "hello", 123, true, null are all accepted as valid JSON.
-
initData
public com.fasterxml.jackson.databind.JsonNode initData(Batch batch, StudyResult studyResult, Study study, Component component) throws java.io.IOException
Returns init data that are requested during initialisation of each component run: Marshals the study properties and the component properties and puts them together with the session data (stored in StudyResult) into a new JSON object.- Throws:
java.io.IOException
-
allGroupResults
public com.fasterxml.jackson.databind.JsonNode allGroupResults(java.util.List<GroupResult> groupResultList)
Returns all GroupResults as a JSON string intended for GUI.
-
studyResultMetadata
public com.fasterxml.jackson.databind.node.ObjectNode studyResultMetadata(StudyResult sr) throws java.io.IOException
- Throws:
java.io.IOException
-
studyResultAsJsonNode
public com.fasterxml.jackson.databind.node.ObjectNode studyResultAsJsonNode(StudyResult sr, java.lang.Integer componentResultCount)
Returns ObjectNode of the given StudyResult. It contains the worker, study's ID and title
-
getComponentResultsByStudyResult
public com.fasterxml.jackson.databind.JsonNode getComponentResultsByStudyResult(StudyResult studyResult)
-
componentResultMetadata
public com.fasterxml.jackson.databind.node.ObjectNode componentResultMetadata(ComponentResult cr)
-
componentResultAsJsonNode
public com.fasterxml.jackson.databind.JsonNode componentResultAsJsonNode(ComponentResult cr)
Returns an ObjectNode of the given ComponentResult.
-
memberUserArrayOfStudy
public com.fasterxml.jackson.databind.JsonNode memberUserArrayOfStudy(java.util.List<User> userList, Study study)
Returns JsonNode with all users of this study. This JSON is intended for JATOS' GUI / in the change user modal.
-
memberUserOfStudy
public com.fasterxml.jackson.databind.node.ObjectNode memberUserOfStudy(User user, Study study)
Returns JsonNode with the given user. This JSON is intended for JATOS' GUI / in the change user modal.
-
getSingleUserData
public com.fasterxml.jackson.databind.node.ObjectNode getSingleUserData(User user)
-
sidebarData
public com.fasterxml.jackson.databind.JsonNode sidebarData(java.util.List<Study> studyList)
Returns the JSON data for the sidebar (study title, ID and components)
-
allBatchesByStudyForUI
public com.fasterxml.jackson.databind.JsonNode allBatchesByStudyForUI(java.util.List<Batch> batchList, java.util.List<java.lang.Integer> resultCountList, java.util.List<java.lang.Integer> groupCountList)
Returns a JSON string of all batches that belong to the given study. This includes the 'resultCount' (the number of StudyResults of this batch so far), 'workerCount' (number of workers without JatosWorkers), and the 'groupCount' (number of GroupResults of this batch so far). Intended for use in JATOS' GUI.
-
getBatchByStudyForUI
public com.fasterxml.jackson.databind.node.ObjectNode getBatchByStudyForUI(Batch batch, java.lang.Integer resultCount, java.lang.Integer groupCount)
Returns a JSON string of one batch. This includes the 'resultCount' (the number of StudyResults of this batch so far), 'workerCount' (number of workers without JatosWorkers), and the 'groupCount' (number of GroupResults of this batch so far). Intended for use in JATOS' GUI.
-
allComponentsForUI
public com.fasterxml.jackson.databind.JsonNode allComponentsForUI(java.util.List<Component> componentList, java.util.List<java.lang.Integer> resultCountList)
Returns a JSON string of all components in the given list. This includes the 'resultCount', the number of ComponentResults of this component so far. Intended for use in JATOS' GUI.
-
getStudyLinkData
public com.fasterxml.jackson.databind.JsonNode getStudyLinkData(StudyLink studyLink)
-
studyLinksSetupData
public com.fasterxml.jackson.databind.JsonNode studyLinksSetupData(Batch batch, java.util.Map<java.lang.String,java.lang.Integer> studyResultCountsPerWorker, java.lang.Integer personalSingleLinkCount, java.lang.Integer personalMultipleLinkCount)
-
studyLinksData
public com.fasterxml.jackson.databind.JsonNode studyLinksData(java.util.List<StudyLink> studyLinkList)
-
asJson
public static java.lang.String asJson(java.lang.Object obj)
Generic JSON marshaller.
-
asJsonNode
public static com.fasterxml.jackson.databind.JsonNode asJsonNode(java.lang.Object obj)
Generic JSON marshaller.
-
asObjectNode
public static com.fasterxml.jackson.databind.node.ObjectNode asObjectNode(java.lang.Object obj)
-
studyAsJsonForIO
public void studyAsJsonForIO(Study study, java.nio.file.Path file) throws java.io.IOException
Marshals the given study into JSON, adds the current study serial version, and saves it into the given File. It uses the view JsonForIO.- Throws:
java.io.IOException
-
studyAsJsonForApi
public com.fasterxml.jackson.databind.JsonNode studyAsJsonForApi(Study study, java.lang.Boolean withComponentProperties, java.lang.Boolean withBatchProperties) throws java.io.IOException
Returns JSON of a study intended for the JATOS API- Throws:
java.io.IOException
-
componentAsJsonForApi
public com.fasterxml.jackson.databind.JsonNode componentAsJsonForApi(Component component) throws java.io.IOException
Returns JSON of a component intended for the JATOS API- Throws:
java.io.IOException
-
batchAsJsonForApi
public com.fasterxml.jackson.databind.JsonNode batchAsJsonForApi(Batch batch) throws java.io.IOException
Returns JSON of a batch intended for the JATOS API- Throws:
java.io.IOException
-
wrapAsDataEnvelope
public static com.fasterxml.jackson.databind.JsonNode wrapAsDataEnvelope(com.fasterxml.jackson.databind.JsonNode jsonNode, java.util.Map<java.lang.String,java.lang.Object> fields)Wraps the given JSON payload into a new top-level object and adds the provided metadata fields. The resulting structure is an ObjectNode that contains all entries fromfieldsas top-level properties and a"data"property holding the originaljsonNodeunchanged. Example: wrapAsDataEnvelope({ "a": 1 }, Map.of("version", "1")) -> { "version": "1", "data": { "a": 1 } } Values are inserted usingputPOJO, allowing complex objects (e.g., Maps, Lists) to be serialized by Jackson as-is.- Parameters:
jsonNode- the JSON payload to be placed under the"data"key; may be any JsonNode (including null)fields- additional top-level properties (metadata) to include in the wrapper; keys must be non-null- Returns:
- a new ObjectNode containing the given
fieldsand thejsonNodeunder"data"
-
-