Skip to content
Jim Earley edited this page May 1, 2022 · 2 revisions

Yet another JSON Library

There are several well known (and well-used) Java-based JSON libraries today. This one was originally an idea I had back in 2012(?) when I needed a JSON library that did some things that the libraries at that time didn't. And, like all things, I was curious to solve the problem in a different way. It's evolved since. In 2016(?) I added an event-driven parser that behaved similar to an XML SAX parser. It worked, but it was a bit kludgy, given some of the limitations of the time with Java 6.

Java has evolved significantly since, and this is an attempt at a complete rewrite of the library using more modern features:

  • Modules (Java 9): Modules were introduced in Java 9, with the benefits of improved class security (i.e., "hiding" public, internal classes not intended for instantiation). In that vein, the use of Modules supports SOLID principles. It also allows for taking advantage of different features as services, so it's easy to include only the features you need: If you only need to parse and read JSON data, then you can simply reference the JsonParser module as a dependency, and it will pull in the appropriate dependencies transitively. The JSON model is exposed as interfaces, with the underlying implementation managed in a separate module library. So if you want to create a new implementation for a given service, you can, without breaking the API.

  • Lambdas (Java 8): JSON structures (and JSON parsing) use a lot of looping and recursion. Lambdas are a great way to simplify the code dramatically, and incorporate functional programming to the problem.

  • Functional Interfaces (Java 8+): There are so many interesting aspects of these that can't be ignored. For example, the JsonPath takes full advantage of the Predicate<T> interface for path predicate expressions. The Function<T, R> interface is implemented for JsonPath functions.

  • Flow API (Java 9): First introduced in Java 9, and enhanced since, the Flow API introduces Reactive Programming patterns to Java. The original event-driven Parser has been completely rewritten using the Flow API, where a Json Processor (publisher) sends events to an Event Handler (subscriber), which processes the event and sends the event data to an Assembler. This allows the entire parser orchestration be more scalable, especially with larger JSON files, and provides greater flexibility to create custom Event Handlers and Assemblers for use cases such as filtering, and transforming data to other formats.

  • Record classes: Records are a perfect solution for simple data classes that don't require a lot of overhead. These are used in the JsonMapper Module (used for marshalling and unmarshalling to and from Json and Java classes) to address simple things like introspected method values.

The library is compiled with Java 17, enabling other newer features including additional String methods, pattern matching, improved switch handling, and so on.

Clone this wiki locally