0

I am trying to deserialize a json string to Card.class and I am unable to do it.

    Card card =new Card();
    card.setHeader(new CardHeader().setTitle("HelloWorld"));
    Section section=new Section().setWidgets(Collections.singletonList(new WidgetMarkup().setImage(new Image().setImageUrl("https://gazabb//..com"))));
    card.setSections(Collections.singletonList(section));
    String req=GsonUtils.GSON.toJson(card,Card.class);
    System.out.println(req);

the output is as expected

{"header":{"title":"HelloWorld"},"sections":[{"widgets":[{"image":{"imageUrl":"https://gazabb//..com"}}]}]}

But when I am trying to serialize it to the Card.class i.e

GsonUtils.GSON.fromJson(req,Card.class);

It is giving me error:

java.lang.IllegalArgumentException: Can not set com.google.api.services.chat.v1.model.CardHeader field com.google.api.services.chat.v1.model.Card.header to com.google.gson.internal.LinkedTreeMap
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at com.google.api.client.util.FieldInfo.setFieldValue(FieldInfo.java:275)
at com.google.api.client.util.FieldInfo.setValue(FieldInfo.java:231)
at com.google.api.client.util.GenericData.put(GenericData.java:98)
at com.google.api.client.util.GenericData.put(GenericData.java:43)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:188)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
at io.gsonfire.gson.HooksTypeAdapter.deserialize(HooksTypeAdapter.java:86)
at io.gsonfire.gson.HooksTypeAdapter.read(HooksTypeAdapter.java:54)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)

Card.class is:

package com.google.api.services.chat.v1.model;
import com.google.api.client.json.GenericJson;
import com.google.api.client.util.Data;
import com.google.api.client.util.Key;
import java.util.List;

public final class Card extends GenericJson {
@Key
private List<CardAction> cardActions;
@Key
private CardHeader header;
@Key
private String name;
@Key
private List<Section> sections;

public Card() {
}

public List<CardAction> getCardActions() {
    return this.cardActions;
}

public Card setCardActions(List<CardAction> var1) {
    this.cardActions = var1;
    return this;
}

public CardHeader getHeader() {
    return this.header;
}

public Card setHeader(CardHeader var1) {
    this.header = var1;
    return this;
}

public String getName() {
    return this.name;
}

public Card setName(String var1) {
    this.name = var1;
    return this;
}

public List<Section> getSections() {
    return this.sections;
}

public Card setSections(List<Section> var1) {
    this.sections = var1;
    return this;
}

public Card set(String var1, Object var2) {
    return (Card)super.set(var1, var2);
}

public Card clone() {
    return (Card)super.clone();
}

static {
    Data.nullOf(CardAction.class);
    Data.nullOf(Section.class);
}

CardHeader class:

package com.google.api.services.chat.v1.model;

import com.google.api.client.json.GenericJson;
import com.google.api.client.util.Key;

public final class CardHeader extends GenericJson {
@Key
private String imageStyle;
@Key
private String imageUrl;
@Key
private String subtitle;
@Key
private String title;

public CardHeader() {
}

public String getImageStyle() {
    return this.imageStyle;
}

public CardHeader setImageStyle(String var1) {
    this.imageStyle = var1;
    return this;
}

public String getImageUrl() {
    return this.imageUrl;
}

public CardHeader setImageUrl(String var1) {
    this.imageUrl = var1;
    return this;
}

public String getSubtitle() {
    return this.subtitle;
}

public CardHeader setSubtitle(String var1) {
    this.subtitle = var1;
    return this;
}

public String getTitle() {
    return this.title;
}

public CardHeader setTitle(String var1) {
    this.title = var1;
    return this;
}

public CardHeader set(String var1, Object var2) {
    return (CardHeader)super.set(var1, var2);
}

public CardHeader clone() {
    return (CardHeader)super.clone();
}

}

I am stuck with it since 2 days. From error it is clearly visible that CardHeader field of Card is unable to set to LinkedTreeMap. But I am unable to find the reason for it. Is it possible to deserialize it without writing my own TypeAdaptor. I have also tried to do the same thing with Jackson but facing same Error.

  • https://googleapis.dev/java/google-api-services-chat/latest/ – Srivstvanik Oct 08 '21 at 19:53
  • I found it a bit strange. As I am able to serialize a class to json string. But when trying to deserialize it to same class using resultant json string, it is giving error – Srivstvanik Oct 08 '21 at 19:59
  • Is there maybe a `header` field in the supertype that conflicts with your field? – Vasily Liaskovsky Oct 08 '21 at 20:02
  • @VasilyLiaskovsky no in the parent class, there is no`header` field – Srivstvanik Oct 08 '21 at 20:04
  • I noticed also your setters are "builder" style, returning host object. That also might be a problem, because Jackson deserialization uses reflection to locate setXXX methods with specific signature. Maybe you should try change these to traditional setter style, with `void` return type. – Vasily Liaskovsky Oct 08 '21 at 20:09
  • @VasilyLiaskovsky , I cant do that as they are the library classes, I have given the link to the library in above comments – Srivstvanik Oct 08 '21 at 20:13
  • 1
    I guess you should resort to custom deserializer then. This is a good starting point for doing that https://www.baeldung.com/jackson-deserialization . You will need to manually register deserializer as the class cannot be marked with annotation. – Vasily Liaskovsky Oct 08 '21 at 20:21
  • i wanted to know the reason for the error, i can anytime write the custom deserializer – Srivstvanik Oct 08 '21 at 20:44
  • _Is it possible to deserialize it without writing my own TypeAdaptor._ -- Aha, don't use `GenericJson` instances then. – terrorrussia-keeps-killing Oct 09 '21 at 04:59

1 Answers1

0

Is there maybe a header field in the supertype that conflicts with your field。remove "header":{"title":"HelloWorld"} ,error disappear.