Class JSONView

java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractView
net.solarnetwork.web.support.AbstractView
net.solarnetwork.web.support.JSONView
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanNameAware, org.springframework.context.ApplicationContextAware, org.springframework.web.context.ServletContextAware, org.springframework.web.servlet.View

public class JSONView extends AbstractView
View to return JSON encoded data.

The view model is turned into a complete JSON object. The model keys become JSON object keys, and the model values the corresponding JSON object values. Array and Collection object values will be rendered as JSON array values. Primitive types will render as JSOM primitive values (numbers, strings). Objects will be treated as JavaBeans and the bean properties will be used to render nested JSON objects.

All object values are handled in a recursive fashion, so array, collection, and bean property values will be rendered accordingly.

The JSON encoding is constructed in a streaming fashion, so object graphs of arbitrary size should not cause any memory-related errors.

Version:
1.2
Author:
Matt Magoffin
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The default content type: application/json;charset=UTF-8.
    static final String
    The default character encoding used: UTF-8.

    Fields inherited from class net.solarnetwork.web.support.AbstractView

    DEFAULT_JAVA_BEAN_IGNORE_PROPERTIES, DEFAULT_JAVA_BEAN_STRING_VALUES

    Fields inherited from class org.springframework.web.servlet.view.AbstractView

    DEFAULT_CONTENT_TYPE

    Fields inherited from class org.springframework.context.support.ApplicationObjectSupport

    logger

    Fields inherited from interface org.springframework.web.servlet.View

    PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Get the number of spaces to indent (pretty print) the JSON output with.
    org.springframework.beans.PropertyEditorRegistrar
    Get the optional registrar of PropertyEditor instances that can be used to serialize specific objects into String values.
    boolean
    Get the "include parentheses" option.
    protected void
    renderMergedOutputModel(Map<String,Object> model, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
     
    void
    setIncludeParentheses(boolean includeParentheses)
    Set the "include parentheses" option.
    void
    setIndentAmount(int indentAmount)
    Set the number of spaces to indent (pretty print) the JSON output with.
    void
    setPropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar propertyEditorRegistrar)
    Set the optional registrar of PropertyEditor instances that can be used to serialize specific objects into String values.

    Methods inherited from class org.springframework.web.servlet.view.AbstractView

    addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, prepareResponse, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, setResponseContentType, toString, writeToResponse

    Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport

    getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext

    Methods inherited from class org.springframework.context.support.ApplicationObjectSupport

    getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • JSON_CONTENT_TYPE

      public static final String JSON_CONTENT_TYPE
      The default content type: application/json;charset=UTF-8.
      See Also:
    • UTF8_CHAR_ENCODING

      public static final String UTF8_CHAR_ENCODING
      The default character encoding used: UTF-8.
      See Also:
  • Constructor Details

    • JSONView

      public JSONView()
      Default constructor.
  • Method Details

    • renderMergedOutputModel

      protected void renderMergedOutputModel(Map<String,Object> model, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception
      Specified by:
      renderMergedOutputModel in class org.springframework.web.servlet.view.AbstractView
      Throws:
      Exception
    • getIndentAmount

      public int getIndentAmount()
      Get the number of spaces to indent (pretty print) the JSON output with.

      If set to zero no indentation will be added (this is the default).

      Returns:
      the indentation amount; defaults to 0
    • setIndentAmount

      public void setIndentAmount(int indentAmount)
      Set the number of spaces to indent (pretty print) the JSON output with.
      Parameters:
      indentAmount - the amount to set
    • isIncludeParentheses

      public boolean isIncludeParentheses()
      Get the "include parentheses" option.
      Returns:
      the option; defaults to false
    • setIncludeParentheses

      public void setIncludeParentheses(boolean includeParentheses)
      Set the "include parentheses" option.
      Parameters:
      includeParentheses - true to wrap the entire response in parentheses, required for JSON evaluation support in certain (old school) browsers
    • getPropertyEditorRegistrar

      public org.springframework.beans.PropertyEditorRegistrar getPropertyEditorRegistrar()
      Get the optional registrar of PropertyEditor instances that can be used to serialize specific objects into String values.
      Returns:
      the registrar
    • setPropertyEditorRegistrar

      public void setPropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar propertyEditorRegistrar)
      Set the optional registrar of PropertyEditor instances that can be used to serialize specific objects into String values.

      This can be useful for formatting Date objects into strings, for example.

      Parameters:
      propertyEditorRegistrar - the registrar to set