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 JSONMap toMap(String json)
      Convenience method to convert String to JSONMap.
      Parameters:
      json - String content of some JSON object.
      Returns:
      instance of JSONMap.
    • toList

      public static JSONList toList(String json)
      Convenience method to convert String to JSONList.
      Parameters:
      json - String content of some JSON array.
      Returns:
      instance of JSONList.
    • toJSON

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

      public static String toJSON(String name, Object value, Object... namesAndValues)
      Generates a JSON object from names and values. Example: this code:
      
           String person = toJSON("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
    • toPrettyJSON

      public static String toPrettyJSON(Object val)
      Convert Java object to a pretty JSON string.
      Parameters:
      val - Java object
      Returns:
      JSON string.
    • 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.
    • toObject

      public static <T> T toObject(String json, Class<T> hintClass)
      Converts JSON document to an object. The class of the object must provide a default constructor. This method can be used for platform-neutral serialization. This method can be used in the combination with the toJSON(Object) to serialize/deserialize objects.
      Parameters:
      json - document to use for de-serialization.
      Returns:
      an object serialized from the argument.