Why FastJson’s Date Formatting Fails on Linux and How to Fix It
This article recounts the author's real‑world debugging of FastJson, covering unexpected date‑format priority issues on Linux, version mismatches, circular‑reference detection quirks, and practical tips such as disabling specific SerializerFeatures, while also reflecting on the library’s maintenance and migration to FastJson2.
FastJson is popular in China, but the author had never used it in a project until a new assignment explicitly prohibited its use.
After reading a FastJson article, the author tried it and encountered several problems, starting with a date‑format priority issue: a global pattern yyyy-MM-dd HH:mmss was set, but a field‑level annotation did not take effect in the Linux test environment, causing timestamps to retain seconds.
The author simulated Linux by adding -Dos.name=linux to VM options, but the issue persisted, leading to remote debugging and source code inspection of JSONSerializer.writeWithFormat without finding the cause.
Searching the FastJson source revealed a comment near JSONSerializer.dateFormatPattern referencing issue #1868. The corresponding GitHub issue (https://github.com/alibaba/fastjson/issues/1868) and pull request (https://github.com/alibaba/fastjson/pull/2706) showed that the bug was fixed in version 1.2.72. The test environment used an older 1.2.53 jar, which caused the failure.
The investigation also uncovered a circular‑reference detection problem. FastJson would emit $ref entries like "$ref":"$.result.young[1]", which the front‑end could not parse. The issue was resolved by disabling the feature with SerializerFeature.DisableCircularReferenceDetect, though the author noted that the code did not actually contain a true circular reference, only shared objects.
Further discussion highlighted other default behaviours such as SerializerFeature.WriteMapNullValue (which omits null fields) and SerializerFeature.WriteNullListAsEmpty (which can output empty lists). The author argued that such features should be opt‑in rather than enabled by default, to avoid unexpected production failures.
Overall, the author observed that FastJson’s test coverage and contributor resources are limited, leading to long‑standing bugs (e.g., issues #1868, #1968, #2029, #2452) that persisted for years. The library’s maintenance has shifted toward FastJson2, which the author hopes will avoid the pitfalls seen in FastJson.
Key takeaways include verifying library versions, checking GitHub issues before debugging, and configuring SerializerFeatures deliberately to prevent hidden bugs.
Review, testcase coverage is insufficient; many bugs depend on a few core contributors; default settings like circular‑reference detection can cause hidden failures; the author has moved to FastJson2, urging better stability.
Author: nyingping Source: juejin.cn/post/7215886869199863869
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
