Class JSONHelper

java.lang.Object
org.javalite.json.JSONHelper
Direct Known Subclasses:
JsonHelper

public class JSONHelper extends Object
Convenience class to convert JSON strings to and from objects.
Author:
Igor Polevoy on 5/26/16.
  • Constructor Details

    • JSONHelper

      public JSONHelper()
  • Method Details

    • toMap

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

      public static JSONMap toJSONMap(String json)
      Convenience method to convert String to JSONMap.
      Parameters:
      json - String content of some JSON object.
      Returns:
      instance of JSONMap.
    • toJSONList

      public static JSONList toJSONList(String json)
      Convenience method to convert String to JSONList.
      Parameters:
      json - String content of some JSON array.
      Returns:
      instance of JSONList.
    • 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... namesAndValues)
      Generates a JSON object from names and values. Example: this code:
      
           String person = toJsonString("first_name", "Marilyn", "last_name", "Monroe");
       
      will generate this JSON string:
      
           {
               "first_name": "Marilyn",
               "last_name": "Monroe"
           }
       
      Parameters:
      namesAndValues - is a list of name and value pairs in a typical JavaLite fashion.
      Returns:
      JSON object with name and values passed in.
    • toJson

      public static String toJson(Object... namesAndValues)
      Generates a JSON object from names and values. Example: this code:
      
           String person = toJsonString("first_name", "Marilyn", "last_name", "Monroe");
       
      will generate this JSON string:
      
           {
               "first_name": "Marilyn",
               "last_name": "Monroe"
           }
       
      This method is a synonym for toJsonString(Object...).
      Parameters:
      namesAndValues - is a list of name and value pairs in a typical JavaLite fashion.
      Returns:
      JSON object with name and values passed in
    • 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.