Class 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
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
FieldsModifier and TypeFieldDescriptionstatic final StringThe default content type: application/json;charset=UTF-8.static final StringThe default character encoding used: UTF-8.Fields inherited from class net.solarnetwork.web.support.AbstractView
DEFAULT_JAVA_BEAN_IGNORE_PROPERTIES, DEFAULT_JAVA_BEAN_STRING_VALUESFields inherited from class org.springframework.web.servlet.view.AbstractView
DEFAULT_CONTENT_TYPEFields inherited from class org.springframework.context.support.ApplicationObjectSupport
loggerFields inherited from interface org.springframework.web.servlet.View
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintGet the number of spaces to indent (pretty print) the JSON output with.org.springframework.beans.PropertyEditorRegistrarGet the optional registrar of PropertyEditor instances that can be used to serialize specific objects into String values.booleanGet the "include parentheses" option.protected voidrenderMergedOutputModel(Map<String, Object> model, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) voidsetIncludeParentheses(boolean includeParentheses) Set the "include parentheses" option.voidsetIndentAmount(int indentAmount) Set the number of spaces to indent (pretty print) the JSON output with.voidsetPropertyEditorRegistrar(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 net.solarnetwork.web.support.AbstractView
getJavaBeanIgnoreProperties, getJavaBeanTreatAsStringValues, getModelObjectIgnoreTypes, getPropertySerializerRegistrar, getResponseCharacterEncoding, render, setJavaBeanIgnoreProperties, setJavaBeanTreatAsStringValues, setModelObjectIgnoreTypes, setPropertySerializerRegistrarMethods 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, writeToResponseMethods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContextMethods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
-
Field Details
-
JSON_CONTENT_TYPE
The default content type: application/json;charset=UTF-8.- See Also:
-
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:
renderMergedOutputModelin classorg.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
-