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 8770023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:28:30+00:00 2026-06-13T17:28:30+00:00

I am using GSON for my serialization purposes, I am not finding a way

  • 0

I am using GSON for my serialization purposes, I am not finding a way of excluding certain fields from serialization based on ExclusionStrategy class provided by Gson based on the value of field, as it only supports top level class or field attributes based exclusions. The field attributes do not include the value of that field. So what should I do?

  • 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-13T17:28:31+00:00Added an answer on June 13, 2026 at 5:28 pm

    The way to achieve this is by creating custom serializer for the class in question. After allowing Gson to create a JSON object in default fashion, remove the property that you want to exclude based on its value.

    public class SerializerForMyClass implements JsonSerializer<MyClass> {  
    
        @Override
        public JsonElement serialize(MyClass obj, Type type, JsonSerializationContext jsc) {
            Gson gson = new Gson();
            JsonObject jObj = (JsonObject)gson.toJsonTree(obj);   
            if(obj.getMyProperty()==0){
                jObj.remove("myProperty");
            }
            return jObj;
        }
    }
    

    And registering the new serializer in the Gson object that you use for serialization in the application for this class.

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(MyClass.class, new SerializerForMyClass());
    Gson gson=gsonBuilder.create();
    gson.toJson(myObjectOfTypeMyClass);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i deserialize a widget-hierarchy using gson, but have problems deserializing final fields. Example: public
Have a couple questions, using GSON. I have a feeling that GSON might not
I have a method for reading JSON from a service, I'm using Gson to
I'm using Gson to convert a class containing two GeoPoints to a JSON string.
I'm creating some big JSON file using GSON to create custom JSON from GTFS
I'm trying to serialize an array of 7000 POJO using GSON and the serialization
I'm using Gson API and created a class like: public class Person { private
I am tring to map json data using Gson from a URL to my
I've recently started using GSON library for deserializing JSON that comes from web service
I am using GSON to decode JSON strings that are returned from our server.

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.