0

Good time!

Say there is such a class:

public class Individual {

   @JsonProperty("passport")
   private Passport passport;

   // getters ans setters
}

There are two use cases. First, when an income/outgoing json must be in a short format:

{
   individual:{
       passport:'123456789'  <-- this is the indentifier
   }
}

Second - an extended format:

{
   individual:{
       passport:{
             series:'1234',
             number:'1234'
      }
   }
}

To handle such cases it is possible to create 2 classes, say, IndividualSh and IndividualExt and use annotations @JsonSerializer and @JsonDeserializer to customize the input/output. I want to try something else to keep the only one Individual class, because it has a lot of fields and half of them needs customization. The first idea is to write several serializers/deserializers and a factory, that would mark the problem fields with required serializer/deserializer annotations using, say, the javassist library, but it seems that it is an overhead.

There is a great example by jlabedo here, but I can't realize how to use it in my situation.

Please, suggest, how to do it right way. Thanks in advance.

Community
  • 1
  • 1
Dmitry
  • 3,028
  • 6
  • 44
  • 66
  • Did you figure out a solution? What part are you stuck with? Are you stuck at serializing a Passport two different ways? Are you stuck at figuring out how to serialize a complex Passport object as a simple JSON string? – Programmer Bruce Apr 02 '13 at 00:46
  • @Programmer Bruce, I'm stuck with serializing nested objects in multiple different ways. Of cource, I can produce several clones of the parent object and annotate them differently, but I don't like this way and try to write a customizing mechanism. – Dmitry Apr 02 '13 at 07:03
  • Of cource, I can produce several clones of the parent object and annotate them differently, but I don't like this way and try to write a customizing mechanism. That's why I use javassist now, but it seems a huge overhead... – Dmitry Apr 02 '13 at 07:04

0 Answers0