Skip to content

[BUG] JSNB反序列化异常 #2902

@yanfuyou

Description

@yanfuyou

问题描述

简要描述您碰到的问题。
原对象中包含LocalDate类型属性且不为空,但目标对象无该属性时使用JSONB对象执行反序列化报错

环境信息

请填写以下信息:

  • OS信息: [e.g.:Microsoft Windows 11 10.0.22635 8Core 2.40GHz 16 GB]
  • JDK信息: [e.g.:Openjdk 17.0.7]
  • 版本信息:[e.g.:Fastjson2 2.0.52]

重现步骤

如何操作可以重现该问题:

  1. 使用 `main 方法
  2. 错误内容
Exception in thread "main" com.alibaba.fastjson2.JSONException: name not support input : LOCAL_DATE -87
	at com.alibaba.fastjson2.JSONReaderJSONB.notSupportType(JSONReaderJSONB.java:2633)
	at com.alibaba.fastjson2.JSONReaderJSONB.skipValue(JSONReaderJSONB.java:2594)
	at com.alibaba.fastjson2.reader.ObjectReaderBean.processExtra(ObjectReaderBean.java:134)
	at com.alibaba.fastjson2.reader.ORG_1_1_Hanbao.readJSONBObject(Unknown Source)
	at com.alibaba.fastjson2.JSONB.parseObject(JSONB.java:799)
	at com.kykj.hk.base.common.bean.Bean2Utils.clone(Bean2Utils.java:50)
	at com.kykj.hk.base.common.bean.Bean2Utils.deepCopyTest(Bean2Utils.java:201)
	at com.kykj.hk.base.common.bean.Bean2Utils.main(Bean2Utils.java:190)

6.调用代码

package com.kykj.hk.base.common.bean;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.TypeReference;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.UtilityClass;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;

import java.beans.PropertyDescriptor;
import java.time.LocalDate;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Supplier;

/**
 * @author ChenHao
 * @date 2023/5/31 18:15
 */
@UtilityClass
public class Bean2Utils extends BeanUtils {

    public static <T, R> R clone(final T source, Class<R> destClass) {
        if (source == null) {
            return null;
        }
        JSONReader.Feature[] features = {
                JSONReader.Feature.ErrorOnNotSupportAutoType,
                JSONReader.Feature.IgnoreAutoTypeNotMatch
        };
        return JSONB.parseObject(JSONB.toBytes(source), destClass,features);
    }

    public static void main(String[] args) {
         deepCopyTest();
    }

    private static void deepCopyTest() {
        Food food = new Food();
        food.setDate(LocalDate.of(2024,1,1));
        food.setName("AA1");
        food.setAge("18");
        Hanbao hanbao = new Hanbao();
        hanbao.setName("汉堡");
        food.setHanbao(hanbao);
        Hanbao clone = clone(food, Hanbao.class);
        System.out.println(clone);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private static class Food {
        private String name;
        private String age;
        private Hanbao hanbao;
        private LocalDate date;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private static class Hanbao {
        private String name;
//        private LocalDate date1;

    }
}

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。
希望可以正确执行

相关日志输出

请复制并粘贴任何相关的日志输出。

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions