简体   繁体   中英

Unable to Deserialize a class using Gson or Jackson

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 ie

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.

超类型中是否有与您的字段冲突的标题字段。删除"header":{"title":"HelloWorld"} ,错误消失。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM