Adding an alias to the @JsonTypeInfo
property lookup
#3979
-
Hi! My org has two JSON encoding schemes active in the system, one with minified keys and values and another one with unminified keys and values. These encodings are serialized and deserialized by independent class definitions. I am unifiying these classes, by adapting existing unminified-deserializing classes to decode both encoding schemes simultaneously, and deleting minified-deserializing classes. I have been well served by the I would like to add
Codifying this into a test: package example;
import static org.assertj.core.api.Assertions.assertThat;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
public class DiscriminatorAliasTest {
@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type", propertyAlias = "A?")
@JsonSubTypes({
@JsonSubTypes.Type(value = Square.class, names = { "square", "A" }),
@JsonSubTypes.Type(value = Circle.class, names = { "circle", "B" })
})
static abstract class Shape {
abstract public String name();
}
static class Square extends Shape {
@JsonProperty("width")
@JsonAlias("A")
public float width;
@Override
public String name() { return "square"; }
}
static class Circle extends Shape {
@JsonProperty("diameter")
@JsonAlias("B")
public float diameter;
@Override
public String name() { return "circle"; }
}
@Test
public void testDiscriminator() throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Passes
String squareJsonUnminified = "{\"type\":\"square\", \"width\": 1.0 }";
Shape square = mapper.readerFor(Shape.class).readValue(squareJsonUnminified);
assertThat(square.name()).isEqualTo("square");
// This fails (as expected)
String squareJsonMinified = "{\"A?\":\"A\", \"A\": 1.0 }";
Shape squareAgain = mapper.readerFor(Shape.class).readValue(squareJsonMinified);
assertThat(squareAgain.name()).isEqualTo("square");
}
} I've used the non-existent I'm wondering if there's a simple enough work around here using a semi-custom Thank-you in advance to all contributors and maintainers of this library. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Ok, I can see how this could be useful, but knowing how the implementation works, I must say that it is unlikely that alias support will be added for Type discriminator property. I suspect various work-arounds may exist; for example, reading of content first as |
Beta Was this translation helpful? Give feedback.
-
@MaxwellBo @cowtowncoder Can you please share more details how exactly this custom deserializer that replaces fields would look like? |
Beta Was this translation helpful? Give feedback.
Ok, I can see how this could be useful, but knowing how the implementation works, I must say that it is unlikely that alias support will be added for Type discriminator property.
I suspect various work-arounds may exist; for example, reading of content first as
JsonNode
, replacing "A?" with "type".But nothing obvious, nice and clean comes to mind unfortunately.