The Behavioral Diversity of Java JSON Libraries

Nicolas Harrand, Thomas Durieux, David Broman, Benoit Baudry · DOI

Abstract

JSON has become the de facto standard for data exchange in modern software systems. While governed by specifications like RFC 8259, the practical implementation of these standards across different libraries reveals significant inconsistencies. This paper investigates the "behavioral diversity" of 20 popular Java JSON libraries, analyzing how they map data types and handle both well-formed and ill-formed inputs. Our findings show that while libraries generally agree on standard cases, they diverge sharply on edge cases and malformed data, presenting both a risk for system reliability and an opportunity for multi-version execution strategies.

Introduction

Most developers treat JSON libraries as interchangeable utilities, selecting them based on performance metrics or API familiarity. However, the specifications governing JSON contain intentional ambiguities, particularly regarding non-standard extensions and the handling of ill-formed data. When different services in a distributed system interpret the same JSON string differently, it can lead to subtle logic bugs, data corruption, or security vulnerabilities. This study systematically quantifies these differences across the Java ecosystem.

Methodology

The research involved a multi-stage analysis of 20 independent Java JSON libraries, including industry standards like Jackson and GSON, as well as specialized libraries like CookJSON. The methodology consisted of four primary components:

  1. Design Mapping: A manual audit of source code to determine how each library maps the six core JSON types (Object, Array, String, Number, Boolean, Null) to internal Java classes.
  2. Well-Formed Testing: A round-trip protocol where valid JSON is parsed and re-serialized to ensure data integrity and precision (e.g., checking for loss of precision in large numbers).
  3. Ill-Formed Testing: Stress-testing libraries with "broken" JSON to observe whether they correctly reject the input or attempt to "fix" it silently.
  4. Diversity Quantification: The application of a Jaccard-based "behavioral distance" metric to measure the frequency of disagreement between library pairs on identical inputs.

Dataset and Experimental Setup

The researchers curated a comprehensive corpus of 473 JSON files by aggregating several prominent benchmarks, including the official json.org test suite, the Native JSON Benchmark, and the "Parsing JSON is a minefield" suite. The final dataset contained 206 well-formed files and 267 ill-formed files, covering complex nested structures, extreme numerical values, duplicate keys, and various syntax errors.

Key Findings

Design Diversity

The study revealed a surprising lack of consensus in internal representation. Across the 20 libraries, the researchers identified 13 different ways to represent JSON Objects and 12 different ways to represent Numbers. For instance, while some libraries use Double for numbers, others utilize BigDecimal or maintain the value as a String until explicitly cast, leading to significant divergence in how high-precision decimals or massive integers are handled.

The "Helpfulness" Hazard

Many libraries implement a "best effort" parsing mode, silently accepting non-standard syntax like trailing commas or unquoted keys. While intended to be user-friendly, this "helpfulness" creates a hazard: two different systems might interpret the same malformed file in conflicting ways. The study found that libraries behaved identically on broken JSON for only 0.75% of the test files, indicating a massive level of disagreement on how to handle errors.

Performance and Conformance

In terms of strict conformance to the specification for well-formed data, GSON emerged as the "gold standard," achieving 100% conformance. While 17 out of 20 libraries maintained a conformance rate above 80%, the remaining 20% showed significant deviations even when processing valid data. Furthermore, the average behavioral distance (disagreement) between libraries was 0.13 for valid files, which nearly tripled to 0.33 when processing ill-formed files.

Discussion: The Power of the Crowd

One of the most significant insights from the research is that while individual libraries frequently fail on edge cases, 99.3% of all test files were handled correctly by at least one library in the set. This suggests that the inherent diversity of these implementations could be harnessed to build more resilient systems. By employing a "multi-version execution" (MVE) approach—where multiple libraries parse the same input and a voting mechanism determines the validity—developers could eliminate a wide class of "minefield" bugs in distributed environments.

Conclusion

The behavioral diversity of Java JSON libraries is a double-edged sword. While it currently introduces risks of inconsistency and data corruption, it also provides the necessary components for building high-reliability systems through redundancy. Developers should be aware that JSON libraries are not interchangeable and that the choice of library can fundamentally alter how a system processes data, especially at the boundaries of the specification.

Summary updated

BibTeX
@inproceedings{harrand2021behavioral,
  author = {Nicolas Harrand and Thomas Durieux and David Broman and Benoit Baudry},
  title = {{The Behavioral Diversity of Java JSON Libraries}},
  booktitle = {ISSRE},
  year = {2021},
  doi = {10.1109/issre52982.2021.00050},
}
Figure