Is there a way to annotate an AutoBean property so that it will not be serialized/deserialized?

StackOverflow https://stackoverflow.com/questions/13849281

  •  07-12-2021
  •  | 
  •  

سؤال

I have an autobean with a property that is only needed for the UI. I believe that you can null out values and the AutoBeanCodex will not serialized that property, but that equates to an extra step which is needed at serialization.

I was hoping for some annotation similar to the Editor @Ignore annotation. For example:

public interface Foo {
    ...
    @Ignore
    String getUiOnlyProperty();
}

So, other than nulling out the value at serialization time, is there any other way to keep an autobean property from being serialized?

هل كانت مفيدة؟

المحلول

Autobeans are meant to be a Java skin on a JSON/XML/whatever format - they aren't really designed to hold other pieces of data. That said, several thoughts that either nearly answer your question with out-of-the-box tools, or might inspire some other ideas on how to solve your problem.

You should be able to build read-only properties by omitting the setter. This isn't quite what you are asking for, but still might be handy.

Along those lines, the JavaDoc for the @PropertyName annotation seems to allude to this possible feature:

/**
 * An annotation that allows inferred property names to be overridden.
 * <p>
 * This annotation is asymmetric, applying it to a getter will not affect the
 * setter. The asymmetry allows existing users of an interface to read old
 * {@link AutoBeanCodex} messages, but write new ones.
 */

Reading old messages but writing new ones seems like it might be closer to what you are after, and still allowing you to work with the thing-that-looks-like-a-bean.

The real answer though seems to be the AutoBean.setTag and getTag methods:

/**
 * A tag is an arbitrary piece of external metadata to be associated with the
 * wrapped value.
 * 
 * @param tagName the tag name
 * @param value the wrapped value
 * @see #getTag(String)
 */
void setTag(String tagName, Object value);

...

/**
 * Retrieve a tag value that was previously provided to
 * {@link #setTag(String, Object)}.
 * 
 * @param tagName the tag name
 * @return the tag value
 * @see #setTag(String, Object)
 */
<Q> Q getTag(String tagName);

As can be seen from the implementation of these methods in AbstractAutoBean, these store their data in a totally separate object from what is sent over the wire. The downside is that you'll need to get the underlying AutoBean object (see com.google.web.bindery.autobean.shared.AutoBeanUtils.getAutoBean(U) for one way to do this) in order to invoke these methods.

نصائح أخرى

child class/interface decoded as a parent interface does not explode on decoding allowing the goods to be passed together before the marshalling steps. my immediate test of the actual code below is performing as expected.

  public interface ReplicateOptions {
    /**
     * Create target database if it does not exist. Only for server replications.
     */
    Boolean getCreateTarget();

    void setCreateTarget(Boolean create_target); 

  //baggage to pass along
  interface ReplicateCall<T> extends ReplicateOptions   {

      /**
       * If true starts subscribing to future changes in the source database and continue replicating them.
       */
      AsyncCallback<T> getContinuous();

      void setContinuous(AsyncCallback<T> continuous); 

}
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top