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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:00:23+00:00 2026-06-13T03:00:23+00:00

All, I’m trying to do the following: public class SomClass { public boolean x;

  • 0

All,

I’m trying to do the following:

public class SomClass
{
    public boolean x;
    public int y;
    public String z;
}       

SomClass s = new SomClass();
s.x = true;
s.y = 10;
s.z = "ZZZ";
Gson gson = new Gson();
String retVal = gson.toJson(s);
return retVal;

So this little snippet will produce:

{"x":true,"y":10,"z":"ZZZ"}

but what I need it to produce is:

{"x":0, "y":10,"z":"ZZZ"}

Can someone please give me some options? I’d prefer not to rewrite my booleans as ints as that will cause several issues with existing code (nonobvious, hard to read, hard to enforce, etc.)

  • 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-13T03:00:27+00:00Added an answer on June 13, 2026 at 3:00 am

    To make it “correct” way, you can use something like that

    import java.lang.reflect.Type;
    
    import com.google.gson.JsonDeserializationContext;
    import com.google.gson.JsonDeserializer;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonParseException;
    import com.google.gson.JsonPrimitive;
    import com.google.gson.JsonSerializationContext;
    import com.google.gson.JsonSerializer;
    
    public class BooleanSerializer implements JsonSerializer<Boolean>, JsonDeserializer<Boolean> {
    
        @Override
        public JsonElement serialize(Boolean arg0, Type arg1, JsonSerializationContext arg2) {
            return new JsonPrimitive(Boolean.TRUE.equals(arg0));
        }
    
        @Override
        public Boolean deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
            return arg0.getAsInt() == 1;
        }
    }
    

    And then use it:

    public class Main {
    
        public class Base {
            @Expose
            @SerializedName("class")
            protected String clazz = getClass().getSimpleName();
            protected String control = "ctrl";
        }
    
        public class Child extends Base {
            protected String text = "This is text";
            protected Boolean boolTest = false;
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            Main m = new Main();
            GsonBuilder b = new GsonBuilder();
            BooleanSerializer serializer = new BooleanSerializer();
            b.registerTypeAdapter(Boolean.class, serializer);
            b.registerTypeAdapter(boolean.class, serializer);
            Gson gson = b.create();
    
            Child c = m.new Child();
            System.out.println(gson.toJson(c));
            String testStr = "{\"text\":\"This is text\",\"boolTest\":1,\"class\":\"Child\",\"control\":\"ctrl\"}";
            Child cc = gson.fromJson(testStr, Main.Child.class);
            System.out.println(gson.toJson(cc));
        }
    }
    

    Hope this helps someone 🙂

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

Sidebar

Related Questions

All- I have the following code: class AddStringTask extends AsyncTask<Void, String, Void> { @Override
All, I have the following class so to check to see if the form
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
All I'm trying to do is something fairly simple : Create a class (let's
All, If I run a query like the following: $qry = Select wrong_column from
All, If I've go the following database fields/values: user_id field_name field_value 1 EventHour1 1
All -- I have these two methods in one of my classes: public static
all. We're trying to get some intersect collisions working, but the problem experience is
All, I am trying to create a table to receive user inputs (UGC). This
All, I have the following code: $fetch = mysql_query(SELECT * FROM calendar_events where event_status='booked'

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.