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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:23:37+00:00 2026-05-24T22:23:37+00:00

I have an ArrayList<CustomClass> inside of ParentClass which I have written to a file

  • 0

I have an ArrayList<CustomClass> inside of ParentClass which I have written to a file using Gson.toJson(). However when I try to de-serialize the JSON using Gson.fromJson() I only get 1 element of the ArrayList<CustomClass>.

For example I will do the following

public class ParentClass {
    private ArrayList<CustomClass> myList = new ArrayList<CustomClass>();

    private GrandParentClass nested;

    public ParentClass() {
        myList.add(new CustomClass("adsf"));
        myList.add(new CustomClass("fdsa"));

        nested = new GrandParentClass();
    }

    public int arraySize() {
        return myList.size();
    }
}

public class GrandParentClass {
    private ArrayList<OtherCustomClass> myList = new ArrayList<OtherCustomClass>();

    public GrandParentClass() {
        myList.add(new CustomClass("asdfasdf.."));
        myList.add(new CustomClass("fdsafdsa..."));

    }

    public int arraySize() {
        return myList.size();
    }
}

Then when I instantiate a new instance of ParentClass, I use the following to write it to a file.

ParentClass pc = new ParentClass();
Gson gson = new Gson();
String writeThis = gson.toJson(pc); // Produces a perfect JSON reflection myList

FileOutputStream fos = new FileOutputStream(new File("writeto.json"));
fos.write(writeThis);
fos.close();

The JSON object is written in plain text to the .json file

FileInputStream fis = new FileInputStream(new File("writeto.json"));
char c;
StringBuffer sb = new StringBuffer();
while ((c = fis.read()) != -1)
    sb.append((char) c);

//Now this is where I only get 1 element of the ArrayList
Gson gson = new Gson();
ParentClass pc = gson.fromJson(sb.toString(), ParentClass.class);

Log.i("SIZE", "Size is " + pc.arraySize()); // Log output: 'Size is 1'

Now, even though I have verified that there are indeed two elements in the ArrayList in the JSON file, only 1 gets loaded into the object using fromJson.

I am serializing these just fine, I would however like to deserialize the ArrayList<OtherCustomClass> inside of the GrandParentClass, which is inside of the ParentClass, in one fell swoop.

Basically I want to serialize ArrayLists nested possibly 3 or 4 layers down in this object heirarchy, and deserialize them into 1 ParentClass that contains these nested ArrayLists<?>. How would this be accomplished?

Thanks

  • 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-24T22:23:37+00:00Added an answer on May 24, 2026 at 10:23 pm

    You need to do some additional work when you want to deserialize a collection when generics are involved. This is explained here.

    However, I am not sure how this applies to your case, since you have the collection “nested” within a top-level non-generic class.

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

Sidebar

Related Questions

I have an arraylist which has a class datastructure. public class empRec { public
I have an arraylist which I'd like to display in a tabular fashion, but
I have an ArrayList that contains Strings and also contains other ArrayLists which may
I have an arraylist which has data structures in it. I am having problems
I have the arraylist which has some process names like Notepad, mspaint I want
In my spring web application I have arraylist in model attribute which I want
I have the following classes: public class CollectionCustomClass extends ArrayList<CustomClass> public abstract class CustomClass
I have a arraylist in my web application project in asp.net/C#/VS2008 and I'm using
I have an arraylist of doubles returned by a JSON library. After the JSON
I have an ArrayList and I am using an iterator to run through it.

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.