[alibaba/fastjson]关于泛型反序列化问题

2024-05-22 300 views
3

实体类是这样的。

public class ProductWrapper<T extends Product>{
    private T product;
    private Integer quantity;
    public ProductWrapper() {
    }
    public ProductWrapper(T product, Integer quantity) {
        this.product = product;
        this.quantity = quantity;
    }
    public T getProduct() {
        return product;
    }
    public Integer getQuantity() {
        return quantity;
    }
    public static <T extends Product> ProductWrapper<T> create(T product, Integer quantity){
        return new ProductWrapper<>(product, quantity);
    }
    @Override
    public String toString() {
        return "ProductWrapper{" +
                "product=" + product +
                ", quantity=" + quantity +
                '}';
    }
}

但是反序列化失败

ProductWrapper<ProductEntity> productEntityProductWrapper =
                ProductWrapper.create(new ProductEntity("1L", "商品名字", "商品描述", BigDecimal.valueOf(11.11)), 100);

String jsonString = JSON.toJSONString(productEntityProductWrapper);

System.out.println(jsonString);  {"product":{"description":"商品描述","id":"1L","name":"商品名字","price":11.11},"quantity":100}

ProductWrapper<ProductEntity> productEntityProductWrapper1 = JSON.parseObject(jsonString,
                new TypeReference<ProductWrapper<ProductEntity>>(ProductEntity.class){});

System.out.println(productEntityProductWrapper1); ProductWrapper{product=null, quantity=null}

有什么解决办法吗?

回答

5

version 1.2.31

8

有人有解决方案吗

2
import java.math.BigDecimal;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @Author Scuman
 * @Date 2020/8/22
 * @Description https://github.com/alibaba/fastjson/issues/3389
 */
public class Issue3389 {
    public static void main(String[] args) {
        ProductWrapper<ProductEntity> productEntityProductWrapper =
                ProductWrapper.create(new ProductEntity("商品描述", "1L", "商品名字", BigDecimal.valueOf(11.11)), 100);

        String jsonString = JSON.toJSONString(productEntityProductWrapper);

        System.out.println(jsonString);

        ProductWrapper<ProductEntity> productEntityProductWrapper1 =
                JSON.parseObject(jsonString, new TypeReference<ProductWrapper<ProductEntity>>(ProductEntity.class) {
                });

        System.out.println(productEntityProductWrapper1);
    }
}

class Product {
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
class ProductEntity extends Product {
    private String description;
    private String id;
    private String name;
    private BigDecimal price;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
class ProductWrapper<T extends Product> {
    private T product;
    private Integer quantity;

    public static <T extends Product> ProductWrapper<T> create(T product, Integer quantity) {
        return new ProductWrapper<>(product, quantity);
    }
}

试试

1

嗯 是好的, 后来我发现没写set方法导致的。

7

方便的话close了吧