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

在字段上使用JSONField注解中的deserializeUsing、serializeUsing无效不起作用。 #2213

Closed
zhouzhibing opened this issue Jan 24, 2024 · 4 comments
Labels
fixed question Further information is requested
Milestone

Comments

@zhouzhibing
Copy link

ListIntReader.java

public class ListIntReader implements ObjectReader<Object> {

    public static final ListIntReader INSTANCE = new ListIntReader();
    @Override
    public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        System.out.println("ListInt.Reader");
        return null;
    }
}

TextEntity.java

public class TextEntity {

    @JSONField(deserializeUsing = ListIntReader.class, serializeUsing = ListIntWriter.class)
    private List<Integer> idList;

    public static void main(String[] args) {
        String body = "{\"idList\":[1,2,3]}";
        TextEntity textEntity = JSON.parseObject(body , TextEntity.class, JSONReader.Feature.FieldBased);
        System.out.println();
    }
}

上述该示例,我在TextEntity.javaidList字段上,使用了JSONField该注解,并指定了序列化的反序列化自定义的使用,
@JSONField(deserializeUsing = ListIntReader.class, serializeUsing = ListIntWriter.class)
但实际运行时,无法去执行ListIntReader.java中的打印操作。

如果我想实现针对该类型该字段定制个性化序例化的效果,我该怎么操作?

  • OS信息: windows
  • JDK信息: jdk 21
  • 版本信息:'com.alibaba.fastjson2:fastjson2:2.0.45'
@zhouzhibing zhouzhibing added the question Further information is requested label Jan 24, 2024
@HongJian-Yang
Copy link

我也很好奇 这个 deserializeUsing 和 serializeUsing 的用法

@HongJian-Yang
Copy link

ListIntReader

public class ListIntReader implements ObjectReader<List<Integer>> {

    
    @Override
    public List<Integer> readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        String json = jsonReader.readString();
        return Arrays.stream(json.split("[\\[\\], ]+"))
                .filter(s -> !s.isEmpty())
                .map(Integer::parseInt)
                .collect(Collectors.toList());
    }

}

TextEntity

@Data // 要getset
public class TextEntity {

    @JSONField(deserializeUsing = ListIntReader.class)
    private List<Integer> idList;

    public static void main(String[] args) {
        String body = "{\"idList\":[1,2,3]}";
        TextEntity textEntity = JSON.parseObject(body , TextEntity.class);
        System.out.println(textEntity);
    }
}

@HongJian-Yang
Copy link

你没写getset所以没触发deserializeUsing

@wenshao wenshao added this to the 2.0.46 milestone Jan 27, 2024
@wenshao wenshao added the fixed label Jan 27, 2024
@wenshao
Copy link
Member

wenshao commented Jan 29, 2024

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

No branches or pull requests

3 participants