Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.struts2.json
Class JSONResult

    java.lang.Object
      org.apache.struts2.json.JSONResult

All Implemented Interfaces:
  com.opensymphony.xwork2.Result, Serializable

    public class JSONResult

extends Object
implements com.opensymphony.xwork2.Result

This result serializes an action into JSON.

Result parameters:

- excludeProperties - list of regular expressions matching the properties to be excluded. The regular expressions are evaluated against the OGNL expression representation of the properties.

Example:

     <!-- START SNIPPET: example -->
     <result name="success" type="json" />
     <!-- END SNIPPET: example -->

See Also:
  Serialized Form

Field Summary
`static String` | `DEFAULT_PARAM` This result type doesn't have a default param, null is ok to reduce noise in logs

Constructor Summary
`JSONResult()`

Method Summary
`protected String` | `addCallbackIfApplicable(javax.servlet.http.HttpServletRequest request, String json)`
`protected SMD` | `buildSMDObject(com.opensymphony.xwork2.ActionInvocation invocation)`
`protected String` | `createJSONString(javax.servlet.http.HttpServletRequest request, Object rootObject)`
`protected boolean` | `enableGzip(javax.servlet.http.HttpServletRequest request)`
`void` | `execute(com.opensymphony.xwork2.ActionInvocation invocation)`
`protected Object` | `findRootObject(com.opensymphony.xwork2.ActionInvocation invocation)`
`String` | `getCallbackParameter()`
`String` | `getDefaultDateFormat()`
`protected String` | `getEncoding()` Retrieve the encoding
`List<Pattern>` | `getExcludePropertiesList()` Gets a list of regular expressions of properties to exclude from the JSON output.
`List<Pattern>` | `getIncludePropertiesList()`
`String` | `getRoot()`
`String` | `getWrapPrefix()`
`String` | `getWrapSuffix()`
`boolean` | `isEnableGZIP()`
`boolean` | `isEnableSMD()`
`boolean` | `isEnumAsBean()`
`boolean` | `isExcludeNullProperties()`
`boolean` | `isIgnoreHierarchy()`
`boolean` | `isNoCache()`
`boolean` | `isWrapWithComments()`
`protected Object` | `readRootObject(com.opensymphony.xwork2.ActionInvocation invocation)`
`void` | `setCallbackParameter(String callbackParameter)`
`void` | `setContentType(String contentType)` Content type to be set in the response
`void` | `setDefaultDateFormat(String defaultDateFormat)`
`void` | `setDefaultEncoding(String val)`
`void` | `setEnableGZIP(boolean enableGZIP)`
`void` | `setEnableSMD(boolean enableSMD)` Enable SMD generation for action, which can be used for JSON-RPC
`void` | `setEncoding(String encoding)` If defined will be used instead of `defaultEncoding`, you can define it with result <result name="success" type="json"> <param name="encoding">UTF-8</param> </result>
`void` | `setEnumAsBean(boolean enumAsBean)` Controls how Enum's are serialized : If true, an Enum is serialized as a name=value pair (name=name()) (default) If false, an Enum is serialized as a bean with a special property _name=name()
`void` | `setErrorCode(int errorCode)` Error code to be set in the response
`void` | `setExcludeNullProperties(boolean excludeNullProperties)` Do not serialize properties with a null value
`void` | `setExcludeProperties(String commaDelim)` Sets a comma-delimited list of regular expressions to match properties that should be excluded from the JSON output.
`void` | `setExcludeWildcards(String commaDelim)` Sets a comma-delimited list of wildcard expressions to match properties that should be excluded from the JSON output.
`void` | `setIgnoreHierarchy(boolean ignoreHierarchy)`
`void` | `setIgnoreInterfaces(boolean ignoreInterfaces)` Controls whether interfaces should be inspected for method annotations You may need to set to this true if your action is a proxy as annotations on methods are not inherited
`void` | `setIncludeProperties(String commaDelim)` Sets a comma-delimited list of regular expressions to match properties that should be included in the JSON output.
`void` | `setIncludeWildcards(String commaDelim)` Sets a comma-delimited list of wildcard expressions to match properties that should be included in the JSON output.
`void` | `setNoCache(boolean noCache)` Add headers to response to prevent the browser from caching the response
`void` | `setPrefix(boolean prefix)` Prefix JSON with "{} &&"
`void` | `setRoot(String root)` Sets the root object to be serialized, defaults to the Action
`void` | `setStatusCode(int statusCode)` Status code to be set in the response
`void` | `setWrapPrefix(String wrapPrefix)` Text to be inserted at the begining of the response
`void` | `setWrapSuffix(String wrapSuffix)` Text to be inserted at the end of the response
`void` | `setWrapWithComments(boolean wrapWithComments)` Wrap generated JSON with comments
`protected void` | `writeToResponse(javax.servlet.http.HttpServletResponse response, String json, boolean gzip)`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### DEFAULT_PARAM

    public static final String DEFAULT_PARAM

  This result type doesn't have a default param, null is ok to reduce noise in logs

Constructor Detail

### JSONResult

    public JSONResult()

Method Detail

### setDefaultEncoding

    public void setDefaultEncoding(String val)

### getExcludePropertiesList

    public List<Pattern> getExcludePropertiesList()

  Gets a list of regular expressions of properties to exclude from the JSON output.

  Returns:
    A list of compiled regular expression patterns

### setExcludeProperties

    public void setExcludeProperties(String commaDelim)

  Sets a comma-delimited list of regular expressions to match properties that should be excluded from the JSON output.

  Parameters:
    `commaDelim` - A comma-delimited list of regular expressions

### setExcludeWildcards

    public void setExcludeWildcards(String commaDelim)

  Sets a comma-delimited list of wildcard expressions to match properties that should be excluded from the JSON output.

  Parameters:
    `commaDelim` - A comma-delimited list of wildcard patterns

