Skip to content

Comments

FEATURE: Unified JSON Projection API#3854

Draft
jknack wants to merge 1 commit intomainfrom
3853
Draft

FEATURE: Unified JSON Projection API#3854
jknack wants to merge 1 commit intomainfrom
3853

Conversation

@jknack
Copy link
Member

@jknack jknack commented Feb 22, 2026

Provide a consistent way of filtering JSON output. This API allows developers to define exactly which fields of a Java object should be serialized, supporting both simple flat structures and deep nested graphs.

The API is designed to be library-agnostic, allowing the same projection definition to work across different JSON providers without requiring changes to domain models or POJOs.

Flexible Syntax: Supports Dot-notation (address.city) and Avaje/LinkedIn-style grouping (address(city, zip)).

Type-Safety: Full support for Java method references (User::getName) to provide refactor-safe projections.

Hierarchical Validation: Projections are validated against the target class hierarchy at definition time, including support for unwrapping Collections and Maps.

Fluent & Declarative API: A Projected wrapper for programmatic use and a @Project annotation for MVC controllers.

  • Programmatic (Script API):
get("/user/{id}", ctx -> {
  User user = service.find(ctx.path("id").value());
  return Projected.wrap(user)
    .include("id, name")
    .include(User::getAddress, addr -> addr.include("city"));
});
  • Declarative (MVC API):
@get
@project("id, name, address(city)")
public User getUser(String id) {
  return service.find(id);
}

The feature consists of three primary components that work together to define, wrap, and apply the filtering logic.

  1. Projection

The engine that parses syntax and validates paths against the class hierarchy.

// Manual definition
Projection<User> p = Projection.of(User.class)
    .include("id, address(city)");
  1. Projected
// Script API
get("/user", ctx -> {
  return Projected.wrap(user).include(User::getName);
});
  1. @Project

A declarative annotation for MVC controllers.

// MVC API
@get
@project("id, name")
public User getUser() { ... }

@jknack jknack added this to the 4.0.16 milestone Feb 22, 2026
@jknack jknack marked this pull request as draft February 22, 2026 23:07
@jknack
Copy link
Member Author

jknack commented Feb 22, 2026

@kliushnichenko here is some

@jknack
Copy link
Member Author

jknack commented Feb 22, 2026

@SentryMan I'm adding your json-view to jooby.

Provide a consistent way of filtering JSON output. This API allows developers to define exactly which fields of a Java object should be serialized, supporting both simple flat structures and deep nested graphs.

The API is designed to be library-agnostic, allowing the same projection definition to work across different JSON providers without requiring changes to domain models or POJOs.

Flexible Syntax: Supports Dot-notation (address.city) and Avaje/LinkedIn-style grouping (address(city, zip)).

Type-Safety: Full support for Java method references (User::getName) to provide refactor-safe projections.

Hierarchical Validation: Projections are validated against the target class hierarchy at definition time, including support for unwrapping Collections and Maps.

Fluent & Declarative API: A Projected<T> wrapper for programmatic use and a `@Project` annotation for MVC controllers.

- Programmatic (Script API):

```java
get("/user/{id}", ctx -> {
  User user = service.find(ctx.path("id").value());
  return Projected.wrap(user)
    .include("id, name")
    .include(User::getAddress, addr -> addr.include("city"));
});
```

- Declarative (MVC API):

```java
@get
@project("id, name, address(city)")
public User getUser(String id) {
  return service.find(id);
}
```

The feature consists of three primary components that work together to define, wrap, and apply the filtering logic.

1. Projection<T>

The engine that parses syntax and validates paths against the class hierarchy.

```java
// Manual definition
Projection<User> p = Projection.of(User.class)
    .include("id, address(city)");
```

2. Projected<T>

```java
// Script API
get("/user", ctx -> {
  return Projected.wrap(user).include(User::getName);
});
```

3. `@Project`

A declarative annotation for MVC controllers.

```java
// MVC API
@get
@project("id, name")
public User getUser() { ... }
```
@SentryMan
Copy link
Contributor

@SentryMan I'm adding your json-view to jooby.

Sweet, glad you like it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants