com.smartgwt.client.util
Class JSONEncoder

java.lang.Object
  extended by com.smartgwt.client.core.JsObject
      extended by com.smartgwt.client.core.DataClass
          extended by com.smartgwt.client.util.JSONEncoder

public class JSONEncoder
extends DataClass

Class for encoding objects as JSON strings.


Field Summary
 
Fields inherited from class com.smartgwt.client.core.JsObject
jsObj
 
Constructor Summary
JSONEncoder()
           
JSONEncoder(JavaScriptObject jsObj)
           
 
Method Summary
 String encode(Object object)
          Serialize an object as a JSON string.
 String encodeDate(Date theDate)
          Encode a JavaScript Date value.
 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.
static JSONEncoder getOrCreateRef(JavaScriptObject jsObj)
           
 Boolean getPrettyPrint()
          Whether to add indentation to the returned JSON string.
 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").
 void setCircularReferenceMarker(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 setPrettyPrint(Boolean prettyPrint)
          Whether to add indentation to the returned JSON string.
 void setStrictQuoting(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.DataClass
doAddHandler, fireEvent, getAttribute, getAttributeAsBoolean, getAttributeAsDate, getAttributeAsDouble, getAttributeAsFloat, getAttributeAsInt, getAttributeAsIntArray, getAttributeAsJavaScriptObject, getAttributeAsMap, getAttributeAsObject, getAttributeAsString, getAttributeAsStringArray, getAttributes, getHandlerCount, getJsObj, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute
 
Methods inherited from class com.smartgwt.client.core.JsObject
isCreated, setJsObj
 
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(JavaScriptObject jsObj)
Method Detail

getOrCreateRef

public static JSONEncoder getOrCreateRef(JavaScriptObject jsObj)

setDateFormat

public void setDateFormat(JSONDateFormat dateFormat)
Format for encoding JavaScript Date values in JSON. See JSONDateFormat for valid options, or override encodeDate(java.util.Date) to do something custom.

Parameters:
dateFormat - dateFormat Default value is "xmlSchema"

getDateFormat

public JSONDateFormat getDateFormat()
Format for encoding JavaScript Date values in JSON. See JSONDateFormat for valid options, or override encodeDate(java.util.Date) to do something custom.

Returns:
JSONDateFormat

setStrictQuoting

public void setStrictQuoting(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").

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 - strictQuoting Default value is true

getStrictQuoting

public 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:
Boolean

setCircularReferenceMode

public void setCircularReferenceMode(JSONCircularReferenceMode circularReferenceMode)
What the JSONEncoder should do if it encounters a circular reference.

Parameters:
circularReferenceMode - circularReferenceMode Default value is "path"

getCircularReferenceMode

public JSONCircularReferenceMode getCircularReferenceMode()
What the JSONEncoder should do if it encounters a circular reference.

Returns:
JSONCircularReferenceMode

setCircularReferenceMarker

public void setCircularReferenceMarker(String circularReferenceMarker)
The string marker used to represent circular references. See circularReferenceMode.

Parameters:
circularReferenceMarker - circularReferenceMarker Default value is "$$BACKREF$$"

getCircularReferenceMarker

public String getCircularReferenceMarker()
The string marker used to represent circular references. See circularReferenceMode.

Returns:
String

setPrettyPrint

public void setPrettyPrint(Boolean prettyPrint)
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 - prettyPrint Default value is true

getPrettyPrint

public 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:
Boolean

encode

public String encode(Object object)
Serialize an object as a JSON string.

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

encodeDate

public String encodeDate(Date theDate)
Encode a JavaScript Date value.

By default, follows the dateFormat setting. Override to do custom encoding.

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 (")