The binding capabilities are the same like the SojoJsonStringWriter or JsonlibJsonStringWriter
You can bind a CustomEditor to Global value class types like java.util.Date
You can bind a CustomEditor to Properties of the ComandBean located by the CommensBeanUtils-Syntax
Optional: To any other object in the model like those added in the referenceData-method.
The XStreamJsonStringWriter supports implizite and explicite collection property binding
and
You can keep unconverted values in different modes.
You can configurate the the JSON rendering by adding xstream Anotations see Annotations Tutorial
XStreamJsonWriterConfiguratorTemplate enables you to use some of the Features of XStream. For example tweaking the Output.
{model:{command:[...]}}
http://jira.codehaus.org/browse/XSTR-566
http://paulhammant.com/blog/google-app-engine-for-java-with-rich-ruby-clients.html
http://www.nabble.com/Making-XStream-compatible-with-the-Google-App-Engine.-td22944106.html
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: ======= {model: {"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: ====== {model: {"command":{ "birthday":"30-01-2008", "marriage":"Wed Jan 30 00:00:00 GMT 2008", "placeofbirth":"Sydney", "age":"40", "childs": "true" } } }
The XStreamStringWriter 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.xstream.XStreamJsonStringWriter"> <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: ======= {model: {"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: ======= {model: {"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 XStreamStringWriter 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 XStreamStringWriter 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.xstream.XStreamJsonStringWriter""> <property name="keepValueTypesMode"><value>ALL</value></property> </bean> </beans> Result: ======= {model: {"signdate":"30-01-2008", "command":{ "birthday":"30-01-1968", "marriage":"Wed Jan 30 00:00:00 GMT 2008", "placeofbirth":"Sydney", "age":40, "childs": true }} }
You can configurate the the JSON rendering by adding xstream Anotations see Annotations Tutorial You can use following propertes to activate Annotation Support.
<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.xstream.XStreamJsonStringWriter""> <property name="enableAnnotationConf"><value>true</value></property> </bean> </beans> CommandBean ======= public class SpringJsonForm { private String placeofbirth; @XStreamAlias("date") private Date birthday; private String placeofbirth; private Integer age; @XStreamOmitField private boolean childs; // setter and getter } Result: ======= {model: {"signdate":"30-01-2008", "command":{ "date":"30-01-1968", "marriage":"Wed Jan 30 00:00:00 GMT 2008", "placeofbirth":"Sydney", "age":40, }} }
If you want to use a XStreamJsonWriterConfiguratorTemplate, you have to
It is recommended to register the XStreamJsonWriterConfiguratorTemplate 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="jsonlibJsonWriter"/></property> </bean> <bean name="jsonWriter" class="org.springframework.web.servlet.view.json.writer.xstream.XStreamJsonStringWriter"> <property name="enableJsonConfigSupport"><value>true</value></property> </bean> </beans>
The SpringJsonXStream extends the com.thoughtworks.xstream.XStream object. SpringJsonXStream enables you to use some of the Features of XStream. For example tweaking the Output.
Please always use the SpringJsonXStreamBuilder to create a new SpringJsonXStream object. You have two functions to do thought:
function parameter | 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 |
rootname | The JSON Stream generated by the XStreamJsonStringWriter has an additional root element like {model:{command:[...]}} |
A valide JSON Object property name of type java.lang.String |
model |
prettyPrint | converts the out put to a structured format | true, false | false |
enableAnnotationConf | enable Annotation Configuration | true, false | false |
processAnnotationsForType processAnnotationsForTypes |
Class/es which should be considerred for Annotation Configuration |
Class Class[] |
CommandBean Class |
autodetectAnnotations | Add autodetection for Annotation Configuration | true, false | false |
initBinder Source: ================== @Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ JsonWriterConfiguratorTemplateRegistry registry = JsonWriterConfiguratorTemplateRegistry.load(request); registry.registerConfiguratorTemplate( new XStreamJsonWriterConfiguratorTemplate(){ @Override public SpringJsonXStream getSpringJsonXStream() { SpringJsonXStream xstream = SpringJsonXStreamBuilder.build(false, JsonStringWriter.MODE_KEEP_VALUETYPES_NONE, "model"); // rename property xstream.aliasField("renamedsigndate", ComplexBean.class, "signdate"); // exclude property xstream.omitField(ComplexBean.class, "age") return xstream; } } ); } Result: ======= {model: {"renamedsigndate":"30-01-2008", "command":{ "birthday":"30-01-1968", "marriage":"Wed Jan 30 00:00:00 GMT 2008", "placeofbirth":"Sydney", "childs":"true" }} }