Why Using isSuccess Can Break JSON Serialization in Java

This article explains how different Java JSON libraries handle boolean getter naming, shows code examples of serialization results with fastjson, Gson, and Jackson, and recommends using a plain "success" field with an isSuccess() getter to avoid runtime bugs.

macrozheng
macrozheng
macrozheng
Why Using isSuccess Can Break JSON Serialization in Java

In daily development we often define boolean fields in classes, e.g., a field indicating whether a request succeeded. Naming conventions vary: some use success, others isSuccess. According to the JavaBeans Specification, non‑boolean properties use get/set, while boolean properties use is for the getter.

For a boolean property isSuccess, the getter should be isSuccess() and the setter setSuccess(boolean). However, IDEs may generate isSuccess() and getSuccess(), leading to ambiguity.

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

When serializing objects, different JSON libraries apply different strategies. fastjson and Jackson inspect getters, treating isSuccess() as a property named success, producing JSON {"success":true}. Gson inspects fields directly, yielding {"isSuccess":true}. Consequently, deserializing fastjson’s output with Gson results in the boolean remaining false.

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"; }
}
public class BooleanMainTest {
    public static void main(String[] args) throws IOException {
        Model model = new Model();
        model.setSuccess(true);
        System.out.println("Serializable Result With fastjson :" + JSON.toJSONString(model));
        Gson gson = new Gson();
        System.out.println("Serializable Result With Gson :" + gson.toJson(model));
        ObjectMapper om = new ObjectMapper();
        System.out.println("Serializable Result With jackson :" + om.writeValueAsString(model));
    }
}

This discrepancy can cause serious bugs in production. The recommended practice is to name the field success and keep the getter isSuccess(), fully complying with JavaBeans rules and ensuring consistent behavior across 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.

JavafastjsonGsonJacksonJavaBeansjson serializationboolean naming
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.