gson将字段反序列化为空

cunj1qz1  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(260)

首先,我想说这是一个大学项目。
我有3个类。Order抽象类和DeliveryDineIn类继承自订单。
我正在使用Gson来序列化/反序列化子类,但是我遇到了一个小问题。Order类有一个字段orderType,gson使用它来确定订单的类型是DineIn还是Delivery。
序列化工作得很好。问题是每当我试图反序列化时,type字段值不被读取,并且总是被设置为null,即使它存在于JSON文件中。当Order中有很多字段时会发生这种情况,因为当我尝试在较小规模上测试这个程序时,Order类只有2个字段(orderType和orderNo)一切都运行得很好。我不知道我做错了什么。我试着在这个网站上搜索,几乎总是遇到一些关于定制类型适配器和序列化器的建议,但我们在大学里没有研究过它们,我不想使用它们(老师没教过的东西都要扣分,我上次上他的课差点挂掉,就是因为我用了他没教过的东西。不过他好像对第三方图书馆没什么意见)。
代码:

public class Main {
    public static final List<Order> ordersList = read();
    public static void main(String[] args) {
        System.out.println(ordersList.get(0).getOrderType());
        System.out.println(ordersList.get(0) instanceof DineIn ? "DineIn": "Delivery");
    }

    private static List<Order> read(){
        List<Order> ordersList = new ArrayList<>();
        Type type = new TypeToken<ArrayList<Order>>() {
        }.getType();

        RuntimeTypeAdapterFactory<Order> adapter = RuntimeTypeAdapterFactory.of(Order.class, "orderType")
                .registerSubtype(DineIn.class)
                .registerSubtype(Delivery.class);

        Gson gson = new GsonBuilder().registerTypeAdapterFactory(adapter).create();
        JsonReader ordersJsonReader;
        try {
            ordersJsonReader = new JsonReader(new FileReader("orders.json"));
            List<Order> tempOrdersList = gson.fromJson(ordersJsonReader, type);
            if (tempOrdersList != null) ordersList = tempOrdersList;
            ordersJsonReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ordersList;
    }
}

abstract class Order {
    private final int orderNumber;
    private final String date, customerName;
    private final int discountRate;
    private final String paymentMethod;
    private String orderStatus;
    private int grossTotal = 0;
    private double netTotal = 0;
    private int totalItems = 0;
    protected final String orderType;

    public abstract String getOrderType();
    public abstract double getExtraCharges();

    public Order(int orderNumber, String date, String customerName, int discountRate, String paymentMethod, String orderStatus, int grossTotal, double netTotal, int totalItems, String orderType) {
        this.orderNumber = orderNumber;
        this.date = date;
        this.customerName = customerName;
        this.discountRate = discountRate;
        this.paymentMethod = paymentMethod;
        this.orderStatus = orderStatus;
        this.grossTotal = grossTotal;
        this.netTotal = netTotal;
        this.totalItems = totalItems;
        this.orderType = orderType;
    }
}

class DineIn extends Order {
    private double serviceCharges = 150;

    public DineIn(int orderNumber, String date, String customerName, int discountRate, String paymentMethod, String orderStatus, int grossTotal, double netTotal, int totalItems) {
        super(orderNumber, date, customerName, discountRate, paymentMethod, orderStatus, grossTotal, netTotal, totalItems, "DineIn");
    }

    @Override
    public String getOrderType() {
        return orderType;
    }

    @Override
    public double getExtraCharges() {
        return serviceCharges;
    }
}

class Delivery extends Order {
    private double deliveryCharges = 100;

    public Delivery(int orderNumber, String date, String customerName, int discountRate, String paymentMethod, String orderStatus, int grossTotal, double netTotal, int totalItems) {
        super(orderNumber, date, customerName, discountRate, paymentMethod, orderStatus, grossTotal, netTotal, totalItems, "Delivery");
    }

    @Override
    public String getOrderType() {
        return orderType;
    }

    @Override
    public double getExtraCharges() {
        return deliveryCharges;
    }
}

“JSON:

[
  {
    "serviceCharges": 150.0,
    "orderNumber": 1,
    "date": "12/12/2021",
    "customerName": "Ali",
    "discountRate": 15,
    "paymentMethod": "Card",
    "orderStatus": "Preparing",
    "grossTotal": 5000,
    "netTotal": 4500.0,
    "totalItems": 14,
    "orderType": "DineIn"
  }
]
pes8fvy9

pes8fvy91#

在代码中,DineInDelivery是从Order扩展而来的。orderType字段的设置方式是通过super()构造函数中的显式String参数。
然而,Gson并不使用构造函数来示例化对象,而是使用一个特殊的无参数构造函数,并通过反射来填充值:https://stackoverflow.com/a/40442037/9698467
在这个特定的例子中,问题来自RuntimeTypeAdapterFactory,它从它读取的JSON中删除了orderType字段。https://github.com/google/gson/blob/86d88c32cf6a6b7a6e0bbc855d76e4ccf6f120bb/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java#L202
正如@fluffy所建议的,库的新版本包括maintain标志,它应该允许字段被保留:https://github.com/google/gson/blob/c1e7e2d2808b042cbe47ca31869ee6ccc62c5417/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java#L214

相关问题