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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:04:24+00:00 2026-06-07T14:04:24+00:00

I have a class that deserializes an ArrayList of generics with this function just

  • 0

I have a class that deserializes an ArrayList of generics with this function just as described in the first answer of this thread: Java abstract class function generic Type

public <T> ArrayList<T> arrayType(String data){
    return g.fromJson(data,   TypeToken.get(new ArrayList<T>().getClass()));
}

Eclipse asks me to cast TypeToken resulting in this (sinde the function fromJson needs a Type, not a TypeToken)

public <T> ArrayList<T> arrayType(String data){
    return g.fromJson(data,  (Type) TypeToken.get(new ArrayList<T>().getClass()));
}

As result I get this error:

java.lang.ClassCastException: com.google.gson.reflect.TypeToken cannot be cast to java.lang.reflect.Type

At the gson user manual they tell you this is the correct way of calling the function

Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);

and I can’t see what am I doing wrong (if it is a valid answer, why am I getting this cast error?)

  • 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-07T14:04:26+00:00Added an answer on June 7, 2026 at 2:04 pm

    Well you’re calling TypeToken.get, which returns a TypeToken – not a Type. in the example you’re showing which works, it’s using TypeToken.getType(), which returns a Type.

    So you could use:

    return g.fromJson(data, TypeToken.get(new ArrayList<T>().getClass()).getType());
    

    … and that would return you a Type, but it may not be what you actually want. In particular, due to type erasure that will return you the same type whatever T you specify from the call site. If you want the type to really reflect ArrayList<T>, you’ll need to pass the class into the method, although I’m not entirely sure where to go from there. (The Java reflection API isn’t terribly clear when it comes to generics, in my experience.)

    As an aside, I’d expect a method called arrayType to have something to do with arrays, not ArrayList.

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

Sidebar

Related Questions

I have class that looks like this: class A { public: class variables_map vm
I have a Class that has a private variable with a public setter/getter function:
I have a simple class in asp.net mvc that looks like this: public class
I have a class that I serialize/deserialize using XmlSerializer . This class contains a
I have a class that maintans a reference to a Hashtable and serializes/deserializes that
I have a class that looks like this: public class Person { public class
I have some JSON that nicely deserializes (with DataContractJsonSerializer) to: class Datum { /*
Possible Duplicate: How to Deserialize XML document Suppose that I have a class that
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that represents users. Users are divided into two groups with different

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.