Why Does FastJSON Call isChinaName() During Serialization? Deep Dive & Fix

This article investigates a NullPointerException caused by FastJSON invoking the isChinaName() method during serialization, explains the underlying JavaBeanSerializer mechanisms, demonstrates debugging steps with code examples, and provides best‑practice guidelines using @JSONField annotations to control serialization behavior.

Architecture Digest
Architecture Digest
Architecture Digest
Why Does FastJSON Call isChinaName() During Serialization? Deep Dive & Fix

Problem Overview

A simple log line added before release triggered a series of alarms. After rolling back the log statement the service started normally again. The root cause was a NullPointerException that occurred during FastJSON serialization because the method isChinaName() was executed while the field country was still null.

Reproducing the Issue

Define a DTO class and a test case:

public class CountryDTO {
    private String country;

    public void setCountry(String country) { this.country = country; }
    public String getCountry() { return this.country; }
    public Boolean isChinaName() { return this.country.equals("中国"); }
}

public class FastJonTest {
    @Test
    public void testSerialize() {
        CountryDTO countryDTO = new CountryDTO();
        String str = JSON.toJSONString(countryDTO);
        System.out.println(str);
    }
}

Running the test produces a NullPointerException because this.country is null when isChinaName() is called.

Why Does Serialization Call isChinaName() ?

FastJSON builds a dynamic serializer class (e.g., ASMSerializer_1_CountryDTO) using ASM bytecode generation. The generated write() method ultimately invokes JavaBeanSerializer.write(), which reflects over the bean to discover getters and boolean‑style methods.

Key Methods in the Serialization Path

JavaBeanSerializer.write()
SerializeConfig#createJavaBeanSerializer()
TypeUtils#computeGetters()

During computeGetters() FastJSON iterates over all public methods, skips those with a void return type or with parameters, and treats methods starting with get or is as property accessors. If a method is annotated with @JSONField(serialize = false) it is excluded.

Three Typical Scenarios That Influence Serialization

@JSONField(serialize = false, name = "xxx")

– explicit exclusion.

Getter methods returning void – ignored.

Boolean‑style methods ( isXxx()) whose return type is not boolean – ignored.

Visualization

The following images illustrate the call chain and flowchart of FastJSON serialization:

Best‑Practice Code Example

Use @JSONField(serialize = false) to clearly mark methods that should not participate in serialization:

@JSONType(ignores = "otherName")
public class CountryDTO {
    private String country;

    public void setCountry(String country) { this.country = country; }
    public String getCountry() { return this.country; }

    @JSONField(serialize = false)
    public Boolean isChinaName() { return "中国".equals(this.country); }

    @JSONField(serialize = false)
    public void queryCountryList() { System.out.println("queryCountryList() executed!!"); }

    public String getEnglishName() { System.out.println("getEnglishName() executed!!"); return "lucy"; }
}

Running the serializer now prints only the desired fields without triggering the NPE.

Conclusion

The investigation follows the pattern Problem → Principle Analysis → Solution → Coding Standards . Understanding FastJSON’s dynamic serializer generation helps developers avoid hidden method invocations, and using explicit @JSONField annotations provides a maintainable way to control serialization across multiple systems.

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.

DebuggingFastJSONJava serializationJSONFieldJavaBeanSerializer
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

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.