This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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