4

One of my function getting Type variable. sometimes it can be java.util.List<Test$MyClass> or java.util.List<java.lang.String> so how can I identify them?

if (type instanceof List) {

}

Both are List type but different type. So that above code does not work. I want to distinguish between the two list types.

my main issue is https://stackoverflow.com/questions/15596112/implement-jsondeserializer-more-than-one-in-gson-in-android

I am using deserializer for that..

public class Data implements JsonDeserializer<ArrayList<MyClass1>> {
public ArrayList<MyClass1> myList1 = new ArrayList<MyClass1>();
public ArrayList<MyClass2> myList2 = new ArrayList<MyClass2>();

@Override
public ArrayList<MyClass1> deserialize(JsonElement json, Type type,
        JsonDeserializationContext context) throws JsonParseException {

    Debug.e("", type.toString());

    ArrayList<Layoutmap> data = new ArrayList<Layoutmap>();

    try {
        if (json.isJsonObject()) {

            // my stuff

            return data;
        } else {
            return new Gson().fromJson(json, type);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return data;
}

}

above code works for public ArrayList<MyClass2> myList2 = new ArrayList<MyClass2>(); and myList2 is also i want to deserialize.. It's part of google Gson.

Community
  • 1
  • 1
Bhavesh Hirpara
  • 22,255
  • 15
  • 63
  • 104
  • Do you want to distinguish between the two list types? – tom Mar 24 '13 at 11:54
  • 2
    IMHO, this indicates a design problem. You shouldn't have a method returning sometimes something, and some other times something else. – JB Nizet Mar 24 '13 at 12:01
  • Agree with JB Nizet. But if you want to compare each element for some reason then you could check my ans. – lokoko Mar 24 '13 at 12:04
  • Type is not list type.. see my updated question.... – Bhavesh Hirpara Mar 24 '13 at 12:09
  • [the first answer to this question](http://stackoverflow.com/questions/1942644/get-generic-type-of-java-util-list) may be usefull in your case. tl;dr: use reflection. – Oren Mar 24 '13 at 12:11

5 Answers5

5

Yeah, the problem is that in Java an instance test against a type that is not reifiable is always an error.

For example:

if(o instance of List<E>)

where E is any type, will not compile.

You simply have to check that what's contained in the Lists are the same type, e.g.

if(list1.get(0) instance of String && list2.get(0) instance of String)

EDIT:

Not sure if there's a language barrier issue here (your question is hard to fully understand), but you should not need to make your method take an argument of type Type. As someone else mentioned, you have a bad design here. What you should be doing is simply defining how to deserialize each of your classes (if custom deserialization needs to be defined at all; that is, if the defaults won't work) and then get a list of those in the regular way with Gson.

I'm not super familiar with Gson, but it should be something like:

List<Class1> stuff = gson.fromJson(json, new TypeToken<List<Class1>>(){}.getType());

So, in other words, if each class has peculiarities that will make default deserialization not work, then there's no way (without a lot of gymnastics) to make a deserialize method work for every single type.

Also, don't combine all functionality into one. Getting an ArrayList<LayoutMap> should be separated from the functionality of deserializing each class.

LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
2

try to use getClass() function on your type to find the class of an object on runtime. for eg.. type.getClass().equals(java.lang.string)

Prince
  • 59
  • 4
1

You can just try something like :

for (int i=0; i<type.size(); ++i) {
    if (type.get(i) instanceof String) {
        // Do something here
    }
}

This would check each element of the list and if its a string you could work on it.

lokoko
  • 5,785
  • 5
  • 35
  • 68
0

Hopefully, type will be an instance of ParameterizedType. So you can use the method getActualTypeArguments().

Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
0

I just find my solution .. I don't know it is better or not

I am comparing two Type variable like this...

if (type.toString().equalsIgnoreCase(
                new TypeToken<List<Test.MyClass>>() {
                }.getType().toString())) {

}else
{

}
Bhavesh Hirpara
  • 22,255
  • 15
  • 63
  • 104