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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:44:09+00:00 2026-05-18T11:44:09+00:00

I have a method with the following signature: // Converts a json string to

  • 0

I have a method with the following signature:

// Converts a json string to a list of objects
// Assumption: json is an array, and all items in the list are of the same type
public <T> List<T> getListFromJson( String json, Class<T> itemType, List<T> defValue ) {
    final ArrayList<T> list = new ArrayList<T>();

    for( JsonElement e : parser.parse(json).getAsJsonArray())
        list.add( (T) (
                Number.class.isAssignableFrom(itemType) ? e.getAsNumber() :
                Boolean.class.isAssignableFrom(itemType) ? e.getAsBoolean() :
                Character.class.isAssignableFrom(itemType) ? e.getAsCharacter() :
                String.class.isAssignableFrom(itemType) ? e.getAsString() :
                JsonElement.class.isAssignableFrom(itemType) ? e :
                null
            )
        );

    return list;
}

It reads the json string and converts it to a list of the appropriate type of object, eg. Integer, String, etc.

Is there a robust way to remove the Class<T> argument from the method by inferring it from the List<T> parameter? Eg. Is there a way I can change the method signature to the following without losing functionality?

public <T> List<T> getListFromJson( String json, List<T> defValue ) {
    ...
}

It seems like the solution is going to require some fancy manipulation of ParameterizedType. I’ve looked at the following, but either I’m using these methods incorrectly or they’re not doing what I expect:

  • getTypeArguments() from http://www.artima.com/weblogs/viewpost.jsp?thread=208860
  • getActualTypeArguments() from https://code.google.com/p/aphillips/source/browse/commons-lang/trunk/src/main/java/com/qrmedia/commons/lang/ClassUtils.java
  • 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-18T11:44:09+00:00Added an answer on May 18, 2026 at 11:44 am

    Due to type erasure, you definitely can’t “infer” what T is–it doesn’t even exist at runtime. The closest you could come is inspect the values in defValue (if it has values) and get the class of the elements there.

    Class<?> tType = defValue.get(0).getClass();
    
    if (Boolean.class.isAssignableFrom(tType)) { //...  
    

    Edit

    With regards to your thinking of using reflection like getTypeArguments(), etc. Those only provide data for declared types, never actual types. So for example, if you got a handle to the Method object and called getTypeParameters(), you’d just get an array containing a type object representing T–not the actual type that T represents at some specific runtime invocation.

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

Sidebar

Related Questions

I am using a method that has the following signature: public static bool TryAuthenticate(string
I have a C++ method that takes one variable the method signature is like
Using GWT I have a Java class: public class Pojo { private String name;
I'm following the example from the Rcpp intro Vignette, trying it with inline. f<-cxxfunction(signature(),
I have a list of about 25 types found in the Microsoft .NET assembly
Update : I appreciate all of the comments, which have essentially comprised unanimous opposition.
Say you have some target class with some methods on it: class Subject {
I have created a Web Service and deployed it on an internal server here.
Item 16 of Effective Java 2nd edition, favor composition over inheritance says the following
I am a bit confused about overriding vs. hiding a method in C#. Practical

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.