com.smartgwt.client.util

## Class JSONEncoder

- java.lang.Object
- - com.smartgwt.client.core.BaseClass
  - - com.smartgwt.client.util.JSONEncoder

- All Implemented Interfaces:
    com.google.gwt.event.shared.HasHandlers

      public class JSONEncoder
      extends BaseClass

  Class for encoding objects as JSON strings.

- - ### Field Summary

    - ### Fields inherited from class com.smartgwt.client.core.BaseClass

      `config, configOnly, factoryCreated, factoryProperties, id, scClassName`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `JSONEncoder()`
    `JSONEncoder(com.google.gwt.core.client.JavaScriptObject jsObj)`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `com.google.gwt.core.client.JavaScriptObject` | `create()`
    `static com.google.gwt.core.client.JavaScriptObject` | `decode(java.lang.String stringToJs)` Convert the passed string as Javascript
    `java.lang.String` | `encode(BaseClass object)` Serialize an object as a JSON string.
    `java.lang.String` | `encode(DataClass object)` Serialize an object as a JSON string.
    `java.lang.String` | `encode(java.lang.Object object)` Serialize an object as a JSON string.
    `java.lang.String` | `encodeDate(java.util.Date theDate)` Encode a JavaScript Date value.
    `java.lang.String` | `getCircularReferenceMarker()` The string marker used to represent circular references.
    `JSONCircularReferenceMode` | `getCircularReferenceMode()` What the JSONEncoder should do if it encounters a circular reference.
    `JSONDateFormat` | `getDateFormat()` Format for encoding JavaScript Date values in JSON.
    `java.lang.Boolean` | `getEscapeNonPrintable()` By default, obscure non-printable characters such as DC3 (Device Control 3, U+0013 hexadecimal) will be escaped according to JSON standards.
    `com.google.gwt.core.client.JavaScriptObject` | `getJsObj()`
    `com.google.gwt.core.client.JavaScriptObject` | `getOrCreateJsObj()`
    `static JSONEncoder` | `getOrCreateRef(com.google.gwt.core.client.JavaScriptObject jsObj)`
    `java.lang.Boolean` | `getPrettyPrint()` Whether to add indentation to the returned JSON string.
    `JSONInstanceSerializationMode` | `getSerializeInstances()` Controls the output of the JSONEncoder when instances of Smart GWT classes (eg a ListGrid) are included in the data to be serialized.
    `java.lang.Boolean` | `getShowDebugOutput()` If objects that cannot be serialized to JSON are encountered during serialization, show a placeholder rather than just omitting them.
    `java.lang.Boolean` | `getSkipInternalProperties()` If true, don't show Smart GWT internal properties when encoding and object.
    `java.lang.Boolean` | `getStrictQuoting()` Whether all property names should be quoted, or only those property names that are not valid identifiers or are JavaScript reserved words (such as "true").
    `boolean` | `isCreated()`
    `void` | `setCircularReferenceMarker(java.lang.String circularReferenceMarker)` The string marker used to represent circular references.
    `void` | `setCircularReferenceMode(JSONCircularReferenceMode circularReferenceMode)` What the JSONEncoder should do if it encounters a circular reference.
    `void` | `setDateFormat(JSONDateFormat dateFormat)` Format for encoding JavaScript Date values in JSON.
    `void` | `setEscapeNonPrintable(java.lang.Boolean escapeNonPrintable)` By default, obscure non-printable characters such as DC3 (Device Control 3, U+0013 hexadecimal) will be escaped according to JSON standards.
    `void` | `setJavaScriptObject(com.google.gwt.core.client.JavaScriptObject jsObj)`
    `void` | `setPrettyPrint(java.lang.Boolean prettyPrint)` Whether to add indentation to the returned JSON string.
    `void` | `setSerializeInstances(JSONInstanceSerializationMode serializeInstances)` Controls the output of the JSONEncoder when instances of Smart GWT classes (eg a ListGrid) are included in the data to be serialized.
    `void` | `setShowDebugOutput(java.lang.Boolean showDebugOutput)` If objects that cannot be serialized to JSON are encountered during serialization, show a placeholder rather than just omitting them.
    `void` | `setSkipInternalProperties(java.lang.Boolean skipInternalProperties)` If true, don't show Smart GWT internal properties when encoding and object.
    `void` | `setStrictQuoting(java.lang.Boolean strictQuoting)` Whether all property names should be quoted, or only those property names that are not valid identifiers or are JavaScript reserved words (such as "true").

    - ### Methods inherited from class com.smartgwt.client.core.BaseClass

      `applyFactoryProperties, asSGWTComponent, createJsObj, destroy, doAddHandler, doInit, error, error, errorIfNotCreated, fireEvent, getAttribute, getAttributeAsBoolean, getAttributeAsDate, getAttributeAsDouble, getAttributeAsElement, getAttributeAsFloat, getAttributeAsInt, getAttributeAsJavaScriptObject, getAttributeAsMap, getAttributeAsString, getAttributeAsStringArray, getClassName, getConfig, getHandlerCount, getID, getRef, getScClassName, getTestInstance, hasAutoAssignedID, internalSetID, internalSetID, isConfigOnly, isFactoryCreated, onBind, onInit, registerID, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setConfig, setConfigOnly, setFactoryCreated, setID, setProperty, setProperty, setProperty, setProperty, setScClassName`

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

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

