mardi 2 juillet 2019

Custom annotation for modifying value at serialization

I have a specific use-case which I'm trying to resolve using custom-serialization (GSON) and custom java annotations.

I'm reading in json data from a source, transforming it and writing it to a sink. My Json is of the following structure: (The transformed Json is of the same structure but has different values for identifiers to be transformed)

{
 "someId": "abc", 
 "otherId": "efgh", 
 "metadata": {"id": "xyz", "count": 5},
 "cardId": "qwe123"
 "version": 1.0
}

This translates to the following model:

public class Customers {
    @Transform private String someId;   // transform this identifier
    @Transform private String otherId;   // transform this identifier
    private CustomerMetadata metadata;
    private String cardId;    // Do not transform
    private long version;
}

and,

public class CustomerMetadata {
    @Transform private String id;   // transform this identifier
    private int count;
}

I have a use-case wherein I want to transform specific identifiers. This transformation happens by another service which accepts a list of identifiers, transforms it and returns the transformed identifiers (of type string) in the same order.

I would ideally want to invoke this transformation service once for a single json file. My idea is to achieve this by writing a custom serializer or a custom de-serializer. The serializer would look for the @Transform annotation to capture the list of identifiers, invoke the transformation service to receive the list of transformed identifiers and finally replace the corresponding identifiers with the transformed values.

Also, I need to achieve this in a general fashion so that my serializer isn't dependent of a particular model (POJO).

Any ideas how to handle this? I'm open to using Jackson if it provides similar functionality (although I didn't find such a use case)





Aucun commentaire:

Enregistrer un commentaire