This content originally appeared on DEV Community and was authored by Quang Nguyen
I. JSON in Financial Software Systems
Choosing a Message Format
While developing a financial software system, our team needed to choose an effective internal messaging format for communication between different modules. After evaluating several options, JSON emerged as a compelling choice due to its multiple advantages:
• Self-descriptive and Flexible: JSON provides a clear, transparent, and human-readable structure that can represent complex business logic. This minimizes debugging time, especially compared to binary formats.
• Easy Conversion from FIX: Messages received by our server are in the FIX protocol, which can be easily converted to JSON.
• Wide Adoption: JSON is ubiquitous. Third-party systems can easily integrate with ours using standard JSON formats, eliminating the need for lengthy documentation or complex onboarding.
• Ease of Use: JSON is simple to produce, parse, and validate. Numerous tools support its verification, facilitating integration and error identification during development.
Performance Considerations
Despite its strengths, JSON has known performance limitations, particularly in high-throughput environments. To ensure suitability, we conducted performance tests focusing on JSON generation, as this is the primary workload for our servers.
Test Setup
• Hardware: Tests were conducted on a modest system running Linux 2.6 with an AMD Ryzen 7 1700 Eight-Core CPU.
• JVM Configuration: Started with -Xms1500M.
• Library: JSON-simple was used to generate JSON objects.
• Source Code: JSONSimplePerf.java
• Test Details: The method produceUsingJSONSimple was executed repeatedly to generate ~1 million JSON objects. Elapsed time was logged to assess consistency. The first run (1,000 objects) served as a warm-up.
• Output: JSONSimpleOutput.txt
Results
The system consistently produced 1 million JSON objects in around 3 seconds. While sufficient for many real-world scenarios, our system’s requirement to handle over 100,000 messages per second highlighted performance concerns. Message complexity in our production system also exceeds that of the test cases.
Opportunities for Optimization
From analyzing the JSON-simple implementation, we identified potential inefficiencies:
• Map-Based Design: Each insertion invokes a hash function, which is unnecessary when we only generate (not retrieve) data.
• Limited Object Reuse: Repeated object creation triggers garbage collection (GC), which is undesirable in real-time systems.
• Indirect Byte Conversion: JSON-simple lacks a direct conversion method to byte arrays; strings must first be created, introducing overhead.
II. Enter JSON0: A High-Performance Alternative
To address these limitations, we tested the same scenarios using JSON0, a custom-built JSON library optimized for speed.
• Key Enhancements:
o Direct creation of child objects via createJSON() and createJSONArray().
o Support for object reuse using reset().
o Efficient toBytes() method to convert JSON directly into byte arrays.
• Results: JSON0Output.txt
JSON0 reduced the time to generate 1 million objects to approximately 1.35 seconds, more than twice as fast as JSON-simple.
Conclusion
Although performance bottlenecks are less common today due to faster hardware, JSON0 demonstrates that software-level optimizations can significantly improve throughput without relying on hardware upgrades. Moreover, the API remains developer-friendly and intuitive.
III. Installation & Usage
JSON0 is available on Maven Central:
<dependency>
<groupId>io.github.kwangng</groupId>
<artifactId>JSON0</artifactId>
<version>1.0.8</version>
</dependency>
Basic Example
JSON0 json = new JSON0();
json.reset();
json.put("Town", "Hoi An Town");
json.put("DayLength", 3);
json.put("Vehicles", "Taxi or Bike");
JSON0 visits = json.createJSON("MustVisits");
visits.put("Morning", "Tra Que Vegetable Village");
visits.put("Afternoon", "The Japanese Bridge");
visits.put("Evening", "An Bang Beach");
JSON0 mustTry = json.createJSONArray("MustTries");
JSON0 foods = mustTry.createJSON("Foods");
foods.put("Food1", "Banh Mi, Banh Xeo");
foods.put("Food2", "Com Ga Hoi An");
JSON0 drinks = mustTry.createJSON("Drinks");
drinks.put("Drink1", "Cafe Sua Da");
drinks.put("Drink2", "Mot Hoi An");
Common Methods
Create root JSON: new JSON0()
Add key-value pair: put(String key, Object value)
Get value: get(String key)
Create child JSON: createJSON(String key)
Create array of JSON objects: createJSONArray(String key)
Reset object for reuse: reset()
Convert to string: toString()
Convert to byte array: toBytes()
References
JSON0 GitHub Repository: https://github.com/quangnguyenvn/JSON0
This content originally appeared on DEV Community and was authored by Quang Nguyen