3

I have an service that talks to backend servers and front-end clients. The backend apps are expecting JSON in one format and the front-end apps are expecting it in a different format.

If I try to using @JsonProperty I end up with a situation where either Jackson can't deserialize the data from the client or it can't serialize the data to the service because the annotations don't match. For example: I need this to properly send my JSON to the backend servers:

   @JsonProperty("createDate")
   public void setCreationDate(Date creationDate) {
      this.creationDate = creationDate;
   }

However, this fails to deserialize the the JSON coming from the clients. If I remove @JsonProperty then the outcome is reserved.

Any suggestions on how to solve this? I think I have the following options:

  1. Use different models for the front end and backend.
  2. Perhaps use different ObjectMapper instances?
  3. Use @JsonView somehow?
  4. Custom serializer?
  5. JSON Schema?

Any pointers?

Thanks!

Edit:

I don't think its a duplicate because I need different names based on where the JSON is coming from or going to. Its contextually instead of being a global rename which is what it appears @JsonProperty is really good at.

donahchoo
  • 275
  • 3
  • 14
  • Possible duplicate of [Different names of JSON property during serialization and deserialization](http://stackoverflow.com/questions/8560348/different-names-of-json-property-during-serialization-and-deserialization) – shmosel Nov 30 '16 at 21:13
  • yeah, I've read that one. I don't think its a duplicate because I need different names based on where the JSON is coming from or going to. Its contextually instead of being a global rename which is what it appears @JsonProperty is really good at. – donahchoo Nov 30 '16 at 21:16
  • 2
    Look into using custom mixin annotations for different services/clients. – shmosel Nov 30 '16 at 21:18
  • @kaqqao no, from what I read he needs to serialize and de-serialize 2 different ways each. – hvgotcodes Nov 30 '16 at 23:33
  • @shmosel mixins worked great! Do you want to add that as a response so I can mark it as the answer? – donahchoo Dec 02 '16 at 02:51

0 Answers0