0

I'm getting a JSON from an endpoint and I'm converting that into a POJO using GSON. I need to manipulate some of that data and convert it back into a JSON but with different names. If I used @SerializedName, the name changes when it's converted to a POJO from the JSON, but changes back again when I convert back into a JSON.

How could I fix this?

user7434041
  • 73
  • 1
  • 1
  • 7

1 Answers1

0

If you are willing to switch from GSON to Jackson, then you can see the solution to this other Stack Overflow question

EDIT

Assuming you are stuck with GSON, it doesn't appear to be possible using the same class to read and write the same field with different keys. While other frameworks accomplish this by performing serialization and deserialization from getters and setters and looking for custom decorators on those methods, according to the GSON design document (see Using fields vs getters to indicate Json elements) they have decided to only use fields for now.

EDIT2

As a workaround, I might suggest creating another POJO that can be instantiated from your original object instance but has the field names you would like for serialization back to JSON. It would be a little hack-y, but you could encapsulate it in a ToJSON() method on the main class to keep things clean when doing the serialization.

RemedialBear
  • 644
  • 5
  • 15