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
-
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
Generates a JSON object from names and values.static JSONList
toJSONList(String json)
Convenience method to convert String toJSONList
.static JSONMap
Convenience method to convert String toJSONMap
.static String
toJsonObject(Object... namesAndValues)
Converts input into a JSON object.static String
toJsonString(Object val)
Convert Java object to a JSON string.static String
toJsonString(Object... namesAndValues)
Generates a JSON object from names and values.static String
toJsonString(Object val, boolean pretty)
Convert Java object to a JSON string.static List
Convert JSON array tp Java Liststatic Map
Convert a JSON map to a Java Mapstatic Map[]
Convert JSON Array to Java array of maps.
-
Constructor Details
-
JSONHelper
public JSONHelper()
-
-
Method Details
-
toMap
Convert a JSON map to a Java Map- Parameters:
json
- JSON map- Returns:
- Java Map.
-
toJSONMap
Convenience method to convert String toJSONMap
.- Parameters:
json
- String content of some JSON object.- Returns:
- instance of
JSONMap
.
-
toJSONList
Convenience method to convert String toJSONList
.- Parameters:
json
- String content of some JSON array.- Returns:
- instance of
JSONList
.
-
toMaps
Convert JSON Array to Java array of maps.- Parameters:
json
- JSON array- Returns:
- Java array.
-
toJsonString
Convert Java object to a JSON string.- Parameters:
val
- Java object- Returns:
- JSON string.
-
toJsonString
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
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 fortoJsonString(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
Convert Java object to a JSON string.- Parameters:
val
- Java objectpretty
- enable/disable pretty print- Returns:
- JSON string.
-
toJsonObject
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
Convert JSON array tp Java List- Parameters:
json
- JSON array string.- Returns:
- Java List instance.
-
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.
-