Binding - Conversion of Datatypes

  1. Instruction
  2. JsonStringWriter
    1. SojoJsonStringWriter
    2. JsonlibJsonStringWriter
    3. XStreamJsonStringWriter
  3. Comparison

Instruction

Spring-Json View currently provides support for binding to JSON-lib, SOJO and XStream. You can implement the binding from either a Spring Command or FormController. Usually you register a CustomEditor at the ServletRequestDataBinder in the initBinder-method of the controller.

By default Spring-MVC provides the binding of CustomEditors to properties of the Command bean to

  • Global value class types like java.util.Date
  • Properties of the ComandBean located by the Apache Commons BeanUtils syntax
@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);
        binder.registerCustomEditor(Date.class, "birthday", editor);
}

JsonStringWriter

The JsonStringWriter manages the converting of the model/commandbean values and of course the rendering of the Json string. Spring-Json View tries to keep the pattern of the CustomEditor registration you know from Spring-MVC. The dataformat convention features of the existing Json libraries do not exactly support the features we need. On the other hand the libraries do support some special features you going to be able to use. So you will find differences of the suppoted binding depending on the implementation of the JsonStringWriter. In the following chapters you might find information which could help you to choose a JsonStringWriter.

SojoJsonStringWriter - SOJO Json support

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

  1. Global value class types like java.util.Date
  2. Properties of the ComandBean are located using Apache Commons BeanUtils syntax
  3. Optional: To any other object in the model like those added in the referenceData-method.

Optional: register a custom "SojoConfig" object by the SojoJsonWriterConfiguratorTemplate

Optional: choose a mode to keep the type of unconverted value.

ATTENTION
SOJO detects setter and getter by starting with get[...] / set[...].

It does not work with boolean getter starting with is[...]

JsonlibJsonStringWriter - JSON-Lib support

The JsonlibJsonStringWriter is the integration of the JSON-lib framework to Spring-Json View. Thanks to francesco -- original ideas we are able to localise properties of the ComandBean by the CommensBeanUtils-Syntax. You can use the full power of the JSON-lib framework by register a JsonlibJsonWriterConfiguratorTemplate. It is a wrapper for an "net.sf.json.JsonConfig" object. For further informations see the JSON-lib homepage.

You can bind a CustomEditor to

  1. Global value class types like java.util.Date (it converts the howl model map.)
  2. Properties of the ComandBean are located using Apache Commons BeanUtils syntax
  3. Optional: To any other object in the model like those added in the referenceData-method.

Optional: register a custom "net.sf.json.JsonConfig" object by the JsonlibJsonWriterConfiguratorTemplate

Optional: keep the null values by setting the "keepNullProperties" - property.

XStreamJsonStringWriter - XStream streaming support

The XStreamJsonStringWriter is the integration of the XStream framework to Spring-Json View. It is able to write the generated parts of the json string into a stream, so it can used for big command beans.

You can bind a CustomEditor to

  1. Global value class types like java.util.Date (it converts the howl model map.)
  2. Properties of the ComandBean are located using Apache Commons BeanUtils syntax
  3. Optional: To any other object in the model like those added in the referenceData-method.

Optional: register a custom XStream (SpringJsonXStream) object by the JsonlibJsonWriterConfiguratorTemplate

Optional: choose a mode to keep the type of unconverted value.

Optional: Annotation Support

ATTENTION
XStream 1.3.1 will not work with Google Application Engine

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

2. Comparison

SojoJsonStringWriter JsonlibJsonStringWriter XStreamJsonStringWriter
Supported library SOJO-0.5.0 >= JSON-lib-2.2.1 xstream-1.3
Default Writer yes no no
ComandBean conversion
Convert class types yes yes yes
Properties located by Commons BeanUtils syntax yes yes yes
Other model map properties conversion
Convert class types optional optional optional
Properties located by optional optional optional
JsonWriterConfiguratorTemplate optional optional optional
"keep type of unconverted value" modes NONE, BOOLEANS, ALL ALL NONE, BOOLEANS, ALL
default mode NONE ALL NONE
Streaming support no no yes
JSON Format characteristics none unconverted values do
allway keep there
orginal type
the JSON Stream has
allways an additional root element
{model:{command:[...]}}
null values
simpel and maped properties allways yes/no yes by default allways
indexed properties (Collections) allways no (keep index of other) no (keep index of other)
Annotation support no no yes
Other it just works with
getters/setter start
get[...]/set[...]
no boolean is[...]
XStream 1.3.1 is not
comaptieble to GAE
Google Application Engine