Is there any way to bring down GWT AutoBean?

I use AutoBeans to map JSON data coming from a web service that does not support Java GWT-RPC. Everything still works, except for one display.

On the server side, the class has a property of type Map, where MyAbstractParentObject is the parent class of about 15 different child classes.

When I map this to the corresponding AutoBean interface on the client, I cannot turn off MyAbstractParentObject to its child type after decoding it. I looked through all the GWT and Googles docs to see if AutoBeans supports polymorphic support, but can't get an answer anyway. Interceptors and categories do not seem to handle this, just the methods they want to use in the interface that are not getters / setters.

I tried to workaround using the type field in the JSON data to create an instance of the child class, but AutoBean does not give me access to raw JSON, although in the debugger I see it as a protected field called "data". If I try to decode the original bean, it will only have fields in the MyAbstractParentObject.

The only alternatives I see are:

  • Extend or create your own AutoBeanCodex, which MyAbstractParentObject children can correctly handle when it decodes JSON.
  • Find a way to access the original JSON in MyAbstractParentObject AutoBean and use this to create and instantiate the child class on the fly.
  • Switch to another JSON-GWT Serialization structure like GWTProJSONSerializer or piriti.

Any help would be appreciated.

+6
source share
2 answers

I know this was asked a long time ago, but I also tried to find the answer. I realized that AutoBeans, since they are basically just fancy wrappers for JSON, still contain all the data for the fields of the child object for which you want to disable it. So I wrote this method:

public <A, B> B cast( A sourceObject, Class<B> targetClass ) { AutoBean<A> sourceBean = AutoBeanUtils.getAutoBean( sourceObject ); // Get the corresponding AutoBean. HasSplittable splittableBean = ( HasSplittable ) sourceBean; // Implementation (if still AbstractAutoBean) supports this interface ;) Splittable splittable = splittableBean.getSplittable().deepCopy(); // If you don't copy it, decode() tries to be clever and returns // the original bean! AutoBean<B> targetBean = AutoBeanCodex.decode( typeFactory, targetClass, splittable ); // Create new AutoBean of // the target type. return targetBean.as(); // Get the proxy for the outside world. } 

- Where typeFactory extends AutoBeanFactory, as you can see.

It worked well enough for me. The hardest bit was passed to HasSplittable, since AutoBean does not extend this interface, but AbstractAutoBean (which implements AutoBean) does, and a subclass of this is what is returned by getAutoBean () calls.

You also need to copy Splittable, otherwise AutoBeanCodex will think: "Hey, I already have AutoBean for this Splittable! That's it!" - and just gives you the original .;)

In any case, you can throw it down, up ... sideways !: P

Later editing: Having stumbled upon this a few months later, I thought I'd add a little warning about something that Jonathan mentioned below. The method described here is intended for use in AutoBean, which has not been modified since it was deserialized. This is because (AFAIK) there is no guarantee that any setters you call will actually update JSON (necessary for casting). This is probably not a big deal, as you will usually use this when you have an inbound DTO and want to convert it to a real ASAP type before doing anything else with it. In our case, none of our AutoBeans even had setters, so this was not a problem.;)

After you deposit it, you can do whatever you want with the result of the bean, which is fresh from the factory in the end!

+4
source

I'm not very familiar with AutoBean, but you can probably use the serializer / deserializer from RestyGWT. It supports polymorphism using annotation.

documentation link: http://restygwt.fusesource.org/documentation/restygwt-user-guide.html#Polymorphic_Sub_Types

0
source

Source: https://habr.com/ru/post/898628/


All Articles