0

I know about customizing the serialization process by including necessary methods in a class. But is it possible to change the serialization process at run time??
For example, I have defined a field in a class as transient but I want to serialize it at run time. Can this be done and how?

Thanks in advance.

Newbie
  • 2,979
  • 9
  • 34
  • 41

2 Answers2

0

You can by using Externalizable. See the example here.enter link description here. You can have to control on what to serialize when and in what form. For e.g. you can check at runtime if the field is marked transient and then make a decision.

Community
  • 1
  • 1
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • I know about this. But this is compile time. My question is about changing it at run time. – Newbie Aug 25 '13 at 15:11
  • @Newbie cam you explain what you mean by runtime? Let us say if you want to serialize conditionally then you can put that logic in your write/read methods. – Aravind Yarram Aug 25 '13 at 15:12
  • I have mentioned it in the question. For example, I have defined a field in a class as transient but during program execution, i want to serialize it. – Newbie Aug 25 '13 at 15:17
  • Ok, I know this. It can tell me whether a field is transient, but how to serializable it?? because serialization logic is present in the class. – Newbie Aug 25 '13 at 15:31
  • @Newbie point is you can't. You need to write your own code for serialization if you want something that is not in spec. – Aravind Yarram Aug 25 '13 at 15:38
0

I dont really know but I doubt it. Maybe you can do this the other way around by setting the field to null when you dont want it serialized and then dont define it as transient.

JWqvist
  • 717
  • 6
  • 15