### getIncludePropertiesList

    public List<Pattern> getIncludePropertiesList()

  Returns:
    the includeProperties

### setIncludeProperties

    public void setIncludeProperties(String commaDelim)

  Sets a comma-delimited list of regular expressions to match properties that should be included in the JSON output.

  Parameters:
    `commaDelim` - A comma-delimited list of regular expressions

### setIncludeWildcards

    public void setIncludeWildcards(String commaDelim)

  Sets a comma-delimited list of wildcard expressions to match properties that should be included in the JSON output.

  Parameters:
    `commaDelim` - A comma-delimited list of wildcard patterns

### execute

    public void execute(com.opensymphony.xwork2.ActionInvocation invocation)
                 throws Exception

  Specified by:
    `execute` in interface `com.opensymphony.xwork2.Result`
  Throws:
    `Exception`

### readRootObject

    protected Object readRootObject(com.opensymphony.xwork2.ActionInvocation invocation)

### findRootObject

    protected Object findRootObject(com.opensymphony.xwork2.ActionInvocation invocation)

### createJSONString

    protected String createJSONString(javax.servlet.http.HttpServletRequest request,
                                      Object rootObject)
                               throws JSONException

  Throws:
    `JSONException`

### enableGzip

    protected boolean enableGzip(javax.servlet.http.HttpServletRequest request)

### writeToResponse

    protected void writeToResponse(javax.servlet.http.HttpServletResponse response,
                                   String json,
                                   boolean gzip)
                            throws IOException

  Throws:
    `IOException`

### buildSMDObject

    protected SMD buildSMDObject(com.opensymphony.xwork2.ActionInvocation invocation)

### getEncoding

    protected String getEncoding()

  Retrieve the encoding

  Returns:
    The encoding associated with this template (defaults to the value of param 'encoding', if empty default to 'struts.i18n.encoding' property)

### addCallbackIfApplicable

    protected String addCallbackIfApplicable(javax.servlet.http.HttpServletRequest request,
                                             String json)

### getRoot

    public String getRoot()

  Returns:
    OGNL expression of root object to be serialized

### setRoot

    public void setRoot(String root)

  Sets the root object to be serialized, defaults to the Action

  Parameters:
    `root` - OGNL expression of root object to be serialized

### isWrapWithComments

    public boolean isWrapWithComments()

  Returns:
    Generated JSON must be enclosed in comments

### setWrapWithComments

    public void setWrapWithComments(boolean wrapWithComments)

  Wrap generated JSON with comments

  Parameters:
    `wrapWithComments` -

### isEnableSMD

    public boolean isEnableSMD()

  Returns:
    Result has SMD generation enabled

### setEnableSMD

    public void setEnableSMD(boolean enableSMD)

  Enable SMD generation for action, which can be used for JSON-RPC

  Parameters:
    `enableSMD` -

### setIgnoreHierarchy

    public void setIgnoreHierarchy(boolean ignoreHierarchy)

### setIgnoreInterfaces

    public void setIgnoreInterfaces(boolean ignoreInterfaces)

  Controls whether interfaces should be inspected for method annotations You may need to set to this true if your action is a proxy as annotations on methods are not inherited

### setEnumAsBean

    public void setEnumAsBean(boolean enumAsBean)

  Controls how Enum's are serialized : If true, an Enum is serialized as a name=value pair (name=name()) (default) If false, an Enum is serialized as a bean with a special property _name=name()

  Parameters:
    `enumAsBean` -

### isEnumAsBean

    public boolean isEnumAsBean()

### isEnableGZIP

    public boolean isEnableGZIP()

### setEnableGZIP

    public void setEnableGZIP(boolean enableGZIP)

### isNoCache

    public boolean isNoCache()

### setNoCache

    public void setNoCache(boolean noCache)

  Add headers to response to prevent the browser from caching the response

  Parameters:
    `noCache` -

### isIgnoreHierarchy

    public boolean isIgnoreHierarchy()

### isExcludeNullProperties

    public boolean isExcludeNullProperties()

### setExcludeNullProperties

    public void setExcludeNullProperties(boolean excludeNullProperties)

  Do not serialize properties with a null value

  Parameters:
    `excludeNullProperties` -

### setStatusCode

    public void setStatusCode(int statusCode)

  Status code to be set in the response

  Parameters:
    `statusCode` -

### setErrorCode

    public void setErrorCode(int errorCode)

  Error code to be set in the response

  Parameters:
    `errorCode` -

### setCallbackParameter

    public void setCallbackParameter(String callbackParameter)

### getCallbackParameter

    public String getCallbackParameter()

### setPrefix

    public void setPrefix(boolean prefix)

  Prefix JSON with "{} &&"

  Parameters:
    `prefix` -

### setContentType

    public void setContentType(String contentType)

  Content type to be set in the response

  Parameters:
    `contentType` -

### getWrapPrefix

    public String getWrapPrefix()

### setWrapPrefix

    public void setWrapPrefix(String wrapPrefix)

  Text to be inserted at the begining of the response

### getWrapSuffix

    public String getWrapSuffix()

### setWrapSuffix

    public void setWrapSuffix(String wrapSuffix)

  Text to be inserted at the end of the response

### setEncoding

    public void setEncoding(String encoding)

  If defined will be used instead of `defaultEncoding`, you can define it with result <result name="success" type="json"> <param name="encoding">UTF-8</param> </result>

  Parameters:
    `encoding` - valid encoding string

### getDefaultDateFormat

    public String getDefaultDateFormat()

### setDefaultDateFormat

    public void setDefaultDateFormat(String defaultDateFormat)

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2000–2017 Apache Software Foundation. All rights reserved.