Package utils.common

Class JsonUtils


  • @Singleton
    public class JsonUtils
    extends java.lang.Object
    Utility class that handles everything around JSON, like marshaling and unmarshaling. Uses a custom JSON object mapper defined in JsonObjectMapper.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DATA  
      static java.lang.String VERSION  
    • 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.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.
      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.
      com.fasterxml.jackson.databind.JsonNode allGroupResults​(java.util.List<GroupResult> groupResultList)
      Returns all GroupResults as a JSON string intended for GUI.
      static java.lang.String asJson​(java.lang.Object obj)
      Generic JSON marshaller.
      java.lang.String asJsonForPublix​(java.lang.Object obj)
      Marshalling an Object into an JSON string.
      static com.fasterxml.jackson.databind.JsonNode asJsonNode​(java.lang.Object obj)
      Generic JSON marshaller.
      com.fasterxml.jackson.databind.JsonNode asJsonWithStrictViewInclusion​(java.lang.Object obj)  
      static com.fasterxml.jackson.databind.node.ObjectNode asObjectNode​(java.lang.Object obj)  
      static java.lang.String asStringForDB​(java.lang.String json)
      Formats a JSON string into a minimized form suitable for storing into a DB.
      com.fasterxml.jackson.databind.JsonNode batchAsJsonForApi​(Batch batch)
      Returns JSON of a batch intended for the JATOS API
      com.fasterxml.jackson.databind.JsonNode componentAsJsonForApi​(Component component)
      Returns JSON of a component intended for the JATOS API
      com.fasterxml.jackson.databind.JsonNode componentResultAsJsonNode​(ComponentResult cr)
      Returns an ObjectNode of the given ComponentResult.
      com.fasterxml.jackson.databind.node.ObjectNode componentResultMetadata​(ComponentResult cr)  
      com.fasterxml.jackson.databind.node.ObjectNode getBatchByStudyForUI​(Batch batch, java.lang.Integer resultCount, java.lang.Integer groupCount)
      Returns a JSON string of one batch.
      com.fasterxml.jackson.databind.JsonNode getComponentResultsByStudyResult​(StudyResult studyResult)  
      com.fasterxml.jackson.databind.node.ObjectNode getSingleUserData​(User user)  
      com.fasterxml.jackson.databind.JsonNode getStudyLinkData​(StudyLink studyLink)  
      com.fasterxml.jackson.databind.JsonNode initData​(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 boolean isValid​(java.lang.String json)
      Checks whether the given string is a valid JSON string.
      com.fasterxml.jackson.databind.JsonNode memberUserArrayOfStudy​(java.util.List<User> userList, Study study)
      Returns JsonNode with all users of this study.
      com.fasterxml.jackson.databind.node.ObjectNode memberUserOfStudy​(User user, Study study)
      Returns JsonNode with the given user.
      com.fasterxml.jackson.databind.JsonNode sidebarData​(java.util.List<Study> studyList)
      Returns the JSON data for the sidebar (study title, ID and components)
      com.fasterxml.jackson.databind.JsonNode studyAsJsonForApi​(Study study, java.lang.Boolean withComponentProperties, java.lang.Boolean withBatchProperties)
      Returns JSON of a study intended for the JATOS API
      void studyAsJsonForIO​(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.JsonNode studyLinksData​(java.util.List<StudyLink> studyLinkList)  
      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)  
      com.fasterxml.jackson.databind.node.ObjectNode studyResultAsJsonNode​(StudyResult sr, java.lang.Integer componentResultCount)
      Returns ObjectNode of the given StudyResult.
      com.fasterxml.jackson.databind.node.ObjectNode studyResultMetadata​(StudyResult sr)  
      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.
      • Methods inherited from class java.lang.Object

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

      • JsonUtils

        public JsonUtils()
    • Method Detail

      • asJsonForPublix

        public java.lang.String asJsonForPublix​(java.lang.Object obj)
                                         throws com.fasterxml.jackson.core.JsonProcessingException
        Marshalling 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 from fields as top-level properties and a "data" property holding the original jsonNode unchanged. Example: wrapAsDataEnvelope({ "a": 1 }, Map.of("version", "1")) -> { "version": "1", "data": { "a": 1 } } Values are inserted using putPOJO, 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 fields and the jsonNode under "data"