I have a List of objects, let’s say:
List<Timestamp>
Each “Timestamp” object includes other objects, in particular it has a “Tag” object.
class Timestamp {
String time;
...
Tag tag;
...
}
Now, every Tag object is identified by an ID of type “Integer”.
class Tag {
Integer id;
...
}
For a few reasons, I have to write the JSON representation of the whole timestamp list into a file by using the Gson library. In some cases I need the decimal representation of the ID of each Tag, while in other cases I need IDs in hexadecimal format.
How can I “switch” between the two formats? Consider that to write the whole list of Timestamp objects I use the following instruction:
ps.println(gson.toJson(timestamps));
and I can’t add other fields/types/objects in the Tag class because the JSON representation would be different.
I think this is the answer:
Custom Serializer (variation from the gson doc example)
Register the custom serializer
Tag Updates
TimeStamp Updates
public void setDisplayIdInDecimal()
{
tag.setDisplayIdInDecimal();
}