alibaba / alibaba/fastjson2

当我entity对象中存在type字段的时候, 反序列化JSONReader.Feature.SupportAutoType. 无法转为我想要的类型

Open
#2,604 6 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

`
String str = "[{\"type\": \"STRING11\", \"@type\": \"com.miyo.product.qingkong.logic.engine.abstraction.rule.bo.RuleOutputFieldDefinitionBO\", \"displayName\": \"_id\", \"logicalName\": \"_id\"}, {\"type\": \"STRING11\", \"@type\": \"com.miyo.product.qingkong.logic.engine.abstraction.rule.bo.RuleOutputFieldDefinitionBO\", \"displayName\": \"_desc\", \"logicalName\": \"_desc\"}, {\"type\": \"INTEGER\", \"@type\": \"com.miyo.product.qingkong.logic.engine.abstraction.rule.bo.RuleOutputFieldDefinitionBO\", \"displayName\": \"age\", \"logicalName\": \"age\"}]";
List list3 = (List) JSON.parseObject(str, Object.class,autoTypeFilter,JSONReader.Feature.SupportAutoType,JSONReader.Feature.ErrorOnNotSupportAutoType);

System.out.println(list3.get(0) instanceof RuleOutputFieldDefinitionBO);
System.out.println(list3);

String str1 = "[{\"@type\": \"com.miyo.product.qingkong.logic.engine.abstraction.rule.bo.RuleOutputFieldDefinitionBO\", \"displayName\": \"_id\", \"logicalName\": \"_id\"}, { \"@type\": \"com.miyo.product.qingkong.logic.engine.abstraction.rule.bo.RuleOutputFieldDefinitionBO\", \"displayName\": \"_desc\", \"logicalName\": \"_desc\"}, {\"type\": \"INTEGER\", \"@type\": \"com.miyo.product.qingkong.logic.engine.abstraction.rule.bo.RuleOutputFieldDefinitionBO\", \"displayName\": \"age\", \"logicalName\": \"age\"}]";
List list4 = (List) JSON.parseObject(str1, Object.class,autoTypeFilter,JSONReader.Feature.SupportAutoType,JSONReader.Feature.ErrorOnNotSupportAutoType);

System.out.println(list4.get(0) instanceof RuleOutputFieldDefinitionBO);
System.out.println(list4.get(0).getClass());

`

简单代码如上. 如果我json中存在了type字段. 就只能反序列化为jsonObject. 而不是我想要的类型了

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied JSON.parseObject reproduction with SupportAutoType and ErrorOnNotSupportAutoType, comparing the payloads that contain both type and @type with the working example. Trace the auto-type handling for the type property and verify that each list element is deserialized as RuleOutputFieldDefinitionBO rather than JSONObject. Done means the failing reproduction produces the requested class while preserving the existing working behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.