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

fix Enums toUtf8Bytes errors #1150

Merged
merged 1 commit into from Feb 27, 2023
Merged

fix Enums toUtf8Bytes errors #1150

merged 1 commit into from Feb 27, 2023

Conversation

lhaox
Copy link
Contributor

@lhaox lhaox commented Feb 27, 2023

What this PR does / why we need it?

一个对象中如果有属性是枚举类型,且枚举值为中文命名,对其进行toJSONBytes序列化时,并不能得到期望的值。测试代码如下所示:

@Test
public void testEnum() {
     Bean bean = new Bean();
     bean.setTestEnum(TestEnum.第二);
     bean.setName("zhangsan");
     byte[] bytes = JSON.toJSONBytes(bean, JSONWriter.Feature.WriteEnumsUsingName);
     System.out.println(new String(bytes, StandardCharsets.UTF_8));
}

@Data
static class Bean {
    private TestEnum testEnum;
    private String name;
}

@SuppressWarnings("all")
enum TestEnum {
    第,
    第二,
}

期望的结果:{"name":"zhangsan","testEnum":"第二"}
实际的结果:{"name":"zhangsan","testEnum":",�"}

经排查,com.alibaba.fastjson2.writer.FieldWriterEnum中使用了过时的getBytes方法复制bytes导致,此方法并不能真正意义上的对字节数组进行复制

Summary of your change

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed.

@CLAassistant
Copy link

CLAassistant commented Feb 27, 2023

CLA assistant check
All committers have signed the CLA.

@wenshao wenshao merged commit 8fded65 into alibaba:main Feb 27, 2023
@wenshao wenshao added this to the 2.0.25 milestone Feb 27, 2023
@wenshao wenshao added bug Something isn't working fixed labels Feb 27, 2023
@lhaox lhaox deleted the fix-utf8bytes branch February 28, 2023 01:54
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

Successfully merging this pull request may close these issues.

None yet

3 participants