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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:42:40+00:00 2026-06-17T20:42:40+00:00

I’m writing a class which will connect to a server and based on some

  • 0

I’m writing a class which will connect to a server and based on some arguments, retrieve a json-string which will be parsed with GSON to the specified (via generics) class.

A stripped down version of the class in charge looks like this:

class Executor<T> {

    private Response<T> response;

    public void execute() {
        Type responseType = new TypeToken<Response<T>>() {}.getType();
        this.response = new Gson().fromJson(json, responseType);
    }

    public Response<T> getResponse() { return this.response; }

}

(the JSON-variable looks like this.)

The class which stores the data once de-serialized looks like this:

class Response<T> {

    private List<T> data = null;

    public List<T> getData() { return this.data; }

}

The class which the data is trying to be de-serialized to:

public class Language {
    public String alias;
    public String label;
}

And the code which runs utilizes the classes above:

Executor<Language> executor = new Executor<Language();
List<Language> languages = executor.execute().getResponse().getData();
System.out.println(languages.get(0).alias); // exception occurs here

Which results in the following exception

ClassCastException: com.google.gson.internal.StringMap cannot be cast to sunnerberg.skolbibliotek.book.Language

Any help or suggestions are greatly appreciated!

  • 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-17T20:42:42+00:00Added an answer on June 17, 2026 at 8:42 pm

    The short answer is that you need to move the creation of the TypeToken out of Executor, bind the T in Response<T> when you create the token (new TypeToken<Response<Language>>() {}), and pass in the type token to the Executor constructor.

    The long answer is:

    Generics on a type are typically erased at runtime, except when the type is compiled with the generic parameter bound. In that case, the compiler inserts the generic type information into the compiled class. In other cases, that is not possible.

    So for instance, consider:

    List<Integer> foo = new ArrayList<Integer>();
    
    class IntegerList extends ArrayList<Integer> { ... }
    List<Integer> bar = new IntegerList();
    

    At runtime, Java knows bar contains integers because the Integer type is bound to ArrayList at compile time, so the generic type information is saved in the IntegerList class file. However, the generic type information for foo is erased, so at runtime it is not really possible to determine that foo is supposed to contain Integers.

    So it often comes up that we need generic type information in a situation where it normally would be erased before runtime, such as here in the case of parsing JSON data in GSON. In these situations, we can take advantage of the fact that type information is preserved when it is bound at compile-time (as in the IntegerList example above) by using type tokens, which are really just tiny anonymous classes that conveniently store generic type information.

    Now to your code:

    Type responseType = new TypeToken<Response<T>>() {}.getType();
    

    In this line of your Executor class, we create an anonymous class (inheriting from TypeToken) which has the type Response<T> hard coded (bound) at compile-time. So at runtime, GSON is able to determine that you want an object of Response<T>. But it doesn’t know what T is, because you didn’t specify it at compile-time! So GSON cannot determine what type will be in the List of the Response object it creates, and it just creates a StringMap instead.

    The moral of the story is that you need to specify that T at compile-time. If Executor is meant to be used generically, you probably need to create the type token outside of that class, in your client code. Something like:

    class Executor<T> {
    
        private TypeToken<Response<T>> responseType;
        private Response<T> response;
    
        public Executor(TypeToken<Response<T>> responseType) {
            this.responseType = responseType;
        }
    
        public void execute() {
            this.response = new Gson().fromJson(json, responseType.getType());
        }
    
        public Response<T> getResponse() { return this.response; }
    
    }
    
    // client code:
    Executor<Language> executor = new Executor<Language>(new TypeToken<Response<Language>>() { });
    executor.execute();
    List<Language> languages = executor.getResponse().getData();
    System.out.println(languages.get(0).alias); // prints "be"
    

    By the way, I did test the above on my machine.

    Sorry if that was too long!

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am writing an app for my school newspaper, which is run completely online
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am confused How to use looping for Json response Array in another Array.
I am using JSon response to parse title,date content and thumbnail images and place
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I am trying to understand how to use SyndicationItem to display feed which is

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.