Class JsonHelper


  • public class JsonHelper
    extends Object
    Convenience class to convert JSON strings to and from objects.
    Author:
    Igor Polevoy on 5/26/16.
    • Method Detail

      • toMap

        public static Map toMap​(String json)
        Convert a JSON map to a Java Map
        Parameters:
        json - JSON map
        Returns:
        Java Map.
      • toMaps

        public static Map[] toMaps​(String json)
        Convert JSON Array to Java array of maps.
        Parameters:
        json - JSON array
        Returns:
        Java array.
      • toJsonString

        public static String toJsonString​(Object val)
        Convert Java object to a JSON string.
        Parameters:
        val - Java object
        Returns:
        JSON string.
      • toJsonString

        public static String toJsonString​(Object val,
                                          boolean pretty)
        Convert Java object to a JSON string.
        Parameters:
        val - Java object
        pretty - enable/disable pretty print
        Returns:
        JSON string.
      • toJsonObject

        public static String toJsonObject​(Object... namesAndValues)
        Converts input into a JSON object.
        Parameters:
        namesAndValues - - expected sequence of corresponding name and value pairs (number of parameters must be even ).
        Returns:
        new string {name:value,name1:value1, etc.}
      • toList

        public static List toList​(String json)
        Convert JSON array tp Java List
        Parameters:
        json - JSON array string.
        Returns:
        Java List instance.
      • cleanControlChars

        public static String cleanControlChars​(String value)
        Clean control characters in a string.
        Parameters:
        value - string to escape
        Returns:
        escaped version
      • escapeControlChars

        public static String escapeControlChars​(String value)
        Escapes control characters in a string.
        Parameters:
        value - string to escape
        Returns:
        escaped version
        See Also:
        sanitize(String)
      • sanitize

        public static String sanitize​(String value)
        Escapes control characters in a string.
        Parameters:
        value - string to escape
        Returns:
        escaped version
        See Also:
        escapeControlChars(String)
      • sanitize

        public static String sanitize​(String value,
                                      boolean clean)
      • sanitize

        public static String sanitize​(String value,
                                      boolean clean,
                                      Character... toEscape)
        Escapes control characters in a string when you need to generate JSON.
        Parameters:
        value - input string
        clean - if true will remove characters that match, if false will escape
        toEscape - array of characters to escape. If not provided, it will escape or clean '"','\\', '\t', '\b', '\n', '\r' '\f'. This method will only escape or clean if provided chars are from this list.
        Returns:
        input string with control characters escaped or removed, depending on the clean flag.