Fastjson Boolean Property Naming Issue and How to Resolve It
This article explains why Fastjson incorrectly renames boolean fields prefixed with "is" during serialization, demonstrates the problem with sample Java code, analyzes the underlying reflection logic, and provides two practical solutions: following Alibaba Java guidelines or using @JSONField annotations.
Background
A colleague noticed that after serializing a Java object with Fastjson, the resulting JSON string unexpectedly contained an extra property and omitted the original boolean field prefixed with is . The issue appears when boolean getters follow the isXxx naming convention.
Reproduction
The following class reproduces the problem:
public class MyClass {
// boolean property
private boolean isActive;
private boolean valid;
// int property
private int id;
public MyClass() {}
public MyClass(boolean isActive, boolean valid, int id) {
this.isActive = isActive;
this.valid = valid;
this.id = id;
}
public boolean isActive() { return isActive; }
public void setActive(boolean isActive) { this.isActive = isActive; }
public boolean getValid() { return valid; }
public void setValid(boolean valid) { this.valid = valid; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
}Test code:
import com.alibaba.fastjson.JSON;
public class MyClassMain {
public static void main(String[] args) {
MyClass myClass = new MyClass(true, false, 123);
String jsonString = JSON.toJSONString(myClass);
System.out.println(jsonString);
}
}The output is {"active":true,"id":123,"valid":false} , showing the isActive field was renamed to active and the original isActive name disappeared.
Analysis
Fastjson builds bean information via com.alibaba.fastjson.serializer.SerializeConfig#createJavaBeanSerializer , which ultimately calls com.alibaba.fastjson.util.TypeUtils#computeGetters . When a method name starts with is , Fastjson treats it as a boolean getter and derives the property name by stripping the is prefix and decapitalizing the remainder. This conversion turns isActive into active , causing the mismatch.
Solutions
1. Follow Alibaba Java Development Manual : Avoid using the is prefix for boolean fields; name the field active instead of isActive . This prevents Fastjson from mis‑interpreting the getter.
2. Use @JSONField Alias : Annotate the field with @JSONField(name="isActive") to explicitly set the JSON property name, though this introduces Fastjson‑specific coupling.
public class MyClass {
@JSONField(name="isActive")
private boolean isActive;
private boolean valid;
// other members omitted
}Conclusion
For Java developers, reading the Alibaba Java Development Manual can prevent many subtle bugs like this, and when necessary, the @JSONField annotation offers a quick fix, albeit with higher library coupling.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.