Home > Mobile >  Unable to convert class to JSON using GSON
Unable to convert class to JSON using GSON

Time:03-09

When i'm trying to convert an object in my Android App to a Json i get the following error:

class android.content.res.ColorStateList declares multiple JSON fields named mChangingConfigurations

The issue is that in some devices all is working GSON version:

implementation 'com.google.code.gson:gson:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

While the code i'm trying to convert the object is the following:

Comanda comanda = new Comanda(
                    tasto.getDescriptionReceipt(),
                    quantitaSelezionata,
                    tasto.getCodice(),
                    turnoValue,
                    currentTimeString,
                    operatore,
                    "A",
                    String.valueOf(tasto.getPrice()));
            intent.putExtra("prodotto", new Gson().toJson(comanda));

Comanda class looks like this:

public class Comanda {

    private final String descProdotto;
    private double qta;
    private int turno;
    private String codice;
    private String time;
    private String codOP;
    private String state;
    private String pre;
    private ArrayList<Varianti> variants = new ArrayList<>();


    public Comanda(String button, double qta, String codice, int turno, String time, String codOP, String state, String pre) {
        this.descProdotto = button;
        this.qta = qta;
        this.codice = codice;
        this.turno = turno;
        this.time = time;
        this.codOP = codOP;
        this.state = state;
        this.pre = pre;
    }

}

public class Varianti implements Serializable {

    String name;
    private String codice;
    BitmapDrawable drawable;
    private String state;
    public String pre;
    public String qta;

    public Varianti(String name, String codice, BitmapDrawable drawable, String state, String pre, String qta) {
        this.name = name;
        this.codice = codice;
        this.drawable = drawable;
        this.state = state;
        this.pre = pre;
        this.qta = qta;
    }
}

Any solution even in kotlin is welcome

CodePudding user response:

I think the problem is the Varianti bean, which has a property BitmapDrawable . when you use the Gson to convert it to string, you should not handle it.

ublic class Varianti implements Serializable {

    String name;
    private String codice;
    private transient BitmapDrawable drawable;
    private String state;
    public String pre;
    public String qta;

    public Varianti(String name, String codice, BitmapDrawable drawable, String state, String pre, String qta) {
        this.name = name;
        this.codice = codice;
        this.drawable = drawable;
        this.state = state;
        this.pre = pre;
        this.qta = qta;
    }
}


  • Related