You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Artashes Hovasapyan <ar...@gmail.com> on 2010/01/19 00:01:30 UTC

Struts 2 JSON plugin

Hi,

I've just started using JSON plugin and noticed strange behaviour. It looks
like JSON annotation fields are completely ignored by the plugin. My action
looks like this:

public class TestAction extends ActionSupport {
    private Date date;

    @JSON(format = "yyyy-MM-dd HH:mm:ss Z")
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String test() {
        date = new Date();
        return "success";
    }
}

Struts configuration looks like this:

    <package name="test" extends="json-default">
        <action name="Test" class="org.test.TestAction" method="test">
            <result name="success" type="json">
                <param name="root">date</param>
            </result>
        </action>
    </package>

Result date is serialized according to default "yyyy-MM-dd'T'HH:mm:ss"
format.

Further research showed that JSONResult.execute() method calls
JSONUtil.serialize() (line 198), which in turn calls
JSONWriter.write()(line 112). This
JSONWriter.write() method calls JSONWriter.value() method (line 99), which
has 2 parameters - actual object to be serialized and the method, which
supposed to be the getter method of the serializable object (at least that's
how it looks to me). Apparently  JSONWriter.write() calls
JSONWriter.value()only passing the object,
null is passed instead of the method. That's where I got confused the most -
if the method is not propagated, how the plugin will pick the annotation
fields?
Any idea anyone? Maybe developers can help me with this?

-- 
Regards,
Artashes Hovasapyan