com.smartgwt.client.util

## Class JSON

- java.lang.Object
- - com.smartgwt.client.util.JSON

-     public class JSON
      extends java.lang.Object

  Utilities for working with JSON data.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `JSON()`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    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 `JSONEncoder.dateFormat` "logicalDateString".
    `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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### JSON

          public JSON()

  - ### Method Detail

    - #### decodeSafe

          public static java.util.Map decodeSafe(java.lang.String jsonString,
                                                 Function jsonReviver)

      Decodes strict JSON using native browser JSON parsing APIs. This API will not work with pseudo-JSON that must be parsed as JavaScript (such as that produced by encoding with `JSONEncoder.dateFormat` "dateConstructor" or "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 `JSONEncoder.dateFormat` "logicalDateString" in combination with `decodeSafeWithDates()` to round-trip date, time and datetime values accurately.

      Parameters:
        `jsonString` - JSON data to be de-serialized
        `jsonReviver` - optional setting
      Returns:
        object derived from JSON String

    - #### decodeSafeWithDates

          public 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 `JSONEncoder.dateFormat` "logicalDateString".
      Parameters:
        `jsonString` - JSON data to be de-serialized

    - #### encode

          public static java.lang.String encode(com.google.gwt.core.client.JavaScriptObject object)

      Serialize an object as a JSON string.

      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:

      - any primitive type (String, Date, Number, Boolean)
      - any Map or Collection in any level of nesting
      - DataClass (Record's superclass) and RecordList
      - any widget (see +link{JSONEncoder.serializeInstances})
      - JavaScriptObject
      - an Array containing any of the above
      Parameters:
        `object` - object to serialize
      Returns:
        object encoded as a JSON String

    - #### encode

          public static java.lang.String encode(com.google.gwt.core.client.JavaScriptObject object,
                                                JSONEncoder settings)

      Serialize an object as a JSON string.

      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:

      - any primitive type (String, Date, Number, Boolean)
      - any Map or Collection in any level of nesting
      - DataClass (Record's superclass) and RecordList
      - any widget (see +link{JSONEncoder.serializeInstances})
      - JavaScriptObject
      - an Array containing any of the above
      Parameters:
        `object` - object to serialize
        `settings` - optional settings for encoding
      Returns:
        object encoded as a JSON String

    - #### decode

          public static com.google.gwt.core.client.JavaScriptObject decode(java.lang.String jsonString)

      De-serialize an object from JSON. Currently, this is simply a JavaScript eval() and should be used for trusted data only.
      Parameters:
        `jsonString` - JSON data to be de-serialized
      Returns:
        object derived from JSON String