- - ### Constructor Detail

    - #### JSONEncoder

          public JSONEncoder()

    - #### JSONEncoder

          public JSONEncoder(com.google.gwt.core.client.JavaScriptObject jsObj)

  - ### Method Detail

    - #### getOrCreateRef

          public static JSONEncoder getOrCreateRef(com.google.gwt.core.client.JavaScriptObject jsObj)

    - #### setJavaScriptObject

          public void setJavaScriptObject(com.google.gwt.core.client.JavaScriptObject jsObj)

      Overrides:
        `setJavaScriptObject` in class `BaseClass`

    - #### create

          public com.google.gwt.core.client.JavaScriptObject create()

      Specified by:
        `create` in class `BaseClass`

    - #### isCreated

          public boolean isCreated()

      Overrides:
        `isCreated` in class `BaseClass`

    - #### getJsObj

          public com.google.gwt.core.client.JavaScriptObject getJsObj()

      Overrides:
        `getJsObj` in class `BaseClass`

    - #### getOrCreateJsObj

          public com.google.gwt.core.client.JavaScriptObject getOrCreateJsObj()

      Overrides:
        `getOrCreateJsObj` in class `BaseClass`

    - #### setCircularReferenceMarker

          public void setCircularReferenceMarker(java.lang.String circularReferenceMarker)
                                          throws java.lang.IllegalStateException

      The string marker used to represent circular references. See `circularReferenceMode`.
      Parameters:
        `circularReferenceMarker` - New circularReferenceMarker value. Default value is "$$BACKREF$$"
      Throws:
        `java.lang.IllegalStateException` - this property cannot be changed after the underlying component has been created

    - #### getCircularReferenceMarker

          public java.lang.String getCircularReferenceMarker()

      The string marker used to represent circular references. See `circularReferenceMode`.
      Returns:
        Current circularReferenceMarker value. Default value is "$$BACKREF$$"

    - #### setCircularReferenceMode

          public void setCircularReferenceMode(JSONCircularReferenceMode circularReferenceMode)
                                        throws java.lang.IllegalStateException

      What the JSONEncoder should do if it encounters a circular reference.
      Parameters:
        `circularReferenceMode` - New circularReferenceMode value. Default value is "path"
      Throws:
        `java.lang.IllegalStateException` - this property cannot be changed after the underlying component has been created

    - #### getCircularReferenceMode

          public JSONCircularReferenceMode getCircularReferenceMode()

      What the JSONEncoder should do if it encounters a circular reference.
      Returns:
        Current circularReferenceMode value. Default value is "path"

    - #### setDateFormat

          public void setDateFormat(JSONDateFormat dateFormat)
                             throws java.lang.IllegalStateException

      Format for encoding JavaScript Date values in JSON. See `JSONDateFormat` for valid options, or override `encodeDate()` to do something custom.
      Parameters:
        `dateFormat` - New dateFormat value. Default value is "xmlSchema"
      Throws:
        `java.lang.IllegalStateException` - this property cannot be changed after the underlying component has been created

    - #### getDateFormat

          public JSONDateFormat getDateFormat()

      Format for encoding JavaScript Date values in JSON. See `JSONDateFormat` for valid options, or override `encodeDate()` to do something custom.
      Returns:
        Current dateFormat value. Default value is "xmlSchema"

    - #### setEscapeNonPrintable

          public void setEscapeNonPrintable(java.lang.Boolean escapeNonPrintable)

      By default, obscure non-printable characters such as DC3 (Device Control 3, U+0013 hexadecimal) will be escaped according to JSON standards. ECMA-404 / The JSON Data Interchange Format requires the quotation mark (U+0022), reverse solidus (U+005C), and control characters (U+0000 through U+001F) to be escaped.

      These characters are very rarely used in JSON data in web applications. If you know that your application does not use such characters in JSON data, there can be a performance advantage to setting `escapeNonPrintable` to false in order to disable the logic for escaping these characters. This is a detectable difference only when dealing with very large JSON structures on older browsers that do not provide native support (for example, Internet Explorer 8).

      Parameters:
        `escapeNonPrintable` - New escapeNonPrintable value. Default value is true

    - #### getEscapeNonPrintable

          public java.lang.Boolean getEscapeNonPrintable()

      By default, obscure non-printable characters such as DC3 (Device Control 3, U+0013 hexadecimal) will be escaped according to JSON standards. ECMA-404 / The JSON Data Interchange Format requires the quotation mark (U+0022), reverse solidus (U+005C), and control characters (U+0000 through U+001F) to be escaped.

      These characters are very rarely used in JSON data in web applications. If you know that your application does not use such characters in JSON data, there can be a performance advantage to setting `escapeNonPrintable` to false in order to disable the logic for escaping these characters. This is a detectable difference only when dealing with very large JSON structures on older browsers that do not provide native support (for example, Internet Explorer 8).

      Returns:
        Current escapeNonPrintable value. Default value is true

    - #### setPrettyPrint

          public void setPrettyPrint(java.lang.Boolean prettyPrint)
                              throws java.lang.IllegalStateException

      Whether to add indentation to the returned JSON string. This makes the returned JSON much easier to read but adds size. Note that when delivering JSON responses compressed, the size difference between prettyPrinted JSON and normal JSON is negligible.
      Parameters:
        `prettyPrint` - New prettyPrint value. Default value is true
      Throws:
        `java.lang.IllegalStateException` - this property cannot be changed after the underlying component has been created

    - #### getPrettyPrint

          public java.lang.Boolean getPrettyPrint()

      Whether to add indentation to the returned JSON string. This makes the returned JSON much easier to read but adds size. Note that when delivering JSON responses compressed, the size difference between prettyPrinted JSON and normal JSON is negligible.
      Returns:
        Current prettyPrint value. Default value is true

    - #### setSerializeInstances

          public void setSerializeInstances(JSONInstanceSerializationMode serializeInstances)
                                     throws java.lang.IllegalStateException

      Controls the output of the JSONEncoder when instances of Smart GWT classes (eg a ListGrid) are included in the data to be serialized. See `JSONInstanceSerializationMode`.

      Note that the JSONEncoder does not support a format that will recreate the instance if passed to decode() or eval().

      Parameters:
        `serializeInstances` - New serializeInstances value. Default value is "long"
      Throws:
        `java.lang.IllegalStateException` - this property cannot be changed after the underlying component has been created

    - #### getSerializeInstances

          public JSONInstanceSerializationMode getSerializeInstances()

      Controls the output of the JSONEncoder when instances of Smart GWT classes (eg a ListGrid) are included in the data to be serialized. See `JSONInstanceSerializationMode`.

      Note that the JSONEncoder does not support a format that will recreate the instance if passed to decode() or eval().

      Returns:
        Current serializeInstances value. Default value is "long"

    - #### setShowDebugOutput

          public void setShowDebugOutput(java.lang.Boolean showDebugOutput)
                                  throws java.lang.IllegalStateException

      If objects that cannot be serialized to JSON are encountered during serialization, show a placeholder rather than just omitting them.

      The resulting String will not be valid JSON and so cannot be decoded/eval()'d

      Parameters:
        `showDebugOutput` - New showDebugOutput value. Default value is false
      Throws:
        `java.lang.IllegalStateException` - this property cannot be changed after the underlying component has been created

    - #### getShowDebugOutput

          public java.lang.Boolean getShowDebugOutput()

      If objects that cannot be serialized to JSON are encountered during serialization, show a placeholder rather than just omitting them.

      The resulting String will not be valid JSON and so cannot be decoded/eval()'d

      Returns:
        Current showDebugOutput value. Default value is false

    - #### setSkipInternalProperties

          public void setSkipInternalProperties(java.lang.Boolean skipInternalProperties)
                                         throws java.lang.IllegalStateException

      If true, don't show Smart GWT internal properties when encoding and object.
      Parameters:
        `skipInternalProperties` - New skipInternalProperties value. Default value is false
      Throws:
        `java.lang.IllegalStateException` - this property cannot be changed after the underlying component has been created

    - #### getSkipInternalProperties

          public java.lang.Boolean getSkipInternalProperties()

      If true, don't show Smart GWT internal properties when encoding and object.
      Returns:
        Current skipInternalProperties value. Default value is false

    - #### setStrictQuoting

          public void setStrictQuoting(java.lang.Boolean strictQuoting)
                                throws java.lang.IllegalStateException

      Whether all property names should be quoted, or only those property names that are not valid identifiers or are JavaScript reserved words (such as "true").

      Encoding only where required produces slightly shorter, more readable output which is still compatible with JavaScript's eval():

            {
                someProp : "someValue",
                "true" : "otherValue",
                otherProp : "otherValue"
            }

      .. but is not understood by many server-side JSON parser implementations.
      Parameters:
        `strictQuoting` - New strictQuoting value. Default value is true
      Throws:
        `java.lang.IllegalStateException` - this property cannot be changed after the underlying component has been created

    - #### getStrictQuoting

          public java.lang.Boolean getStrictQuoting()

      Whether all property names should be quoted, or only those property names that are not valid identifiers or are JavaScript reserved words (such as "true").

      Encoding only where required produces slightly shorter, more readable output which is still compatible with JavaScript's eval():

            {
                someProp : "someValue",
                "true" : "otherValue",
                otherProp : "otherValue"
            }

      .. but is not understood by many server-side JSON parser implementations.
      Returns:
        Current strictQuoting value. Default value is true

    - #### encodeDate

          public java.lang.String encodeDate(java.util.Date theDate)

      Encode a JavaScript Date value.

      By default, follows the `dateFormat` setting. To override the date format, all Dates should be converted to Strings beforehand.

      Parameters:
        `theDate` - JavaScript date object to be serialized
      Returns:
        value to be included in result. If this value is intended to appear as a String it should include quotes (")

    - #### encode

          public java.lang.String encode(java.lang.Object object)

      Serialize an object as a JSON string.

      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

      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.

      Parameters:
        `object` - object to serialize
      Returns:
        object encoded as a JSON String

    - #### encode

          public java.lang.String encode(DataClass object)

      Serialize an object as a JSON string.

      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

      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.

      Parameters:
        `object` - object to serialize
      Returns:
        object encoded as a JSON String

    - #### encode

          public java.lang.String encode(BaseClass object)

      Serialize an object as a JSON string.

      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

      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.

      Parameters:
        `object` - object to serialize
      Returns:
        object encoded as a JSON String

    - #### decode

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

      Convert the passed string as Javascript
      Parameters:
        `stringToJs` - the string to convert
      Returns:
        the JavaScriptObject upon evaluation