Package org.javalite.json
Class JSONHelper
java.lang.Object
org.javalite.json.JSONHelper
- Direct Known Subclasses:
JsonHelper
Convenience class to convert JSON strings to and from objects.
- Author:
- Igor Polevoy on 5/26/16.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
cleanControlChars(String value)
Clean control characters in a string.static String
escapeControlChars(String value)
Escapes control characters in a string.static String
Escapes control characters in a string.static String
static String
Escapes control characters in a string when you need to generate JSON.static String
Convert Java object to a JSON string.static String
Generates a JSON object from names and values.static JSONList
Convenience method to convert String toJSONList
.static JSONMap
Convenience method to convert String toJSONMap
.static <T> T
Converts JSON document to an object.static String
toPrettyJSON(Object val)
Convert Java object to a pretty JSON string.
-
Constructor Details
-
JSONHelper
public JSONHelper()
-
-
Method Details
-
toMap
Convenience method to convert String toJSONMap
.- Parameters:
json
- String content of some JSON object.- Returns:
- instance of
JSONMap
.
-
toList
Convenience method to convert String toJSONList
.- Parameters:
json
- String content of some JSON array.- Returns:
- instance of
JSONList
.
-
toJSON
Convert Java object to a JSON string.- Parameters:
val
- Java object- Returns:
- JSON string.
-
toJSON
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
Convert Java object to a pretty JSON string.- Parameters:
val
- Java object- Returns:
- JSON string.
-
cleanControlChars
Clean control characters in a string.- Parameters:
value
- string to escape- Returns:
- escaped version
-
escapeControlChars
Escapes control characters in a string.- Parameters:
value
- string to escape- Returns:
- escaped version
- See Also:
sanitize(String)
-
sanitize
Escapes control characters in a string.- Parameters:
value
- string to escape- Returns:
- escaped version
- See Also:
escapeControlChars(String)
-
sanitize
-
sanitize
Escapes control characters in a string when you need to generate JSON.- Parameters:
value
- input stringclean
- if true will remove characters that match, if false will escapetoEscape
- 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
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 thetoJSON(Object)
to serialize/deserialize objects.- Parameters:
json
- document to use for de-serialization.- Returns:
- an object serialized from the argument.
-