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] Map转json后没有写入类型, 导致转换为java对象的时候报错 #507

Closed
ahgaoyong opened this issue Jun 25, 2022 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@ahgaoyong
Copy link

ahgaoyong commented Jun 25, 2022

问题描述

Map转json后没有写入类型, 导致转换为java对象的时候报错

环境信息

  • OS信息: [win10 i5 9400F 16 GB]
  • JDK信息: [Openjdk 1.8.0_332]
  • 版本信息:[Fastjson2 2.0.7]

重现步骤

如何操作可以重现该问题:

  1. 以下是使用1.2.83_noneautotype版本测试的,结果没有问题
public static void main(String[] args) {
        Map<Long, String> map = new HashMap<>();
        map.put(1L, "张三");
        map.put(2L, "张四");

        byte[] bytes = JSON.toJSONString(map, SerializerFeature.WriteClassName).getBytes(StandardCharsets.UTF_8);
        String str = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(str);
        System.out.println(JSON.parseObject(str, Object.class));
    }

结果:

{"@type":"java.util.HashMap",1L:"张三",2L:"张四"}
{1=张三, 2=张四}
  1. 以下是使用2.0.7版本测试的,结果有问题
public static void main(String[] args) {
        Map<Long, String> map = new HashMap<>();
        map.put(1L, "张三");
        map.put(2L, "张四");

        byte[] bytes = JSON.toJSONString(map, JSONWriter.Feature.WriteClassName).getBytes(StandardCharsets.UTF_8);
        String str = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(str);
        System.out.println(JSON.parseObject(str, Object.class, JSONReader.Feature.SupportAutoType));
    }

结果:

{1:"张三",2:"张四"}
Exception in thread "main" com.alibaba.fastjson2.JSONException: illegal character 1
	at com.alibaba.fastjson2.JSONReaderUTF16.readFieldNameHashCode(JSONReaderUTF16.java:635)
	at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:31)
	at com.alibaba.fastjson2.JSON.parseObject(JSON.java:629)

期待的正确结果

期望和1.2.83_noneautotype版本输出的结果一致

@ahgaoyong ahgaoyong added the bug Something isn't working label Jun 25, 2022
@wenshao wenshao added this to the 2.0.9 milestone Jun 25, 2022
@wenshao
Copy link
Member

wenshao commented Jun 25, 2022

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

@ahgaoyong
Copy link
Author

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

已验证通过,谢谢,希望尽快发布正式版本

@wenshao
Copy link
Member

wenshao commented Jul 10, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.9
问题已修复,请用新版本

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants