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

  • Home
  • SEARCH
  • 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 6323759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:34:48+00:00 2026-05-24T16:34:48+00:00

With gson, is it possible to use a custom deserializer / serializer only on

  • 0

With gson, is it possible to use a custom deserializer / serializer only on certain fields? The user guide shows how to register an adapter for an entire type, not for specific fields. The reason why I want this is because I parse a custom date format and store it in a long member field (as a Unix timestamp), so I don’t want to register a type adapter for all Long fields.

Is there a way to do this?

  • 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-05-24T16:34:49+00:00Added an answer on May 24, 2026 at 4:34 pm

    I also store Date values as long in my objects for easy defensive copies. I also desired a way to override only the date fields when serializing my object and not having to write out all the fields in the process. This is the solution I came up with. Not sure it is the optimal way to handle this, but it seems to perform just fine.

    The DateUtil class is a custom class used here to get a Date parsed as a String.

    public final class Person {
      private final String firstName;
      private final String lastName;
      private final long birthDate;
    
      private Person(String firstName, String lastName, Date birthDate) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate.getTime();
      }
    
      public static Person getInstance(String firstName, String lastName, Date birthDate) {
        return new Person(firstName, lastName, birthDate);
      }
    
      public String toJson() {
        return new GsonBuilder().registerTypeAdapter(Person.class, new PersonSerializer()).create().toJson(this);
      }
    
      public static class PersonSerializer implements JsonSerializer<Person> {
        @Override
        public JsonElement serialize(Person person, Type type, JsonSerializationContext context) {
          JsonElement personJson = new Gson().toJsonTree(person);
          personJson.getAsJsonObject().add("birthDate", new JsonPrimitive(DateUtil.getFormattedDate(new Date(policy.birthDate), DateFormat.USA_DATE)));
          return personJson;
        }
      }
    }
    

    When the class is serialized, the birthDate field is returned as a formatted String instead of the long value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does GSON use ONLY fields(private,public,protected)? Is there a way to tell GSON to
I want to deserialize a JSON object (using GSON, because I already use it
Seems like Gson.toJson(Object object) generates JSON code with randomly spread fields of the object.
I've got somthing to parse with Gson, but Gson recognize only json object and
I'm using GSON for a project. In particular I use this code to generate
I use gson in several places in my code, and it seems that every
I am planning to use gson's fromJson() method to parse a string coming from
I want to parse this JSON string using Gson {name:name,type:[a,b,c]} is it possible? 1st
I am using the Gson library to make Json objects. I made a serializer
I am using Gson to parse Json. What I don't understand what the return

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.