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]自定义反序列化异常 #1249

Closed
SummerDream-z opened this issue Mar 17, 2023 · 2 comments
Closed

[BUG]自定义反序列化异常 #1249

SummerDream-z opened this issue Mar 17, 2023 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@SummerDream-z
Copy link

问题描述

自定义反序列化,偶尔成功一次,大部分情况执行时异常:

Caused by: java.lang.IllegalArgumentException: wrong number of arguments
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.alibaba.fastjson2.reader.FieldReaderObject.accept(FieldReaderObject.java:264)

环境信息

java sdk: OpenJDK 1.8.0_281
fastjson: 2.0.21

重现步骤

定义

@Data
public class CallInfo {

    private String phoneNum;

    @JSONField(deserializeUsing = JsonDeserializer.class)
    private Map<String, Object> varDefine;

    @JSONField(deserializeUsing = TagDeserializer.class)
    private List<Tag> tagSession;
}
@Data
public class Tag {
    private String tagId;
    private String tagName;
}
public class JsonDeserializer implements ObjectReader<Map<String, Object>> {
    @Override
    public Map<String, Object> readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        String value = jsonReader.readString();
        return JSONObject.parseObject(value);
    }
}
public class TagDeserializer implements ObjectReader<List<Tag>> {
    @Override
    public List<Tag> readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        String value = jsonReader.readString();
        return JSON.parseArray(value, Tag.class);
    }
}

使用

public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("phoneNum", "1");
        map.put("varDefine", "{\"success\":\"yes\"}");
        map.put("tagSession", "[{\"tagId\":\"6908494829\",\"tagName\":\"不是本人\"}]");
        CallInfo callInfo = JSON.parseObject(JSON.toJSONString(map), CallInfo.class);
    }

相关日志输出

Exception in thread "main" com.alibaba.fastjson2.JSONException: read field 'CallInfo.getVarDefine, offset 117, character }, line 1, column 118, fastjson-version 2.0.21 {"phoneNum":"1","tagSession":"[{\"tagId\":\"6908494829\",\"tagName\":\"不是本人\"}]","varDefine":"{\"success\":\"yes\"}"}
	at com.alibaba.fastjson2.reader.FieldReaderObject.readFieldValue(FieldReaderObject.java:162)
	at com.alibaba.fastjson2.reader.ObjectReader3.readObject(ObjectReader3.java:337)
	at com.alibaba.fastjson2.JSON.parseObject(JSON.java:601)
	at Test.main(Test.java:20)
Caused by: com.alibaba.fastjson2.JSONException: set varDefine error
	at com.alibaba.fastjson2.reader.FieldReaderObject.accept(FieldReaderObject.java:268)
	at com.alibaba.fastjson2.reader.FieldReaderObject.readFieldValue(FieldReaderObject.java:147)
	... 3 more
Caused by: java.lang.IllegalArgumentException: wrong number of arguments
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.alibaba.fastjson2.reader.FieldReaderObject.accept(FieldReaderObject.java:264)
	... 4 more
@SummerDream-z SummerDream-z added the bug Something isn't working label Mar 17, 2023
@wenshao wenshao added this to the 2.0.26 milestone Mar 17, 2023
@wenshao wenshao added the fixed label Mar 17, 2023
@wenshao
Copy link
Member

wenshao commented Mar 17, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.26-SNAPSHOT/
问题已经修复,请帮忙用2.0.26-SNAPSHOT版本验证

@wenshao
Copy link
Member

wenshao commented Mar 19, 2023

@wenshao wenshao closed this as completed Mar 19, 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