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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:13:46+00:00 2026-06-16T05:13:46+00:00

I need to serialize an object like this: class A { int a =

  • 0

I need to serialize an object like this:

class A {
    int a = 1;
    String b = "hello";
    boolean isDog = false;
}

into JSON array like this:

[1,"hello",false]

I know one (wrong) way to do this: create an untyped Collection out of object’s fields and then Gson it:

class A {
    // ...
    Collection forGson() {
        ArrayList col = new ArrayList();
        col.add(a);
        col.add(b);
        col.add(c);
        return col;
    } 
}
new Gson().toJson(new A().forGson());

But it produces a lot of warnings because of untyped collections usage. So is there any way to serialize objects into an array of arbitrary types without getting any warnings?

  • 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-16T05:13:47+00:00Added an answer on June 16, 2026 at 5:13 am

    This is literally “You’re doing it wrong”. You don’t have an array of random things, you have an (A) object. It has nothing in common with the JSON you want to produce.

    That being said, if you really wanted to do this, you supply your own Serializer / Deserializer to Gson:

    class ASerializer implements JsonSerializer<A>
    {
    
        public JsonElement serialize(A t, Type type, JsonSerializationContext jsc)
        {
            JsonArray ja = new JsonArray();
            ja.add(new JsonPrimitive(t.a));
            ja.add(new JsonPrimitive(t.b));
            ja.add(new JsonPrimitive(t.isDog));
            return ja;
        }
    
    }
    

    You’d create a JsonDeserializer that did the reverse, creating a A object from the supplied JSON array.

    See: https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization for more info.

    Then using GsonBuilder you’d tell Gson to use them:

    GsonBuilder builder = new GsonBuilder(); 
    builder.registerTypeAdapter(A.class, new ASerializer());
    builder.registerTypeAdapter(A.class, new ADeserializer());
    Gson gson = builder.create(); 
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to serialize an object like this: public class Book { public string
I need to serialize ArrayList<String> arr=new ArrayList<String>(); on server and then send this array
I would like to use this kind of Objects with json: class Message{ int
I've got an ICollection<T> of POCO like this: public class SearchJsonModel { public string
I'm using Marshal.dump to serialize an array of objects, I need to get the
I have a business class which I need to serialize to xml. It has
I have a simple Java class that I need to serialize to be stored
I have a class that I need to be able to serialize to a
I have the following C# helper class which retrieves a Json String from a
I am quite new trying msgpack. I need to serialize an object (instance of

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.