Class JsonUtil


  • public class JsonUtil
    extends Object
    Various utility methods for processing JSON.
    Author:
    jdlee, jcoustick
    • Field Detail

      • CONFIDENTIAL_PROPERTY_UNSET

        public static final String CONFIDENTIAL_PROPERTY_UNSET
    • Method Detail

      • getJsonValue

        public static jakarta.json.JsonValue getJsonValue​(Object object)
                                                   throws jakarta.json.JsonException
        Converts an object to a JsonValue

        The object must be one of JsonValue, Collection, Map, ResponseBody, String, Integer, Long, Double, Boolean, BigInteger, BigDecimal, a class that has a REST model or an array of one of the above.

        Parameters:
        object - The object to convert
        Returns:
        The resulting JsonValue
        Throws:
        jakarta.json.JsonException - If the object cannot be converted to a JsonValue
      • getJsonValue

        public static jakarta.json.JsonValue getJsonValue​(Object object,
                                                          boolean hideConfidentialProperties)
                                                   throws jakarta.json.JsonException
        Converts an object to a JsonValue

        The object must be one of JsonValue, Collection, Map, ResponseBody, String, Integer, Long, Double, Boolean, BigInteger, BigDecimal, a class that has a REST model or an array of one of the above.

        Parameters:
        object - The object to convert
        hideConfidentialProperties -
        Returns:
        resulting JsonValue
        Throws:
        jakarta.json.JsonException - If the object cannot be converted to a JsonValue
      • getJsonObject

        @Deprecated
        public static jakarta.json.JsonValue getJsonObject​(Object object,
                                                           boolean hideConfidentialProperties)
                                                    throws jakarta.json.JsonException
        Deprecated.
        As of 5.0, replaced by getJsonValue(Object) as a more accurately named method with the removal of Jettison the return value is no longer JSONObject but JsonValue.
        Converts an object to a JsonValue

        The object must be one of JsonValue, Collection, Map, ResponseBody, String, Integer, Long, Double, Boolean, BigInteger, BigDecimal, a class that has a REST model or an array of one of the above.

        Parameters:
        object - The object to convert
        hideConfidentialProperties -
        Returns:
        Throws:
        jakarta.json.JsonException - If the object cannot be converted to a JsonValue
      • getJsonForRestModel

        public static jakarta.json.JsonObject getJsonForRestModel​(RestModel model,
                                                                  boolean hideConfidentialProperties)
      • isConfidentialProperty

        public static boolean isConfidentialProperty​(Class clazz,
                                                     String getterMethodName)
      • processCollection

        public static jakarta.json.JsonArray processCollection​(Collection c)
                                                        throws jakarta.json.JsonException
        Converts a Collection of JsonValues or other Json-compatible types into a JsonAray
        Parameters:
        c -
        Returns:
        Throws:
        jakarta.json.JsonException
      • processMap

        public static jakarta.json.JsonObject processMap​(Map map)
                                                  throws jakarta.json.JsonException
        Throws:
        jakarta.json.JsonException
      • getString

        public static String getString​(jakarta.json.JsonObject jsonObject,
                                       String key,
                                       String dflt)
        Gets a String from a JsonObject
        Parameters:
        jsonObject -
        key -
        dflt - returned if there is no mapping for the key
        Returns:
      • getInt

        public static int getInt​(jakarta.json.JsonObject jsonObject,
                                 String key,
                                 int dflt)
        Gets an integer from a JsonObject
        Parameters:
        jsonObject -
        key -
        dflt - returned if there is no mapping for the key
        Returns:
      • accumalate

        public static jakarta.json.JsonObject accumalate​(jakarta.json.JsonObject jsonObject,
                                                         String key,
                                                         jakarta.json.JsonValue value)
        Puts a value into a JsonObject with the specified key.

        If the key already exists then a JsonArray is used to store all the values. This differs to Map.put(Object, Object) as put replaces the value if the key already exists.

        Parameters:
        jsonObject -
        key -
        value -
        Returns:
        Since:
        5.0
      • jsonArraytoArray

        public static List<Object> jsonArraytoArray​(jakarta.json.JsonArray array)
        Converts a JsonArray to a List<Object> where the objects are all the standard java types represented by the values in the array
        Parameters:
        array - The JsonArray to convert to List
        Returns:
        Since:
        5.0
        See Also:
        jsonValueToRaw(jakarta.json.JsonValue)
      • jsonValueToRaw

        public static Object jsonValueToRaw​(jakarta.json.JsonValue value)
        Converts a JsonValue to the java standard type it represents.

        JsonArray -> List<Object>
        JsonObject -> Map<String, Object>
        JsonString -> String
        JsonNumber -> BigDecimal
        JsonValue.TRUE -> Boolean.TRUE
        JsonValue.FALSE -> Boolean.FALSE
        JsonValue.NULL -> null

        Parameters:
        value -
        Returns:
        The java standard type represented by the JsonValue
        Since:
        5.0