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

[FEATURE]建议NameFilter.process(...)方法签名传入Field/FieldInfo #484

Closed
vergilyn opened this issue Jun 17, 2022 · 2 comments
Closed
Labels
enhancement New feature or request
Milestone

Comments

@vergilyn
Copy link

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

需求:
部分java-bean中的boolean类型属性或者isSucceess()方法,序列化时json-key强制命名成isSuceess(默认key命名是success)。
(不期望 通过 JSONField 单独指定。或者是否还有别的更好的建议?

建议,NameFilter.process(...)扩展传入FieldInfo等信息。(当前方法签名NameFilter > process(Object object, String name, Object value)。)

否则,可能需要再次通过反射去获取FieldInfo。

请描述你建议的实现方案

传入与 name 对应的 FieldFieldInfo

描述您考虑过的替代方案

附加信息

@vergilyn vergilyn added the enhancement New feature or request label Jun 17, 2022
@vergilyn vergilyn changed the title [FEATURE]建议NameFilter.process(...)扩展传入FieldInfo等信息。 [FEATURE]建议NameFilter.process(...)方法签名传入Field/FieldInfo Jun 17, 2022
@wenshao wenshao added this to the 2.0.8 milestone Jun 17, 2022
@wenshao
Copy link
Member

wenshao commented Jun 17, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.8-SNAPSHOT/
已经支持,增加ContextNameFilter和ContextValueFilter,请用2.0.8-SNAPSHOT版本验证。使用方法如下

package com.alibaba.fastjson2.filter;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.PropertyNamingStrategy;
import com.alibaba.fastjson2.annotation.JSONField;
import com.alibaba.fastjson2.util.BeanUtils;
import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicReference;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ContextVNameFilterTest {
    @Test
    public void test() throws Exception {
        Bean bean = new Bean();
        bean.id = 10;

        AtomicReference<BeanContext> contextReference = new AtomicReference<>();

        ContextNameFilter filter = (BeanContext context, Object object, String name, Object value) -> {
            contextReference.set(context);
            return BeanUtils.fieldName(name, PropertyNamingStrategy.KebabCase.name());
        };

        assertEquals("{\"user-id\":10}", JSON.toJSONString(bean, filter));

        BeanContext context = contextReference.get();
        assertEquals(Bean.class, context.getBeanClass());
        assertEquals(int.class, context.getFieldClass());
        assertEquals(int.class, context.getFieldType());
        assertEquals(Bean.class.getField("id"), context.getField());
        assertEquals("userId", context.getAnnotation(JSONField.class).name());
        assertEquals(null, context.getFormat());
        assertEquals(null, context.getLabel());
        assertEquals(0, context.getFeatures());
    }

    public static class Bean {
        @JSONField(name = "userId")
        public int id;
    }
}

@wenshao
Copy link
Member

wenshao commented Jun 25, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants