JsonTypeInfo property seems to appear twice in JSON
See original GitHub issueHi, 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:
- Created 10 years ago
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 ofEXTERNAL_PROPERTY
. If you do observe such behavior, please file a bug atjackson-databind
; this repo is for Streaming part (JsonParser
,JsonGenerator
); object handling is implemented at higher layer.I’ve succeeded by removing the field
messageType
, but sustaining the annotation as it is.