Package com.bandwidth

Class ApiHelper


  • public class ApiHelper
    extends Object
    This is a Helper class with commonly used utilities for the SDK.
    • Field Detail

      • mapper

        public static com.fasterxml.jackson.databind.ObjectMapper mapper
    • Constructor Detail

      • ApiHelper

        public ApiHelper()
    • Method Detail

      • serialize

        public static String serialize​(Object obj)
                                throws com.fasterxml.jackson.core.JsonProcessingException
        Json Serialization of a given object.
        Parameters:
        obj - The object to serialize into Json.
        Returns:
        The serialized Json String representation of the given object.
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - Signals that a Json Processing Exception has occurred.
      • serialize

        public static String serialize​(Object obj,
                                       com.fasterxml.jackson.databind.JsonSerializer serializer)
                                throws com.fasterxml.jackson.core.JsonProcessingException
        Json Serialization of a given object using a specified JsonSerializer.
        Parameters:
        obj - The object to serialize into Json.
        serializer - The instance of JsonSerializer to use.
        Returns:
        The serialized Json string representation of the given object.
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - Signals that a Json Processing Exception has occurred.
      • deserialize

        public static <T> List<T> deserialize​(String json,
                                              com.fasterxml.jackson.core.type.TypeReference<List<T>> typeReference,
                                              Class<T> cls,
                                              com.fasterxml.jackson.databind.JsonDeserializer<T> deserializer)
                                       throws IOException
        Json deserialization of the given Json string using a specified JsonDerializer.
        Type Parameters:
        T - The type of the object to deserialize into.
        Parameters:
        json - The Json string to deserialize.
        typeReference - TypeReference of list of T.
        cls - The class to attach the deserializer to.
        deserializer - The deserializer to use.
        Returns:
        The deserialized object.
        Throws:
        IOException - Signals if any I/O exception occured.
      • deserialize

        public static <T> T deserialize​(String json,
                                        Class<T> clazz)
                                 throws IOException
        Json deserialization of the given Json string.
        Type Parameters:
        T - The type of the object to deserialize into
        Parameters:
        json - The Json string to deserialize
        clazz - The type of the object to deserialize into
        Returns:
        The deserialized object
        Throws:
        IOException - Signals if any I/O exception occured.
      • deserialize

        public static LinkedHashMap<String,​Object> deserialize​(String json)
                                                              throws IOException
        Json deserialization of the given Json string.
        Parameters:
        json - The Json string to deserialize
        Returns:
        The deserialized Json as a Map
        Throws:
        IOException - Signals if any I/O exception occured.
      • deserialize

        public static <T> T deserialize​(String json,
                                        com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
                                 throws IOException
        JSON Deserialization of the given json string.
        Type Parameters:
        T - The type of the object to deserialize into
        Parameters:
        json - The json string to deserialize
        typeReference - TypeReference of T
        Returns:
        The deserialized object
        Throws:
        IOException - Signals if any I/O exception occured.
      • deserializeAsObject

        public static Object deserializeAsObject​(String json)
        Json deserialization of the given Json string.
        Parameters:
        json - The Json string to deserialize
        Returns:
        The deserialized Json as an Object
      • deserializeArray

        public static <T> List<T> deserializeArray​(String json,
                                                   Class<T[]> classArray)
                                            throws IOException
        JSON Deserialization of the given json string.
        Type Parameters:
        T - The type of the object to deserialize into
        Parameters:
        json - The Json string to deserialize
        classArray - The class of the array of objects to deserialize into
        Returns:
        The deserialized list of objects
        Throws:
        IOException - Signals if any I/O exception occured.
      • populate

        public static void populate​(String json,
                                    ApiException obj)
                             throws IOException
        Populates an object of an ApiException subclass with the required properties.
        Parameters:
        json - The Json string to deserialize
        obj - The object to populate.
        Throws:
        IOException - Signals if any I/O exception occured.
      • appendUrlWithTemplateParameters

        public static void appendUrlWithTemplateParameters​(StringBuilder queryBuilder,
                                                           Map<String,​AbstractMap.SimpleEntry<Object,​Boolean>> parameters)
        Replaces template parameters in the given URL.
        Parameters:
        queryBuilder - The query string builder to replace the template parameters
        parameters - The parameters to replace in the URL
      • appendUrlWithQueryParameters

        public static void appendUrlWithQueryParameters​(StringBuilder queryBuilder,
                                                        Map<String,​Object> parameters)
        Appends the given set of parameters to the given query string.
        Parameters:
        queryBuilder - The query URL string to append the parameters.
        parameters - The parameters to append.
      • isNullOrWhiteSpace

        public static boolean isNullOrWhiteSpace​(String s)
        Validates if the string is null, empty or whitespace.
        Parameters:
        s - The string to validate.
        Returns:
        The result of validation.
      • replaceAll

        public static void replaceAll​(StringBuilder stringBuilder,
                                      String toReplace,
                                      String replaceWith)
        Replaces all occurrences of the given string in the string builder.
        Parameters:
        stringBuilder - The string builder to update with replaced strings.
        toReplace - The string to replace in the string builder.
        replaceWith - The string to replace with.
      • removeNullValues

        public static void removeNullValues​(Map<String,​?> map)
        Removes null values from the given map.
        Parameters:
        map - Map of values.
      • cleanUrl

        public static String cleanUrl​(StringBuilder url)
        Validates and processes the given URL.
        Parameters:
        url - The given URL to process.
        Returns:
        Pre-process URL as string.
      • prepareFormFields

        public static List<AbstractMap.SimpleEntry<String,​Object>> prepareFormFields​(Map<?,​?> value)
        Prepares Array style form fields from a given array of values.
        Parameters:
        value - Value for the form fields.
        Returns:
        Dictionary of form fields created from array elements.