Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check Type in android

Tags:

java

android

gson

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.

like image 674
Bhavesh Hirpara Avatar asked Oct 16 '25 04:10

Bhavesh Hirpara


2 Answers

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.

like image 56
LuxuryMode Avatar answered Oct 17 '25 19:10

LuxuryMode


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)

like image 42
Prince Avatar answered Oct 17 '25 18:10

Prince



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!