I'm trying to convert rest-facebook response to a Post class object as follows:
Post post = gson.fromJson(restFBResponse.toString(), Post.class);
where, restFBResponse
is a post from Facebook. But it is resulting into error:
Exception in thread "main" java.lang.IllegalArgumentException: class com.restfb.types.Post declares multiple JSON fields named type
I think this is due to:
1)
class Post extends NamedFacebookType{
@Facebook
private String type;
//and some more class members
}
2)
class NamedFacebookType extends FacebookType {
//few class members
}
3)
class FacebookType implements Serializable {
@Facebook
private String type;
//and some more class members
}
So, private String type;
is being declared twice, in class Post
and class FacebookType
.
1) If such re-declaration occurs in subclass,should it not be overridden? and
2) How can I overcome this error class com.restfb.types.Post declares multiple JSON fields named type
?
I guess GSON suggested that
@SerializedName("type1") private String type;
This may resolve issue. But I think there should be a cleaner solution to this..I found that the issue has been marked invalid and no further response was received.
You can write your own serializer and deserializer for gson to serialize and deserialize
Post
object.While doing so use restfb's
DefaultJsonMapper
to map Post ob.Something Like,
You don't want to declare the same variable "type" in your class Post. As it extends from FacebookType, it implicitly has the variable "type".
Solution:
I just written my own class without extension, found no other better option..