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] Boolean 字段private修饰 使用valueFilter 会有问题 #3076

Open
mystox opened this issue Oct 9, 2024 · 1 comment
Open

[BUG] Boolean 字段private修饰 使用valueFilter 会有问题 #3076

mystox opened this issue Oct 9, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@mystox
Copy link

mystox commented Oct 9, 2024

问题描述

实体中字段为Boolean时 想将对应空值转成“”时报错,
其中active 修饰为public的时候正常,
请问这种情况是设计如此还是存在bug?
补充一下 似乎 Integer也有这个问题,自定义的ObjectWriter 不生效

@DaTa
class User {
public String name;
public Integer age;
private Boolean active;

public User(String name, Integer age, Boolean active) {
    this.name = name;
    this.age = age;
    this.active = active;
}

}

public class Fastjson2ValueFilterExample {
public static void main(String[] args) {
User user = new User("Alice", null, null);

    // 定义一个 ValueFilter,将 null 值修改为默认值
    ValueFilter filter = (object, name, value) -> {
        if (value == null) {
            return "N/A"; // 如果字段值为 null,改为 "N/A"
        }
        return value;
    };

    // 使用 ValueFilter 序列化
    String jsonString = JSON.toJSONString(user, filter, JSONWriter.Feature.WriteNulls);
    System.out.println(jsonString);
    // 输出:{"name":"Alice","age":"N/A","active":"N/A"}
}

}

提示报错
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
at com.alibaba.fastjson2.writer.ObjectWriterImplBoolean.write(ObjectWriterImplBoolean.java:25)
at com.alibaba.fastjson2.writer.ObjectWriterAdapter.writeWithFilter(ObjectWriterAdapter.java:577)
at com.alibaba.fastjson2.writer.ObjectWriter3.write(ObjectWriter3.java:63)
at com.alibaba.fastjson2.JSON.toJSONString(JSON.java:3083)
at com.itime.debtdefuse.assets.util.Fastjson2ValueFilterExample.main(Fastjson2ValueFilterExample.java:34)

似乎Integer字段也有这个问题

参考代码:

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.writer.ObjectWriter;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**

  • @author Baoyi Chen
    */
    public class Main {
    public static void main(String[] args) {
    JSON.register(Integer.class, new ObjectWriter() {
    @OverRide
    public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
    Integer value = (Integer) object;
    if (value == null) jsonWriter.writeNull(); else jsonWriter.writeString(Long.toString(value));
    }
    });

     JSON.register(BigDecimal.class, new ObjectWriter<BigDecimal>() {
         @Override
         public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
             BigDecimal value = (BigDecimal) object;
             if (value == null) jsonWriter.writeNull(); else jsonWriter.writeString(value.toPlainString());
         }
     });
    
     Map<String, Long> map = new HashMap<>();
     map.put("0", 0L);
     String s = JSON.toJSONString(map);
     System.out.println(s);
    
     Test t = new Test();
     t.setValue(0);
     t.setValue1(new BigDecimal("0.11"));
     s = JSON.toJSONString(t);
     System.out.println(s);
    

    }

    public static class Test {
    private Integer value;
    private BigDecimal value1;

     public Integer getValue() {
         return value;
     }
    
     public BigDecimal getValue1() {
         return value1;
     }
    
     public void setValue(Integer value) {
         this.value = value;
     }
    
     public void setValue1(BigDecimal value1) {
         this.value1 = value1;
     }
    

    }
    }

@mystox mystox added the bug Something isn't working label Oct 9, 2024
rowstop added a commit to rowstop/fastjson2 that referenced this issue Oct 9, 2024
wenshao pushed a commit that referenced this issue Oct 12, 2024
…ype fields, for issue #3076 (#3077)

* nextIfComma supports ignoring all white space chars, for issue #2164

* Fix ClassCastException caused by using ValueFilter in Boolean/Short type fields, for issue #3076
@xcodemap
Copy link

@mystox 看代码走向,跟非public类有关。推荐使用 XCodeMap 走读源码,追踪对象字段的生命周期,快速定位有效分支。 https://xcodemap.tech/

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