Treat Java Objects Like JSON
Java is powerful, but working with nested objects can be tedious. Getters, setters, null checks, conversions — a lot of code for simple operations.
What if you could use JSONPath, JSON Schema, and JSON Patch directly on Java objects?
That's what SJF4J (Simple JSON Facade for Java) does. No intermediate JSON trees, no serialization required.
Start with an Ordinary Java Object
Consider two simple classes:
public class User {
public String name;
public int age;
public Address address;
}
public class Address {
public String city;
}
Nothing special here. No annotations, interfaces, or base classes.
Now suppose we have a User instance:
User user = ...;
Navigate with JSONPath
With SJF4J, we can use JSONPath directly on this object:
JsonPath path = JsonPath.parse("$.address.city");
// Read
String city = path.getString(user);
// Update
path.put(user, "Shanghai");
That's it.
No serialization, no intermediate JSON tree, and no conversion back to a Java object.
We're simply treating the Java object as a JSON-like structure.
The same approach works with Map, List, arrays, and other supported object models.
Validate with JSON Schema
JSONPath is just the beginning.
Suppose we want to validate a User against a JSON Schema:
JsonSchema schema = JsonSchema.fromJson("""
{
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0 }
},
"required": ["name"]
}
""");
ValidationResult result =
schema.createPlan().validate(user);
Yes, user is still an ordinary Java object.
SJF4J supports JSON Schema Draft 2020-12, 2019-09, and 7, allowing the same validation rules to work across different data representations.
Modify with JSON Patch
We can also apply standard JSON Patch operations directly:
JsonPatch patch = JsonPatch.fromJson("""
[
{
"op": "replace",
"path": "/name",
"value": "Alice Zhang"
}
]
""");
patch.apply(user);
The original User object is updated in place.
This can be useful for applying structured updates without writing property-specific mutation logic.
How Does It Work?
The key is OBNT (Object-Based Node Tree), SJF4J's unified structural model.
Instead of converting everything into a dedicated JSON tree, SJF4J recognizes three basic structural concepts:
| Node | Java representations |
|---|---|
| Object | POJO, Map, JsonObject |
| Array | List, Java array, JsonArray |
| Value | String, Number, Boolean, custom values |
This allows JSON-oriented operations to work consistently across different Java object representations.
Think of it as a common structural vocabulary for data that already exists in memory.
Runtime Flexibility, Compiled Speed
Dynamic structural operations are convenient, but Java also has a powerful advantage: the compiler.
SJF4J takes advantage of this through Compiled APIs, using annotation processing to generate optimized Java implementations at compile time.
For example:
@CompiledNavigator
interface UserNavigator {
@GetByPath("$.address.city")
String getCity(User user);
}
Instead of interpreting the JSONPath expression at runtime, the generated implementation can access Java fields directly:
// Simplified generated code
public String getCity(User user) {
if (user == null) return null;
Address address = user.address;
return address == null ? null : address.city;
}
The result is ordinary Java code: readable, debuggable, and easy for the JVM to optimize.
And the performance difference can be substantial.
In SJF4J's benchmarks, Compiled APIs achieve up to 50× faster execution than their Runtime counterparts in selected workloads. Actual gains depend on the operation and data structure.
This gives developers the flexibility of dynamic JSON-oriented APIs, alongside the performance benefits of statically generated Java code.
Beyond JSONPath
SJF4J brings several related capabilities together:
- Binding: JSON/YAML serialization and deserialization.
- Navigation: JSONPath and JSON Pointer.
- Validation: JSON Schema.
- Mapping: Object-to-object mapping.
- Patching: JSON Patch and Merge Patch.
It also supports multiple backends, including Jackson 2/3, Gson, Fastjson2, JSON-P, and SnakeYAML.
The goal isn't to replace all these libraries. It's to provide a consistent way to work with structured data, regardless of its underlying Java representation.
Final Thoughts
Java gives us a robust type system and a powerful compiler. JSON gives us a flexible structural model and a rich set of standards.
SJF4J brings these strengths together.
We can work with native Java objects using JSON semantics, and when performance matters, compile those operations into optimized Java code.
That's the idea behind SJF4J: JSON semantics, native Java objects, and compile-time performance.
If you're interested:
Feedback and ideas are welcome!
Top comments (0)