Why Using isSuccess Can Break JSON Serialization in Java and How to Fix It

This article explains how different Java boolean naming conventions affect JSON serialization across fastjson, Gson, and Jackson, demonstrates the resulting inconsistencies with code examples, and recommends using a plain "success" field with an isSuccess getter to ensure reliable cross‑library behavior.

macrozheng
macrozheng
macrozheng
Why Using isSuccess Can Break JSON Serialization in Java and How to Fix It

In daily development we often define boolean fields in classes, e.g., a field indicating whether a request succeeded.

Developers use different naming conventions such as success or isSuccess, and IDEs often generate isSuccess getters automatically.

According to the JavaBeans Specification, a normal property propertyName has getters/setters getPropertyName() and setPropertyName(...). For boolean properties the getter is isPropertyName() and the setter remains setPropertyName(...).

public <PropertyType> get<PropertyName>();
public void set<PropertyName>(<PropertyType> a);

For a boolean property the signatures become:

public boolean is<PropertyName>();
public void set<PropertyName>(boolean m);

If the property is named success, the getter should be isSuccess (or getSuccess) and the setter setSuccess. If the property itself is named isSuccess, the getter would be isIsSuccess or getIsSuccess, which is confusing.

Consider the following JavaBean:

class Model implements Serializable {
    private static final long serialVersionUID = 1836697963736227954L;
    private boolean isSuccess;

    public boolean isSuccess() { return isSuccess; }
    public void setSuccess(boolean success) { isSuccess = success; }
    public String getHollis() { return "hollischuang"; }
}

When this class is serialized with different JSON libraries, the results differ:

Serializable Result With fastjson :{"hollis":"hollischuang","success":true}
Serializable Result With Gson :{"isSuccess":true}
Serializable Result With jackson :{"success":true,"hollis":"hollischuang"}

Fastjson and Jackson inspect getter methods, treat isSuccess as a property named success, and therefore output "success". Gson, on the other hand, inspects fields directly and outputs the original field name "isSuccess".

Consequently, if an object serialized by fastjson (or Jackson) is deserialized by Gson, the isSuccess field will be missing and default to false, which can cause serious bugs in production.

To avoid this inconsistency, the recommendation is to name the field success and keep the getter isSuccess. This follows the JavaBeans convention and yields identical results across all major JSON serialization frameworks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JSONfastjsonGsonJacksonJavaBeansnaming
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.