Backend Development 6 min read

FastJSON Serialization Mechanism and How to Control Method Invocation

This article analyzes a FastJSON serialization issue where a getter method is unexpectedly invoked, explains the underlying ASM-generated serializer workflow, details which methods are considered during serialization, and provides best‑practice code annotations to prevent unwanted method execution.

Architecture Digest
Architecture Digest
Architecture Digest
FastJSON Serialization Mechanism and How to Control Method Invocation

The author recounts a recent incident where adding a simple log line to a new feature caused a cascade of alerts after deployment, leading to a rollback. Investigation revealed that FastJSON serialization was invoking the isChinaName() method, which threw a NullPointerException because the country field was null.

To reproduce the problem, a CountryDTO class is defined with a String country field and an isChinaName() method that checks whether the country equals "中国". A test class FastJonTest serializes an instance of CountryDTO using JSON.toJSONString , which triggers the exception.

Debugging shows that FastJSON generates an ASM‑based serializer class ( ASMSerializer_1_CountryDTO ) that calls the JavaBeanSerializer's write() method. The serializer determines which getters to invoke via SerializeConfig#createJavaBeanSerializer and ultimately TypeUtils#computeGetters .

The computeGetters logic selects methods based on three criteria:

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

Methods starting with get

Methods starting with is

Consequently, isChinaName() is treated as a getter and executed during serialization, even though its field is null.

The article provides a comprehensive example illustrating four cases:

public class CountryDTO {
    private String country;
    public void setCountry(String country) { this.country = country; }
    public String getCountry() { return this.country; }
    public static void queryCountryList() { System.out.println("queryCountryList()执行!!"); }
    public Boolean isChinaName() { System.out.println("isChinaName()执行!!"); return true; }
    public String getEnglishName() { System.out.println("getEnglishName()执行!!"); return "lucy"; }
    @JSONField(serialize = false)
    public String getOtherName() { System.out.println("getOtherName()执行!!"); return "lucy"; }
    // additional cases omitted for brevity
}

Running the test prints the method calls and produces the JSON output:

isChinaName()执行!!
getEnglishName()执行!!
{"chinaName":true,"englishName":"lucy"}

To avoid unintended method execution, the author recommends explicitly marking methods that should not participate in serialization with @JSONField(serialize = false) . The revised CountryDTO class shows this annotation applied to static methods, non‑boolean getters, and other methods that should be ignored.

Finally, the article summarizes three frequently encountered serialization scenarios and emphasizes a workflow of problem discovery, principle analysis, solution implementation, and coding standards improvement.

JavaSerializationAnnotationsfastjsonASMJavaBean
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

login 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.