Skip to content

@JsonView should allow the same property to have different typesΒ #6272

Open

Description

@fabzzap

Is your feature request related to a problem? Please describe.

Let's have 2 different interfaces

public interface OneType {}

and

public interface AnotherType {}

.
This should be possible

package org.example;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;

public class Serializable {
    private final List<Integer> number;

    @JsonView(OneType.class)
    @JsonProperty("number")
    int getSum() {
        return number.stream().mapToInt(x->x).sum();
    }

    public Serializable(List<Integer> number) {this.number = number;}

    @JsonView(AnotherType.class)
    List<Integer> getNumber() {return number;}
}

. However, serializing such an instance fails.

        Serializable serializable = new Serializable(List.of(2, 3));
        ObjectMapper objectMapper = new ObjectMapper();
        IO.println("one type");
        IO.println(objectMapper.writerWithView(OneType.class).writeValueAsString(serializable));
        IO.println("another type");
        IO.println(objectMapper.writerWithView(AnotherType.class).writeValueAsString(serializable));

it types one type then fails at runtime with

Exception in thread "main" tools.jackson.databind.exc.InvalidDefinitionException: Invalid type definition for type `org.example.Serializable`: Conflicting getter definitions for property "number": org.example.Serializable#getNumber() vs org.example.Serializable#getSum()

.

Of course it succeeds if @JsonProperty("number") is changed to give the property a different name. But the point is, the @JsonView annotations mean that the property number has exactly one type.

Describe the solution you'd like

The output should be

one type
{"number":5}
another type
{"number":[2,3]}

Usage example

No response

Additional context

No response

Activity

  1. added
    to-evaluateIssue that has been received but not yet evaluated
    on Oct 10, 2026
  2. cowtowncoder commented on Oct 11, 2026

    @cowtowncoder
    Member

    I doubt this can or will be supported due to complexity in trying to support heterogenous per-view types in property introspection system; but will leave open as a request in case someone wants to tackle a particularly hard problem. :)

  3. JooHyukKim commented on Oct 11, 2026

    @JooHyukKim
    Member

    Seems too complex honestly πŸ€”

  4. removed
    to-evaluateIssue that has been received but not yet evaluated
    on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions