Class JsonUtils


  • public class JsonUtils
    extends Object
    Contains utility methods used in this module.
    Author:
    mtaal
    • Constructor Detail

      • JsonUtils

        public JsonUtils()
    • Method Detail

      • getIdentifierSet

        public static List<Property> getIdentifierSet​(Property property)
        A utility method which retrieves the properties used for identifying an entity and its first level references. Takes into accoun the different scenarios implemented in the IdentifierProvider.getIdentifier(Object) method. Is used by distinct queries to get the smallest possible select clauses.
      • createDateFormat

        public static SimpleDateFormat createDateFormat()
        Returns:
        a new instance of the SimpleDateFormat using a format of yyyy-MM-dd (xml schema date format). The date format has lenient set to true.
      • createTimeFormat

        public static SimpleDateFormat createTimeFormat()
        Returns:
        a new instance of the SimpleDateFormat using a format of HH:MM:SS+0000. The date format has lenient set to true.
      • createTimeFormatWithoutGMTOffset

        public static SimpleDateFormat createTimeFormatWithoutGMTOffset()
        Returns:
        a new instance of the SimpleDateFormat using a format of HH:MM:SS. The date format has lenient set to true.
      • createJSTimeFormat

        public static SimpleDateFormat createJSTimeFormat()
        Returns:
        a new instance of the SimpleDateFormat using a format of yyyy-MM-dd'T'HH:mm:ss (see http://www.w3.org/TR/xmlschema-2/#dateTime). The date format has lenient set to true.
      • createDateTimeFormat

        public static SimpleDateFormat createDateTimeFormat()
        Note the formatted date string must be repaired in the timezone to follow the XSD format, see: convertToCorrectXSDFormat(String).
        Returns:
        a new instance of the SimpleDateFormat using a format of yyyy-MM-dd'T'HH:mm:ss (xml schema date time format). The date format has lenient set to true.
      • convertToCorrectXSDFormat

        public static String convertToCorrectXSDFormat​(String dateValue)
        Adds a colon in the timezone part of the formatted date, see here: http://weblogs.java.net/blog/felipegaucho/archive/2009/12/06/jaxb-customization-xsddatetime
        Parameters:
        dateValue - the date value without a :, for example 2009-12-06T15:59:34+0100
        Returns:
        a colon added in the timezone part: 2009-12-06T15:59:34+01:00
      • convertExceptionToJson

        public static String convertExceptionToJson​(Throwable throwable)
        Converts an exception to its json represention. Uses the Smartclient format for the json string, see here: RestDataSource
        Parameters:
        throwable - the exception to convert to json
        Returns:
        the resulting json string
      • isQueryTimeout

        public static boolean isQueryTimeout​(Throwable localThrowable)
      • getEmptyResult

        public static String getEmptyResult()
        Returns an empty result for a fetch call
        Returns:
        the JSON representation of an empty result
      • getPropertiesOnPath

        public static List<Property> getPropertiesOnPath​(Entity entity,
                                                         String propertyPath)
        Determines the list of properties based on the property path (for example bankAccount.bank.name).
        Parameters:
        entity - the entity to start from, the first property in the property path is a property of this entity
        propertyPath - the property path, i.e. property names separated by dots
        Returns:
        the list of properties determined from the property path
      • buildCriteria

        public static org.codehaus.jettison.json.JSONObject buildCriteria​(Map<String,​String> parameters)
      • isValueEmpty

        public static boolean isValueEmpty​(String value)
        Returns whether a JSON value is empty. The following values are considered as empty:
        • An empty string
        • null
        • "null" literal
        • "undefined" literal
      • merge

        public static void merge​(org.codehaus.jettison.json.JSONObject source,
                                 org.codehaus.jettison.json.JSONObject target)
                          throws org.codehaus.jettison.json.JSONException
        Merges a given JSONObject with a target JSONObject. If a property in the source object already exists in the target object, the following happens depending on the type of that property:
        - If it is a JSONObject, both source and target object are merged by calling this method recursively. If the property on the target object is not of type JSONObject, then a JSONException will be thrown.
        - If it is a Map, the entries of the map and the target property are merged by invoking merge(Map, JSONObject) if the target property is a JSONObject or by putting all the entries if the target property is a Map. If the property on the target object is neither of type JSONObject or Map, then a JSONException will be thrown.
        - It it is a JSONArray, both source and target arrays are merged by invoking merge(JSONArray, JSONArray). If the property on the target object is not of type JSONArray, then a JSONException will be thrown.
        - For the rest of the cases, the property is overridden in the target object. This method mutates the target object: after invoking this method it will contain the result of merging the properties of the two given objects.
        Parameters:
        source - The source object
        target - The object where the properties are merged. It is mutated by this method.
        Throws:
        org.codehaus.jettison.json.JSONException - in case the merge operation cannot be completed
      • merge

        public static void merge​(Map<String,​Object> sourceProperties,
                                 org.codehaus.jettison.json.JSONObject target)
                          throws org.codehaus.jettison.json.JSONException
        Merges the entries of a given Map with the properties of a target JSONObject. See merge(JSONObject, JSONObject)
        Throws:
        org.codehaus.jettison.json.JSONException
      • merge

        public static void merge​(org.codehaus.jettison.json.JSONArray source,
                                 org.codehaus.jettison.json.JSONArray target)
                          throws org.codehaus.jettison.json.JSONException
        Puts all the objects of a given JSONArray into a target JSONArray.
        Parameters:
        source - The source array
        target - The array where the elements of the source array are put. Note that this method modifies the contents of this array by adding on it the elements of the source array.
        Throws:
        org.codehaus.jettison.json.JSONException - in case the merge operation cannot be completed