DEV Community

Yu Han
Yu Han

Posted on

SJF4J: A Structured JSON Facade for Java

Introduction

Working with JSON in Java usually means choosing between two approaches:

  • Data binding (POJO) — strong typing, but rigid
  • Tree model (JsonNode / Map) — flexible, but unstructured

Most libraries force you to pick one and live with the trade-offs.

SJF4J (Simple JSON Facade for Java) takes a different approach:
it introduces a unified JSON-semantic layer that supports both structured and dynamic access — without locking you into a single model.


What SJF4J Is

SJF4J is not a JSON parser.
It is a facade layer built on top of existing parsers (e.g., Jackson), providing:

  • Unified node model
  • Structured + dynamic access
  • Schema-aware capabilities
  • JSON-semantic operations

The goal is to make JSON handling more consistent, expressive, and extensible.


Core Idea: Unified Node Semantics

In SJF4J, all JSON values are treated as nodes with consistent behavior.

You can operate on them via:

  • Object-oriented APIs (JsonObject, JsonArray)
  • Static utilities (Nodes)

Two access styles are supported:

  • toXxx() → type-safe access
  • asXxx() → semantic conversion

This removes the need to constantly switch between parsing styles.


Structured + Dynamic in One Model

A key feature is that SJF4J does not force a strict boundary between POJO and dynamic JSON.

You can define structured fields while still allowing flexible extensions.

Example

public class Student extends JsonObject {
    private String name;
    private Map<String, Integer> scores;
    private List<Student> friends;
}
Enter fullscreen mode Exit fullscreen mode

This is neither a traditional POJO nor a raw JSON tree — it’s a hybrid model.

SJF4J calls this pattern JOJO (JSON Object Java Object).


Path and Patch Support

SJF4J includes built-in support for:

  • JSON Path-like navigation
  • Patch-style updates

This allows you to manipulate deeply nested structures without manual traversal.

student.putByPath("$.profile.name", "Alice");
Enter fullscreen mode Exit fullscreen mode

Schema Integration

SJF4J integrates JSON Schema as a runtime concept:

  • Validate data dynamically
  • Support conditional rules
  • Separate domain invariants from runtime constraints

This complements traditional validation approaches like Jakarta Bean Validation.


Why Not Just Use Jackson or Gson?

Jackson and Gson are excellent libraries, but they focus on:

  • Serialization / deserialization
  • Data binding

SJF4J focuses on a different layer:

  • JSON as a structured data model
  • Cross-cutting operations (path, patch, schema)
  • Hybrid static + dynamic access

It does not replace existing libraries — it builds on top of them.


Performance Considerations

SJF4J operates on top of existing parsers, so performance depends on the underlying implementation.

In typical scenarios:

  • Overhead is modest
  • In some cases (e.g., JOJO + optimized IO), performance can be comparable or even slightly better

The design goal is capability without significant cost.


When It Makes Sense

SJF4J is useful when:

  • JSON structure is partially dynamic
  • You need both typed access and flexible fields
  • You work with evolving schemas
  • You want a unified model for JSON operations

It may be less necessary if:

  • Your schema is completely static
  • You only need simple serialization/deserialization

Conclusion

SJF4J explores a different layer in the JSON stack:

Not parsing, not binding — but structuring.

It provides a way to treat JSON as a first-class data model in Java, combining:

  • Structure
  • Flexibility
  • Semantic operations

Project: https://github.com/sjf4j-projects/sjf4j

Top comments (0)