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

[QUESTION] 反序列化 typeKey使用请教 #1488

Closed
Mrwcz opened this issue May 17, 2023 · 5 comments
Closed

[QUESTION] 反序列化 typeKey使用请教 #1488

Mrwcz opened this issue May 17, 2023 · 5 comments
Labels
enhancement New feature or request fixed question Further information is requested
Milestone

Comments

@Mrwcz
Copy link

Mrwcz commented May 17, 2023

我在反序列化不同子类对象时给typeKey配置一个自定义字段“aniType”,
如果aniType是String类型时,运行下面test()方法运行正常,可以得到正确的子对象;
如果aniType是Int类型时,运行下面test()方法直接报错
com.alibaba.fastjson2.JSONException: No suitable ObjectReader found foraniType, offset 12, character 1, line 1, column 13, fastjson-version 2.0.32 {"aniType":1,"dogName":"dog1001"}

请问aniType为Int类型时,需要如何配置可以正常解析?

@JSONType(seeAlso={Dog.class, Cat.class},typeKey = "aniType")
   public static class Animal {
      public int aniType;
   }

   @JSONType(typeName = "1" )
   public static class Dog extends Animal {
      public String dogName;
   }

   @JSONType(typeName = "2" )
   public static class Cat extends Animal {
      public String catName;
   }

   public static void test(){
      Dog dog = new Dog();
      dog.dogName = "dog1001";
      dog.aniType = 1;

      String text = JSON.toJSONString(dog);
      Dog cat = (Dog) JSON.parseObject(text,Animal.class);
   }
@Mrwcz Mrwcz added the question Further information is requested label May 17, 2023
@wenshao
Copy link
Member

wenshao commented May 17, 2023

目前还不支持non-string typeName,我考虑下怎么支持

@Mrwcz
Copy link
Author

Mrwcz commented May 18, 2023

extends

好的,谢谢关注

@wenshao wenshao added this to the 2.0.33 milestone May 18, 2023
@wenshao wenshao added the enhancement New feature or request label May 18, 2023
@wenshao
Copy link
Member

wenshao commented May 18, 2023

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

@wenshao wenshao added the fixed label May 18, 2023
@Mrwcz
Copy link
Author

Mrwcz commented May 19, 2023

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

用上面版本试过可以了,多谢🙏

@wenshao
Copy link
Member

wenshao commented May 29, 2023

@wenshao wenshao closed this as completed May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants