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

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

Open
superwen0001 opened this issue May 21, 2024 · 6 comments
Labels
question Further information is requested

Comments

@superwen0001
Copy link

`
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<RuleOutputFieldDefinitionBO> list4 = (List<RuleOutputFieldDefinitionBO>) 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. 而不是我想要的类型了

@superwen0001 superwen0001 added the question Further information is requested label May 21, 2024
@superwen0001 superwen0001 changed the title 当我entity对象中存在type字段的时候, 反序列化不支持JSONReader.Feature.SupportAutoType 当我entity对象中存在type字段的时候, 反序列化JSONReader.Feature.SupportAutoType. 无法转为我想要的类型 May 21, 2024
@superwen0001
Copy link
Author

image

@yanxutao89
Copy link
Collaborator

@type 属性调整至首位试试

@superwen0001
Copy link
Author

@type 属性调整至首位试试
这样好像能够反序列化出来.
但是我是本身序列化出来的, 我怎么能让@type默认在首位呢

@superwen0001
Copy link
Author

还是不行,@type放到首位后. 反序列化回来的对象, 丢失String type字段了

@yanxutao89
Copy link
Collaborator

image
以WriteClassName来序列化对象,@type默认首位

@superwen0001
Copy link
Author

我找到问题了. 但是没有办法解决...
我实际上是写了一个JsonbTypeHandler extends AbstractJsonTypeHandler
对应了pgsql的jsonb类型.
写进前的时候是{"@type":"com.miyo.product.qingkong.logic.engine.abstraction.proj.bo.ProjectOverwriteTableBO","extensions":{"@type":"java.util.HashMap"},"id":"28a68367-c469-2d4d-0373-76f545122618"}}
写进去后被pgsql重排序
{"@type": "java.util.HashMap", "9c686998-29f0-d106-a694-73a4e76889e8": {"id": "9c686998-29f0-d106-a694-73a4e76889e8", "@type": "com.miyo.product.qingkong.logic.engine.abstraction.proj.bo.ProjectOverwriteRuleBO"}}

导致回来的时候, 没有办法反序列化为对应格式了. 这种有什么好办法处理吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants