Class JsonHelper


  • public class JsonHelper
    extends Object
    Helper class for JSON.
    Author:
    FracPete (fracpete at waikato dot ac dot nz)
    • Constructor Detail

      • JsonHelper

        public JsonHelper()
    • Method Detail

      • parse

        public static Object parse​(Reader reader)
                            throws Exception
        Parses the JSON via a Reader.
        Parameters:
        reader - the reader to use
        Returns:
        the parsed JSON
        Throws:
        Exception - if parsing fails
      • parse

        public static Object parse​(String json,
                                   LoggingSupporter logging)
        Parses the JSON String.
        Parameters:
        json - the string to parse
        logging - for logging error messages, uses stderr if null
        Returns:
        the object, null if failed to parse
      • parse

        public static Object parse​(File file,
                                   LoggingSupporter logging)
        Parses the JSON file.
        Parameters:
        file - the file to parse
        logging - for logging error messages, uses stderr if null
        Returns:
        the object, null if failed to parse
      • prettyPrint

        public static String prettyPrint​(net.minidev.json.JSONAware json)
        Generates pretty printed JSON.
        Parameters:
        json - the JSON element to convert
        Returns:
        the pretty string
      • prettyPrint

        public static String prettyPrint​(com.google.gson.JsonElement json)
        Generates pretty printed JSON.
        Parameters:
        json - the JSON element to convert
        Returns:
        the pretty string
      • prettyPrint

        public static String prettyPrint​(String json)
        Generates pretty printed JSON.
        Parameters:
        json - the JSON string to convert
        Returns:
        the pretty string
      • toJSON

        public static Object toJSON​(Object value)
        Turns the object into a JSON object, if necessary.
        Parameters:
        value - the value associated with the key
      • toArray

        public static Object[] toArray​(net.minidev.json.JSONArray array)
        Turns the JSON array into an object array.
        Parameters:
        array - the array to convert
        Returns:
        the generated array
      • toList

        public static List<Object> toList​(net.minidev.json.JSONArray array)
        Turns the JSON array into an object list.
        Parameters:
        array - the array to convert
        Returns:
        the generated list
      • toMap

        public static Map<String,​Object> toMap​(net.minidev.json.JSONObject object,
                                                     boolean arraysAsList)
        Turns the JSON object into a map.
        Parameters:
        object - the object to convert
        Returns:
        the generated map