Skip to content

Instantly share code, notes, and snippets.

@chethanbandi
Last active August 25, 2017 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chethanbandi/46567745e28b669252b50cdd233f4ca8 to your computer and use it in GitHub Desktop.
Save chethanbandi/46567745e28b669252b50cdd233f4ca8 to your computer and use it in GitHub Desktop.
package com.adzcentral.core.service;
import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
@RunWith(BlockJUnit4ClassRunner.class)
public class JacksonTypeInfoTest {
@Test
public void test() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
//objectMapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT, "@class");
A whiteLabel = new A();
whiteLabel.setResourceId(1);
whiteLabel.setResourceType((short) 1);
whiteLabel.setContentType("image/jpg");
String json = objectMapper.writeValueAsString(whiteLabel);
System.out.println(json);
Object l2 = objectMapper.readValue(json, I.class);
System.out.println(l2.getClass());
System.out.println(l2);
Object l3 = objectMapper.readValue(json, Map.class);
System.out.println(l3.getClass());
System.out.println(l3);
}
@Getter
@Setter
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
static class A implements I {
private int resourceId;
private int resourceType;
private String contentType;
}
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
interface I {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment