Tatu, thanks for your persistent help! If my program is not correct, could
you post a sample (like in your new blog post) that simultaneously
configures an ObjectMapper to:
- use JsonTypeInfo (without annotations) for certain types and their
subclasses to disable polymorphic serialisation (if I use default typing)
- only use certain types and their subclasses to be serialized and
deserialized polymorphically (with class attribute) even to Object
target/generic types
- don't serialize Collections/Maps polymophically (only certain types)
- All instances of the resulting graph should be of same class as in the
original graph (according to config)
Thanks, Marc
Following is an other approach, where Test0 should not be
serialized/deserialized polymorphically, but it serializes Test0 which
leads to error (if @JsonTypeInfo is not set):
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import
com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
public class Main4 {
// @JsonTypeInfo(use = Id.NONE) // with this, it works
static class Test0 { public Object test1; }
static class Test1 { public Object exception; }
public static void main(String[] args) throws java.io.IOException,
ClassNotFoundException {
Test0 test0 = new Test0();
Test1 test = new Test1();
test.exception=new Exception("Test");
test0.test1=test;
PolymorphicTypeValidator ptv =
BasicPolymorphicTypeValidator.builder().allowIfSubType(Test1.class).allowIfSubType(Exception.class).build();
ObjectMapper mapper =
JsonMapper.builder().activateDefaultTyping(ptv,
DefaultTyping.NON_FINAL,
As.PROPERTY).disable(SerializationFeature.FAIL_ON_EMPTY_BEANS).build();
String json=mapper.writeValueAsString(test0);
System.out.println(json);
Test0 readValue = mapper.readValue(json, Test0.class);
System.out.println(readValue.test1.getClass());
System.out.println(readValue.getClass());
}}
--
You received this message because you are subscribed to the Google Groups
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jackson-user/94802aad-b212-4667-be35-ad28d6185586%40googlegroups.com.