question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@JsonTypeInfo property added twice to generated JSON: add support for `JsonType.As.EXISTING_PROPERTY`

See original GitHub issue

Hi,

I have an abstract super class annotated with

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")

where type is an existing property of the class.

The full class hierarchy looks like this:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
                      @JsonSubTypes.Type(value = SubClassA.class, name = "a"),
                      @JsonSubTypes.Type(value = SubClassB.class, name = "b")
              })
public abstract class AbstractSuperClass {
    @Transient
    private String type;

    protected AbstractSuperClass() {
        setType(getTypeValue());
    }
    protected abstract String getTypeValue();

    // getters and setters
}

public class SubClassA extends AbstractSuperClass {
    private double a;

    public SubClassA(double a) {
        this.a = a;
    }

    // getters and setters

    @Override
    protected String getTypeValue() {
        return "a";
    }
}

public class SubClassB extends AbstractSuperClass {

    private int b;

    public SubClassB(int b) {
        this.b = b;
    }

    // getters and setters

    @Override
    protected String getTypeValue() {
        return "b";
    }
}

Now when a SubClass of this class gets serialised, the type is added twice to the generated JSON. E.g. the JSON for a SubClassB would look like this:

{"type":"b","type":"b","b":42}

For the most part this does not seem to be an issue, but some clients (e.g. Postman) refuse this as Malformed JSON.

I know I could add e.g. @JsonTypeId or @JsonIgnore but in that case, type will not be included in generated JSON when using a List<AbstractSuperClass>, which is what I definitely need.

I assume that include = JsonTypeInfo.As.EXISTING_PROPERTY would solve my problem but this results in java.lang.IllegalStateException: Do not know how to construct standard type serializer for inclusion type: EXISTING_PROPERTY

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Jun 11, 2015

Original fix was in 2.5.0. There is one related fix (#222) that will be included in 2.6.0 (and 2.6.0-rc3 if such is released before 2.6.0 final).

0reactions
ouaibskycommented, Jun 11, 2015

Hi

Can you tell me in which version this fix has been released ? Thx Christophe

Read more comments on GitHub >

github_iconTop Results From Across the Web

@JsonTypeInfo property added twice to generated JSON: add ...
Hi, I have an abstract super class annotated with @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property ...
Read more >
Duplicate JSON Field with Jackson - java - Stack Overflow
I solved this by using the JsonTypeInfo.As.EXISTING_PROPERTY in the @JsonTypeInfo annotation. The project is open source, check it out here: ...
Read more >
Index (jackson-databind 2.11.4 API) - javadoc.io
Method for adding all property members from specified collector into this ... for JSON schema support (currently just ability to generate schemas using ......
Read more >
JsonTypeInfo.As (Jackson-annotations 2.4.0 API) - FasterXML
Inclusion mechanism that wraps typed JSON value (POJO serialized as JSON) in a JSON Object that has a single entry, where field name...
Read more >
Type info is not serialized when a @JsonTypeInfo element is a ...
@JsonTypeInfo (include = As.PROPERTY, use = Id.CLASS) ... work) but then the type information appears twice if you just serialize the object ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found