AutoBeanCodexEncode does not encode numbers when zero is the value

115 views
Skip to first unread message

AJ

unread,
Jul 19, 2013, 1:16:20 PM7/19/13
to google-we...@googlegroups.com
com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl

    @Override
    public boolean visitValueProperty(String propertyName, Object value, PropertyContext ctx) {
      if (value != null && !value.equals(ValueCodex.getUninitializedFieldValue(ctx.getType()))) {
        encodeProperty(propertyName, value, ctx);
      }
      return false;
    }

If I create an AutoBean with set/get for an native number type, say int, and set that value to zero then it is not in the result of AutoBeanCodex.encode(bean).getPayload()
So if the bean only contains that field and it is set to zero the resulting payload is {}

I would think it should be {"myIntValue":0} shouldn't it?

public class AutoBeanPoc implements EntryPoint {

    private PocBeanFactory beanFactory = GWT.create(PocBeanFactory.class);

    interface PocBeanFactory extends AutoBeanFactory {
        AutoBean<PocBean> pocBean();
    }

    interface PocBean {
        void setIntValue(int intValue);
        int getIntValue();
    }

    static class PocJsObject extends JavaScriptObject {
        protected PocJsObject() {  }

        public final native int getIntValue() /*-{
            if(typeof(this.intValue) == "undefined") {
                throw "intValue is undefined"
            }
            return this.intValue;
        }-*/; 
    }

    @Override
    public void onModuleLoad() {
        PocBean bean = beanFactory.pocBean().as();
        bean.setIntValue(0);

        String payload = AutoBeanCodex.encode(AutoBeanUtils.getAutoBean(bean)).getPayload();
        PocJsObject jsObj = JsonUtils.safeEval(payload);
        try {
            RootPanel.get().add(new HTML("intValue is " + jsObj.getIntValue()));
        }
        catch (JavaScriptException e) {
            RootPanel.get().add(new HTML(e.toString()));
        }
    }
}

I would be interested to hear the communities thoughts before I take the plunge and file a report.

Jens

unread,
Jul 19, 2013, 2:00:06 PM7/19/13
to google-we...@googlegroups.com
I would say its an AutoBean Framework optimization and it is working as intended. The framework assumes that you use AutoBeanCodex.decode() to parse the payload, which will probably set all AutoBean properties to default values and then update the ones found in the payload. There is no need to have default values in the payload if you can recreate them. As AutoBeans are based on interfaces, the framework knows the type of each property and thus knows the default value.

So you should either use AutoBeanCodex.decode() or setup the default value in PocJsObject.getIntValue() yourself.

-- J.



Anthony Elcocks

unread,
Jul 19, 2013, 3:26:33 PM7/19/13
to google-we...@googlegroups.com
That restricts me to destinations that are within my control and are Java based.
Say I want to send some data to a restful server based on node.js, fo example.
The AutoBeans are very useful because they allow me to create beans without the need to code my own toJson() method on each object.
I understand what you are saying, but the fact remains that the output from the getPayload does not serialize the object into JSON that any JSON parser can accurately reconstruct.


--
You received this message because you are subscribed to a topic in the Google Groups "Google Web Toolkit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-web-toolkit/dyPqr7O_RnA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

AJ

unread,
Jul 19, 2013, 3:33:26 PM7/19/13
to google-we...@googlegroups.com
The AutoBean page states

Goals

  • Decrease boilerplate in model-rich applications
  • Support easy encoding of AutoBeans to JSON structures
  • Provide support code for common operations on data-model objects
  • Usable in non-GWT (e.g. server) code
Of course, that could mean usable in non-GWT Java code, but it doesn't say that :)

Jens

unread,
Jul 19, 2013, 4:18:49 PM7/19/13
to google-we...@googlegroups.com
I can see your point, maybe you just create an issue asking for a flag that causes default values to be included in the payload so that AutoBeans can better be used with existing REST services. Wouldn't hurt and would make AutoBeans even more attractive.

-- J.

Pedro Lamarão

unread,
Jul 19, 2013, 5:56:24 PM7/19/13
to google-we...@googlegroups.com
It is usable, as intended.
It is just not "decodable by unawares JSON parser" as you initially expected.
A Java machine is not needed to decode this representation.
You just need to decode it according with its protocol.

--
 P. 

Yann Vo

unread,
Sep 21, 2016, 8:32:22 AM9/21/16
to GWT Users
For what it's worth, more than 3 years later... one workaround to have zero values serialized (along with the key) as you ask for is to use an Integer rather than an int.
This is the JavaScript semantic (anything can be undefined or null) and then it behaves as other JSON serializers to omit null value but not zero values.

Hope this helps if anybody ever reads this...
Reply all
Reply to author
Forward
0 new messages