DEV Community

Yu Han
Yu Han

Posted on • Edited on

SJF4J in 5 Minutes: A Practical JSON Facade for Java

JSON handling in Java is rarely simple.

In real-world applications, data constantly moves between:

  • POJOs
  • Map / List
  • JSON strings
  • configuration files
  • APIs with evolving schemas

Java developers are often forced into a painful decision:

Either type safety, or flexibility — but not both.

SJF4J (Simple JSON Facade for Java) is built to remove that trade-off.


What Is SJF4J?

SJF4J is a lightweight facade over JSON libraries including Jackson, Gson, Fastjson2, and JSON-P, with support for YAML and Java Properties.

It provides a unified programming model for structured data processing, with support for JSON standards such as JSON Path, JSON Pointer, JSON Patch, and JSON Schema.

It does not replace your JSON parser — it unifies how you work with structured data above them.


The Core Idea: Object-Based Node Tree

Instead of introducing a custom JSON AST, SJF4J treats existing Java objects as JSON nodes.

This is called the Object-Based Node Tree (OBNT).

In OBNT:

  • JSON objects → JsonObject, Map, POJOs
  • JSON arrays → JsonArray, List, arrays
  • JSON values → native Java types

Everything stays plain Java — with JSON semantics layered on top.


Quick Example

JsonObject jo = JsonObject.fromJson("""
{
  "id": 1,
  "active": true
}
""");

int id = jo.getInt("id");
String activeText = jo.getAsString("active");
Enter fullscreen mode Exit fullscreen mode

SJF4J offers two access levels:

  • getXxx → strict typed access
  • getAsXxx → lenient cross-type conversion

You choose the conversion behavior per call


Path-Based Access

SJF4J fully supports:

  • JSON Path (RFC 9535)
  • JSON Pointer (RFC 6901)
String name = jo.getByPath("$.user.name");
List<Integer> ids = jo.findByPath("$.items[*].id", Integer.class);
Enter fullscreen mode Exit fullscreen mode

The same path API works across:

  • JSON
  • Maps / Lists
  • POJOs
  • Hybrid object graphs

Dynamic + Typed: JOJO

SJF4J introduces JOJO (JSON-Object Java Object) —
a domain object that extends JsonObject.

class User extends JsonObject {
    public String name;
}
Enter fullscreen mode Exit fullscreen mode
user.getName();               // typed field
user.getString("age");        // dynamic property
user.findByPath("$..name");   // JSON Path query
Enter fullscreen mode Exit fullscreen mode

You can start dynamic and gradually add structure — without breaking APIs.


Declarative Transformation with JsonPatch

SJF4J supports:

  • JSON Patch (RFC 6902)
  • JSON Merge Patch (RFC 7386)
JsonPatch patch = JsonPatch.diff(source, target);
patch.apply(source);
Enter fullscreen mode Exit fullscreen mode

Patches work uniformly across POJOs, Maps, Lists, and JSON objects.


When Does SJF4J Shine?

SJF4J is ideal if you:

  • Work with evolving or semi-structured data
  • Need both flexibility and type safety
  • Want one API across multiple JSON libraries
  • Care about JSON specifications
  • Need JSON Patch, JSON Schema validation, or compile-time object mapping

Summary

SJF4J provides JSON-oriented APIs for Java without locking applications into one JSON backend or one object representation.

It is small, composable, and specification-driven.

👉 GitHub: https://github.com/sjf4j-projects/sjf4j

Top comments (0)