Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to make third party objects serializable without writing wrapper

I have third party platform jars of which APIs need to be tested using webservices. These APIs take custom objects which are non serializable. Say for example some of the APIs are like this.

doSomething(CustomId someId, DBLoaderType type, DBFilter filter, boolean exclude) returns java.util.List<SomeNavigationSystem>

But these CustomId, DBLoaderType, DBFilter, SomeNavigationSystem are not serializable objects.

Is there a way that I need not write any extra classes for all these classes (there is a lot of such classes) to serialize? If I write one wrapper or equivalent DTO for each business class then this will be very unscalable.

like image 220
java_enthu Avatar asked May 16 '11 13:05

java_enthu


People also ask

Can we serialize an object without implementing serializable interface?

If the superclass is Serializable, then by default, every subclass is serializable. Hence, even though subclass doesn't implement Serializable interface( and if its superclass implements Serializable), then we can serialize subclass object.

How do you serialize an object that does not implement serializable?

If Address is null, you can serialize the whole employee even if Address 's type is not serializable. If you mark Address as transient, it will skip trying to serialize Address . This may solve your problem.

How an object can become serializable?

To serialize an object means to convert its state to a byte stream so way that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.

Which interface needs to be implemented in order to make an object serialized?

An object is serializable only if its class implements the Serializable interface. Thus, if you want to serialize the instances of one of your classes, the class must implement the Serializable interface.


1 Answers

please have a look at the Google gson lib under http://code.google.com/p/google-gson/.

I am not sure if I fully understood your problem, but if you want to serialize objects that do not implement java.io.Serializable you should consider json or one of its implementations. Assuming that your objects are serializable from their 'nature', gson can serializable them into a string. Your web service schema may look ugly later, but it could work. You will end up in sending strings.

like image 101
Wintermute Avatar answered Sep 20 '22 23:09

Wintermute