The SojoJsonStringWriter is the default writer of the spring json-view. It is an integration of the sojo json string renderer and comes close to the default spring-mvc binding behaviour.
You can bind a CustomEditor to
Global value class types like java.util.Date
Properties of the ComandBean located by the CommensBeanUtils-Syntax
Optional: To any other object in the model like those added in the referenceData-method.
SojoJsonWriterConfiguratorTemplates enables you to use additional features on the fly.
It does not work with boolean getter starting with is[...]
and
NOW you can keep unconverted values in different modes.
Register a SojoJsonStringWriter-Bean to the JsonView.
<beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonWriter"><ref bean="jsonWriter"/></property> </bean> <bean name="jsonWriter" class="org.springframework.web.servlet.view.json.writer.sojo.SojoJsonStringWriter"/> </beans>
initBinder Source: ================= @Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); CustomDateEditor editor = new CustomDateEditor(dateFormat, true); binder.registerCustomEditor(Date.class, editor); } Result: ======= {"command":{ "birthday":"30-01-2008", "marriage":"30-01-2008", "placeofbirth":"Sydney", "age":"40", "childs": "true" }}
Properties of the CommandBean are located by the CommonsBeanUtils-Syntax
initBinder Source: ================== @Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); CustomDateEditor editor = new CustomDateEditor(dateFormat, true); binder.registerCustomEditor(Date.class, "birthday", editor); } Result: ====== {"command":{ "birthday":"30-01-2008", "marriage":"Wed Jan 30 00:00:00 GMT 2008", "placeofbirth":"Sydney", "age":"40", "childs": "true" }}
The SojoJsonStringWriter does provide the optional conversion of non CommandBean-Values of the model map. You have to activate this feature by setting the convertAllMapValues property in the JsonWriter-Bean in the view.xml.
You can locate them by registering a CustomEditor for a field starting with (non_commandbean_key).
<beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonWriter"><ref bean="jsonWriter"/></property> </bean> <bean name="jsonWriter" class="org.springframework.web.servlet.view.json.writer.sojo.SojoJsonStringWriter"> <property name="convertAllMapValues"><value>true</value></property> </bean> </beans>
initBinder Source: ================== @Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); CustomDateEditor editor = new CustomDateEditor(dateFormat, true); binder.registerCustomEditor(Date.class, editor); } Result: ======= {"signdate":"30-01-2008", "command":{ "birthday":"30-01-2008", "marriage":"30-01-2008", "placeofbirth":"Sydney", "age":"40", "childs": "true" }}
initBinder Source: ================== @Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); CustomDateEditor editor = new CustomDateEditor(dateFormat, true); binder.registerCustomEditor(Date.class, "birthday", editor); binder.registerCustomEditor(Date.class, "(signdate)", editor); } Result: ======= {"signdate":"30-01-2008", "command":{ "birthday":"30-01-2008", "marriage":"Wed Jan 30 00:00:00 GMT 2008", "placeofbirth":"Sydney", "age":"40", "childs": "true" }}
All values which are converted by an registered CustomEditor are rendered as a String. The spring jsl-view converts all other values of the model to strings too. The SojoJsonStringWriter keeps this behaviour by default. Some times it is very useful to know what type a particular unconverted value has. The JavaScript Object Notation specifys that numbers and booleans not quoted see http://www.json.org. The SojoJsonStringWriter is able to serialize unconverted values in 3 different modes.
<beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonWriter"><ref bean="jsonWriter"/></property> </bean> <bean name="jsonWriter" class="org.springframework.web.servlet.view.json.writer.sojo.SojoJsonStringWriter"> <property name="keepValueTypesMode"><value>ALL</value></property> </bean> </beans> Result: ======= {"signdate":"30-01-2008", "command":{ "birthday":"30-01-1968", "marriage":"Wed Jan 30 00:00:00 GMT 2008", "placeofbirth":"Sydney", "age":40, "childs": true }}
If you want to use a SojoJsonWriterConfiguratorTemplate, you have to
It is recommended to register the SojoJsonWriterConfiguratorTemplate in the initBinder method but you can register it in any controller method you can reach the request. This even could be the handleRequest method of an ControllerInterface implementation.
<beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonWriter"><ref bean="jsonWriter"/></property> </bean> <bean name="jsonWriter" class="org.springframework.web.servlet.view.json.writer.sojo.SojoJsonStringWriter"> <property name="enableJsonConfigSupport"><value>true</value></property> </bean> </beans>
SojoConfig property | Function | Allowed Values | Default Value |
convertAllMapValues | see 4. Convert all Model Values | true, false | false |
keepValueTypesMode | s.a Keep unconverted value type format. | NONE, BOOLEANS, ALL | NONE |
ignoreNullValues | All "null" valued property of the Model-Map are ignored Other wise the "null" value is rendered as "property":null |
true, false | false |
excludedProperties | Properties of the whole Model Map can be excluded. | String-Array with values like (name_in_model_map_key).property commandbeanproperty |
Empty String - Array |
interceptorList | Additional Sojo WalkerInterceptor-Interface Implementations can be registered see sojo homepage |
Sojo WalkerInterceptor Implementation |
Empty List |
initBinder Source: ================== @Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ JsonWriterConfiguratorTemplateRegistry registry = JsonWriterConfiguratorTemplateRegistry.load(request); registry.registerConfiguratorTemplate( new SojoJsonWriterConfiguratorTemplate(){ @Override public SojoConfig getJsonConfig() { SojoConfig config= new SojoConfig(); String[] excludes = new String[]{ "birthday" }; config.setExcludedProperties(excludes); return config; } } ); } Result: ======= { "command":{ "placeofbirth":"Sydney" }}