Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[FEATURE] 全局序列化和反序列化支持枚举类型 #851

Closed
ilxqx opened this issue Oct 18, 2022 · 3 comments
Closed

[FEATURE] 全局序列化和反序列化支持枚举类型 #851

ilxqx opened this issue Oct 18, 2022 · 3 comments
Labels
bug Something isn't working enhancement New feature or request fixed
Milestone

Comments

@ilxqx
Copy link

ilxqx commented Oct 18, 2022

请描述您的需求或者改进建议

JSON全局自定义序列化器居然不支持枚举类,希望可以支持枚举类的自定义序列化和反序列化。

public interface FierceEnum {

    /**
     * 获取枚举值名称
     *
     * @return 名称
     */
    String getName();

    Integer getValue();

    /**
     * 获取枚举值描述
     *
     * @return 描述
     */
    default String getDescription() {
        return null;
    }

    static <T> T getEnum(final Class<T> clazz, final Integer value) {
        if (clazz.isEnum() && FierceEnum.class.isAssignableFrom(clazz)) {
            final T[] constants = clazz.getEnumConstants();
            for (T constant : constants) {
                final FierceEnum item = (FierceEnum) constant;
                if (item.getValue().equals(value)) {
                    return constant;
                }
            }
        }
        // 找不到或者不是枚举类,则返回null
        return null;
    }
}

@RequiredArgsConstructor
    @Getter
    public enum UserType implements FierceEnum {
        BOY(1, "男孩"),
        GIRL(2, "女孩");
        private final Integer value;
        private final String name;
    }

    @TableName("user")
    @Data
    @EqualsAndHashCode(callSuper = true)
    @ToString(callSuper = true)
    public static class User extends AbstractEntity {
        private String name;
        private Integer age;
        private UserType userType;
    }

@Test
    public void testUser() throws Throwable {
        JSON.register(UserType.class, new ObjectWriter<UserType>() {
            @Override
            public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
                jsonWriter.writeInt32(((UserType)object).getValue());
            }
        });
        JSON.register(LocalDateTime.class, new ObjectWriter<LocalDateTime>() {
            @Override
            public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
                jsonWriter.writeString(DateTimeUtils.toDateTimeString((LocalDateTime) object));
            }
        });
        User user = new User();
        user.setId("123");
        user.setName("张三");
        user.setUserType(UserType.BOY);
        user.setAge(20);
        user.setCreatedTime(LocalDateTime.now());
        user.setLastModifiedTime(LocalDateTime.now());
   
       JSON.toJSONString(user)
    }

输出结果:

{
	"age":20,
	"createdTime":"2022-10-18 16:00:28",
	"id":"123",
	"lastModifiedTime":"2022-10-18 16:00:28",
	"name":"张三",
	"userType":0
}

期望结果:

{
	"age":20,
	"createdTime":"2022-10-18 16:00:28",
	"id":"123",
	"lastModifiedTime":"2022-10-18 16:00:28",
	"name":"张三",
	"userType":1 // 注意,理论上自定义序列化器生效应该是:1
}

描述您考虑过的替代方案

暂无替代方案,只能用Jackson

@ilxqx ilxqx added the enhancement New feature or request label Oct 18, 2022
@ilxqx ilxqx changed the title [FEATURE] [FEATURE] 全局序列化和反序列化支持枚举类型 Oct 18, 2022
@wenshao wenshao added this to the 2.0.16 milestone Oct 20, 2022
@wenshao wenshao added bug Something isn't working fixed labels Oct 21, 2022
@wenshao
Copy link
Member

wenshao commented Oct 21, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.16-SNAPSHOT/
问题已经修复,请用2.0.16-SNAPSHOT帮忙验证,2.0.16版本预计在10月23日前发布

@wenshao
Copy link
Member

wenshao commented Oct 22, 2022

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

@wenshao wenshao closed this as completed Oct 22, 2022
@ilxqx
Copy link
Author

ilxqx commented Oct 22, 2022

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

👍🏻

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working enhancement New feature or request fixed
Projects
None yet
Development

No branches or pull requests

2 participants