Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]自定义枚举序列化报空指针异常 #1729

Closed
snoopylovefiona opened this issue Aug 10, 2023 · 3 comments
Closed

[BUG]自定义枚举序列化报空指针异常 #1729

snoopylovefiona opened this issue Aug 10, 2023 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@snoopylovefiona
Copy link

问题描述

系统规范了枚举的使用方案,枚举统一使用@JSONField来处理。
并根据自己的枚举规范实现了序列化和反序列化能力。
从1升级到2后,这部分能力失效了。

环境信息

  • OS信息: [MacBook Pro (16-inch, 2019) 2.6 GHz 六核Intel Core i7 16 GB]
  • JDK信息: [e.g.:Openjdk 1.8.0_312]
  • 版本信息:[e.g.:Fastjson2 2.0.38]

重现步骤

  1. 自定义枚举序列化反序列化handler
@Slf4j
public class EnumJsonHandler implements ObjectSerializer, ObjectDeserializer {

    @Override
    public long getFeatures() {
        return ObjectSerializer.super.getFeatures();
    }

    @SuppressWarnings("unchecked")
    @Override
    public Object deserialze(DefaultJSONParser parser, Type type, Object o) {
      if (Objects.isNull(type)){
            throw new RuntimeException("类型为空");
        }
        if (type instanceof ParameterizedType) {
            //    规则实现
        } else {
            //    规则实现
        }
        return null;
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void write(JSONSerializer jsonSerializer, Object o, Object o1, Type type, int i) throws IOException {

    }

}
  1. 声明包含枚举的类
import com.alibaba.fastjson.annotation.JSONField;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class Demo {

    @JSONField(serializeUsing = EnumJsonHandler.class, deserializeUsing = EnumJsonHandler.class)
    private EnumDemo demo;

    enum EnumDemo {

        A,
        B,

    }
}
  1. 客户端调用
    public static void main(String[] args) {
        JSONObject object = new JSONObject();
        object.put("demo", 1);
        Demo compRule = JSONObject.toJavaObject(object,Demo.class);
        System.out.println(JSON.toJSONString(compRule));
    }

期待的正确结果

希望能拿到类型,根据类型来做规则处理。

相关日志输出

Exception in thread "main" java.lang.RuntimeException: 类型为空
at com.alibaba.amap.fiona.handler.EnumJsonHandler.deserialze(EnumJsonHandler.java:27)
at com.alibaba.fastjson.parser.deserializer.ObjectDeserializer.readObject(ObjectDeserializer.java:16)
at com.alibaba.fastjson2.reader.FieldReader.acceptAny(FieldReader.java:427)
at com.alibaba.fastjson2.reader.ObjectReaderAdapter.createInstance(ObjectReaderAdapter.java:627)
at com.alibaba.fastjson2.util.TypeUtils.cast(TypeUtils.java:1449)
at com.alibaba.fastjson.JSONObject.toJavaObject(JSONObject.java:87)
at com.alibaba.amap.fiona.handler.Demo.main(Demo.java:30)

附加信息

image

@snoopylovefiona snoopylovefiona added the bug Something isn't working label Aug 10, 2023
@wenshao wenshao added this to the 2.0.39 milestone Aug 13, 2023
@wenshao
Copy link
Member

wenshao commented Aug 13, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.39-SNAPSHOT/
问题已修复,请帮忙用2.0.39-SNAPSHOT版本验证,2.0.39版本预计在8月27日前发布

@snoopylovefiona
Copy link
Author

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.39-SNAPSHOT/ 问题已修复,请帮忙用2.0.39-SNAPSHOT版本验证,2.0.39版本预计在8月27日前发布
image

貌似还是给了空
image

@wenshao
Copy link
Member

wenshao commented Aug 15, 2023

@wenshao wenshao closed this as completed Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants