java 多态序列化

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.DatabindContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
import lombok.Data;

import java.io.IOException;

public class PartnerTypeIdResolver extends TypeIdResolverBase {
    private JavaType superType;

    @Override
    public void init(JavaType baseType) {
        superType = baseType;
    }

    @Override
    public String idFromValue(Object value) {
        return null;
    }

    @Override
    public String idFromValueAndType(Object value, Class<?> suggestedType) {
        return null;
    }

    @Override
    public JsonTypeInfo.Id getMechanism() {
        return null;
    }

    @Override
    public JavaType typeFromId(DatabindContext context, String id) throws IOException {
        char c = id.charAt(0);
        Class<?> subType = null;
        switch (c) {
            case '1':
                subType = Person.class;
                break;
            case '2':
                subType = Community.class;
                break;
            case '3':
                subType = Organization.class;
                break;
                default:
                    throw new RuntimeException("Invalid Partner type");
        }
        return context.constructSpecializedType(superType, subType);
    }
}

class Main {
    public static void main(String[] args) throws IOException {
        String json = "{\"id\":\"1\", \"name\":\"asd\", \"partnerType\":\"220\", \"communityClass\":\"1\"}";
        ObjectMapper mapper = new ObjectMapper();
        Partner partner = mapper.readValue(json, Partner.class);
        System.out.println(partner);
    }
}

@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.PROPERTY, property = "partnerType")
@JsonTypeIdResolver(PartnerTypeIdResolver.class)
class Partner {
    private Long id;
    private String name;
    private Integer partnerType;
}

@Data
class Person extends Partner{
    private Integer personClass;
}

@Data
class Community extends Partner{
    private Integer communityClass;
}

@Data
class Organization extends Partner{
    private Integer organisationClass;
}

 

posted @ 2022-04-18 19:13  liyx0618  阅读(85)  评论(0编辑  收藏  举报