Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:22:33+00:00 2026-06-18T02:22:33+00:00

I have a List of objects, let’s say: List<Timestamp> Each Timestamp object includes other

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:22:35+00:00Added an answer on June 18, 2026 at 2:22 am

    I think this is the answer:

    1. write a custom gson serializer for the Tag class.
    2. Add a flag variable to Tag that indicates when to output the id in hex and when to toutput the id in decimal.
    3. Create a toString() method on the Tag class that pays attention to the newly added flag.

    Custom Serializer (variation from the gson doc example)

    private class TagSerializer implements JsonSerializer<Tag>
    {
      public JsonElement serialize(Tag src, Type typeOfSrc, JsonSerializationContext context)
      {
        return new JsonPrimitive(src.toString());
      }
    }
    

    Register the custom serializer

    GsonBuilder gson = new GsonBuilder();
    gson.registerTypeAdapter(Tag.class, new TagSerializer());
    

    Tag Updates

    boolean displayIdInHex = false;
    
    public void setDisplayIdInDecimal()
    {
      displayIdInHex = false;
    }
    
    public void setDisplayIdInHex()
    {
      displayIdInHex = true;
    }
    
    public String toString()
    {
      ... stuff ...
      if (displayIdInHex)
      {
        ... output id in hex.
      }
      else
      {
        ... output id in decimal.
      }
    }
    

    TimeStamp Updates
    public void setDisplayIdInDecimal()
    {
    tag.setDisplayIdInDecimal();
    }

    public void setDisplayIdInHex()
    {
      tag.setDisplayIdInHex();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an array list of objects, and each object has the
Let's say, we have and list of objects in variable called articles, each object
Let's say I have a generic Object class, and a generic List class. I
I have a list of objects: [Object_1, Object_2, Object_3] Each object has an attribute:
Let's say I have an array of primitives or a list of objects, doesn't
Let's say I have a list of objects. (All together now: I have a
I have an uniform list of objects in python: class myClass(object): def __init__(self, attr):
Let's say I have an unsorted list of four objects: [B, C, A, D]
Let's assume I have a list with objects of type Value . Value has
Let's say I have a list of User objects with two properties...ID and Name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.