Android:Gson序列化缺少一个特定领域

这是课程

class TransactionDetails {

    @SerializedName("item")
    private String item;
    @SerializedName("itemPrice")
    private double itemPrice;
    @SerializedName("commissions")
    private double commissions;
    @SerializedName("itemCount")
    private double itemCount;
    @SerializedName("when")
    private long when;
    @SerializedName("tax")
    private double tax;

    TransactionDetails(String item,double itemPrice,double commissions,double itemCount,long when,double tax) {
        this.item = item;
        this.itemPrice = itemPrice;
        this.commissions = commissions;
        this.itemCount = itemCount;
        this.when = when;
        this.tax = tax;
    }

    String getItem() {
        return item;
    }

    double getItemPrice() {
        return itemPrice;
    }

    double getcommissions() {
        return commissions;
    }

    double getItemCount() {
        return itemCount;
    }

    long getWhen() {
        return when;
    }

    double getTotalPrice() {
        return itemCount * itemPrice;
    }

    String getWhenStr() {
        return Utils.getInstance().getDateStr(when);
    }

    double getTax() {
        return tax;
    }

}

double getTax()是我在代码中不使用的唯一方法,因为我没有在我的应用程序中使用它,因此我不确定“从未使用过方法getTax()”,尽管我不确定相关

这是实例

ArrayList<TransactionDetails> transactionDetailsFull = new ArrayList<>();

这是失败的代码

new Gson().toJson(Utils.getInstance().transactionDetailsFull);

由于某种原因,以上代码序列化了该类的所有字段(税项除外)。仅在释放模式下会发生这种情况:

minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'

在调试模式下,它可以工作。请注意,我做了@SerializedName,所以我不明白我缺少什么以及什么导致仅税项无法序列化。

gswgyh727 回答:Android:Gson序列化缺少一个特定领域

您是否在proguard文件中添加了Gson和您的类(TransactionDetails)? 如果不。 尝试像这样更改proguard文件:

# Add the gson class
-keep public class com.google.gson

# Add any classes the interact with gson
-keep class yourpackage.TransactionDetails { *; }
,

好吧,显然这部分是错误的:double getTax()是我在代码中不使用的唯一方法,因为我在我的应用程序中没有使用它,因此我看到“从未使用过方法getTax()”尽管我不确定是否相关。我在代码中添加了对getTax()的引用,现在Gson对其进行了序列化...

也许是错误或者我对序列化的工作方式不了解...

本文链接:https://www.f2er.com/2893515.html

大家都在问