Genson selective use of useClassMetadata and addAlias

53 views Asked by At

I'm using Genson in my project and have just hit the polymorphic types issue (trying to serialise classes of generic type X).

Genson handles this by inserting some class meta data as explained here.

I'd like to use the 'addAlias' feature to make the json resilient to class refactoring changes but using this feature for my one polymorphic class seems to (as a side effect) turn on the 'useClassMetadata' flag for all other (non-polymorphic) classes.

This means to make my code resilient to class refactoring changes I need to add an alias for all my classes (and any future classes I might add) which is a pain and prone to be forgotten about and missed.

Is there a way in genson to turn on the inclusion of class meta data for just a small sub-set of classes and exclude it in the rest?

I've tried adding to the GensonBuilder .exclude("@class") .include("@class", classOf[X])

but this had no effect.

Any ideas?

1

There are 1 answers

0
glidester On

So the best I've come up with so far is to create a separate instance of Genson with the same config as the original but with the additional 'addAlias' instructions for the polymorphic classes.

This is only really an option for me because I can select the appropriate Genson instance when serialising the polymorphic classes and select the other instance for the rest.

Not an ideal solution in my book so I'm open to better answers :)