Search code examples
smooks

How to write decoder from date to date in smooks java to java


I am preparing Java object from json using ObjectMapper. Here is the json data

"dateTimeSent" : "LongValue"

Source and target both java classes have field is java.util.Date type. I tried mapping of like this

 <jb:value property="dtSent" data="dateTimeSent" decoder="Date">
        <jb:decodeParam name="format">EEE MMM dd HH:mm:ss z yyyy</jb:decodeParam>
 </jb:value>

In documentation it is mentioned that this decoder used to encode/decode from String to java.util.Date. Is that i need to write custom decoder for that. If yes please let me know how to write. I am new to smooks.


Solution

  • As smooks encode/decode from String to java.util.Date/java.sql.Date/ java.util.Calendar/java.sql.Time/java.sql.Timestamp. My use case, i have to decode from date to date. So i had created one more variable in source class with setter and getter like - private String modeifiedDateTimeSent

    I am mapping with modifiedDateTimeSent variable in smooks-config.xml

    <jb:value property="dtSent" data="modeifiedDateTimeSent" decoder="Date">
        <jb:decodeParam name="format">yyyy-MM-dd HH:mm:ss</jb:decodeParam>
    </jb:value>
    

    Next, I have to set the value in the variable before mapping java class A to Class B.

        Date modifiedDtTimeSent = order.getLr().getAdminSection().getDateTimeSent();
        String modifiedDtTimeSentString = getDateAsString(modifiedDtTimeSent,"yyyy-MM-dd HH:mm:ss");
        object.setModifieddatetimesent(modifiedDtTimeSentString);
    

    Then finally, do your smooks java to java convertion -

            Smooks smooks =  new Smooks("smooks-config.xml");
            ExecutionContext executionContext = smooks.createExecutionContext();
            JavaSource source = new JavaSource(object);
            JavaResult result = new JavaResult();
            smooks.filterSource(executionContext, source, result);
            ConvertedClass cc = (IimLocalResponse) result.getBean("xyz");
    

    Hope this will help.