SojoJsonStringWriter - SOJO support

  1. Instruction
  2. Simple Binding
  3. Binding to CommandBean-Property
  4. Convert all Model Values
    1. Convert all values of Model-Map by CustomEditor
    2. Convert specific value in Model-Map by CustomEditor
  5. Keep unconverted value type format
  6. Register SojoJsonWriterConfiguratorTemplates

Instruction

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 located by the CommensBeanUtils-Syntax

  3. 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.

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

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

NEWS
NOW the SojoJsonStringWriter supports implizite and explicite collection property binding
  • implizite: bean.list.property: locates all properties of the collection from index 0-n

    and

  • explicite: bean.list[1].property: locates properties implicate Collection-Beans with EXPIZITE index

NOW you can keep unconverted values in different modes.

Using

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>

Simple Binding

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"
}}

Binding to CommandBean-Property

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"
}}

Convert all Model Values

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).

  • (name_in_model_map_key).property
  • (key).list[1].property
<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>

Convert all values in Model-Map by CustomEditor

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"
}}

Convert specific values in Model-Map by CustomEditor

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"
}}

Keep unconverted value type format

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.

  1. "NONE"-Mode (Default): serialize all values to strings.
  2. "BOOLEANS"-Mode: keep unconverted Boolean value type format like "flag":true
  3. "ALL"-Mode: keep all unconverted value type formats like "flag":true ore "number":2

Set the "keepValueTypesMode"-property in the spring-configuration

<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
}}

Register SojoJsonWriterConfiguratorTemplates

If you want to use a SojoJsonWriterConfiguratorTemplate, you have to

  1. set the "enableJsonConfigSupport"-property of the SojoJsonStringWriter.
  2. implement the abstract "SojoJsonWriterConfiguratorTemplate".
  3. register the SojoJsonWriterConfiguratorTemplate at the JsonWriterConfiguratorTemplateRegistry.

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.

Set the "enableJsonConfigSupport"-property in the spring-configuration

<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>

Configuration : SojoConfig

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

5.3 Register the SojoJsonWriterConfiguratorTemplates

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"
}}