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 seems to appear twice in JSON

See original GitHub issue

Hi, I have the following class:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "messageType", visible = true)
@JsonSubTypes({  
  @Type(value = InstanMessage.class, name = "insant_sensor_data_sync"),  
  @Type(value = TimedMessage.class, name = "response_timed_sensor_data_sync") })
public abstract class Message {

@JsonProperty
protected String source;

@JsonProperty
protected String sourceId;

@JsonProperty
protected Long timestamp;

@JsonProperty
protected MessageType messageType;

@JsonProperty
protected String errorMessage;
}

When serializing:

public void serializes_InstantSensorDataSyncMessage() throws Exception {
    InstantSensorDataSyncMessage message = new InstantSensorDataSyncMessage();
    message.setTimestamp(12345L);
    message.setSource("Sun");
    message.setSourceId("1A2B3C");
    System.out.println(asJson(message));
    //assertThat(asJson(message), is(fixture("fixtures/instantSensorDataSync.json")));
}

public static String asJson(Object object) throws IOException {
    return MAPPER.writeValueAsString(object);
}

I got:

{"messageType":"insant_sensor_data_sync","source":"Sun","sourceId":"1A2B3C",
  "timestamp":12345,"messageType":null,"errorMessage":null,"payload"}

Why does messageType appear TWICE?

And is there’s a way NOT to make JsonTypeInfo property always appear first in the JSON?

version:

   <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.2.0</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.2.0</version>
    </dependency>

     <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.2.0</version>
    </dependency>

Thanks so much.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, May 19, 2016

This is not the correct issue tracker, check out jackson-databind. But as to the problem; no, it should not occur with new versions (2.7.4), although there may be some edge cases with inclusion of EXTERNAL_PROPERTY. If you do observe such behavior, please file a bug at jackson-databind; this repo is for Streaming part (JsonParser, JsonGenerator); object handling is implemented at higher layer.

0reactions
tsuda7commented, May 19, 2016

I’ve succeeded by removing the field messageType, but sustaining the annotation as it is.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicate JSON Field with Jackson - java - Stack Overflow
However I am having an issue with a field being twice in some cases. I have an abstract class: @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include ......
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 >
More Jackson Annotations - Baeldung
The @JsonAppend annotation is used to add virtual properties to an object in addition to regular ones when that object is serialized. This...
Read more >
Release Notes - Miredot
Comments are shown for rest interfaces with the @deprecated Javadoc tag. Support for @JsonUnwrapped (see Jackson Annotations). Support for @ ...
Read more >
Is there a jackson annotation to suppress unnecessary ...
Optional marker property that can be defined as true to force * wrapping of root ... I just want to show other possibility...
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