public class JSON
extends java.lang.Object
Constructor and Description |
---|
JSON() |
Modifier and Type | Method and Description |
---|---|
static com.google.gwt.core.client.JavaScriptObject |
decode(java.lang.String jsonString)
De-serialize an object from JSON.
|
static java.util.Map |
decodeSafe(java.lang.String jsonString,
Function jsonReviver)
Decodes strict JSON using native browser JSON parsing APIs.
|
static void |
decodeSafeWithDates(java.lang.String jsonString)
Uses
decodeSafe() to decode strict JSON using native browser JSON
parsing APIs, with settings to correctly process formatted date values created with JSONDateFormat logicalDateConstructor. |
static java.lang.String |
encode(com.google.gwt.core.client.JavaScriptObject object)
Serialize an object as a JSON string.
|
static java.lang.String |
encode(com.google.gwt.core.client.JavaScriptObject object,
JSONEncoder settings)
Serialize an object as a JSON string.
|
public static java.util.Map decodeSafe(java.lang.String jsonString, Function jsonReviver)
JSONDateFormat
logicalDateConstructor). This API is called "safe" because using a JavaScript eval() to decode JSON is potentially
unsafe if the data being decoded is untrusted. For example, if users are able to save data as JSON for other uses to
see (such as sharing ListGrid.viewState
) and there is no
validation of the saved data to ensure safety and some users are untrusted, then saved JSON could be used similarly to
an XSS attack, allowing one user to execute JavaScript code in another user's browser.
Note that, because JSON has
no way of representing dates, serializing a structure that contains dates and then deserializing with decodeSafe()
necessarily results in any Dates becoming strings. Use JSONDateFormat
"logicalDateString" in combination with decodeSafeWithDates()
to round-trip date, time and datetime values accurately.
jsonString
- JSON data to be de-serializedjsonReviver
- optional settingpublic static void decodeSafeWithDates(java.lang.String jsonString)
decodeSafe()
to decode strict JSON using native browser JSON
parsing APIs, with settings to correctly process formatted date values created with JSONDateFormat
logicalDateConstructor.jsonString
- JSON data to be de-serializedpublic static java.lang.String encode(com.google.gwt.core.client.JavaScriptObject object)
Automatically handles circular references - see circularReferenceMode
.
Note that using the String produced by this API with JSON.decode
will not
successfully preserve dates. Use JSONEncoder.setDateFormat
"dateConstructor" to have
dates round-trip properly.
Because GWT does not support Java reflection, JSON encoding cannot discover the properties of an arbitrary Java POJO. The following objects are supported:
object
- object to serializepublic static java.lang.String encode(com.google.gwt.core.client.JavaScriptObject object, JSONEncoder settings)
Automatically handles circular references - see circularReferenceMode
.
Note that using the String produced by this API with JSON.decode
will not
successfully preserve dates. Use JSONEncoder.setDateFormat
"dateConstructor" to have
dates round-trip properly.
Because GWT does not support Java reflection, JSON encoding cannot discover the properties of an arbitrary Java POJO. The following objects are supported:
object
- object to serializesettings
- optional settings for encodingpublic static com.google.gwt.core.client.JavaScriptObject decode(java.lang.String jsonString)
jsonString
- JSON data to be de-serialized