`
rensanning
  • 浏览: 3566423 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:38527
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:608780
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:684149
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:90387
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:402972
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:69935
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:92241
社区版块
存档分类
最新评论

Jackson实例入门

 
阅读更多
Jackson:Java平台的JSON解析器。

版本:
jackson-databind-2.8.8.1.jar
jackson-core-2.8.8.jar
jackson-annotations-2.8.8.jar

1.基本

字符串和对象间的转换
    private static void object2String() throws JsonProcessingException {
        People p = new People();
        p.id = 101;
        p.name = "rensanning101";

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);

        System.out.println(json); // {"id":101,"name":"rensanning101"}
    }

    private static void string2Object() throws JsonParseException, JsonMappingException, IOException {
        String json = "{\"id\":102, \"name\":\"rensanning102\"}";

        ObjectMapper mapper = new ObjectMapper();
        People p = mapper.readValue(json, People.class);

        System.out.println(p); // People [id=102, name=rensanning102]
    }

    static class People {
        public int id;
        public String name;
        @Override
        public String toString() {
            return "People [id=" + id + ", name=" + name +"]";
        }
    }


字符串和数组间的转换
    private static void objectArray2String() throws JsonProcessingException {
        People p1 = new People();
        p1.id = 103;
        p1.name = "rensanning103";

        People p2 = new People();
        p2.id = 104;
        p2.name = "rensanning104";

        People[] p = new People[2];
        p[0] = p1;
        p[1] = p2;

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);

        System.out.println(json); // [{"id":103,"name":"rensanning103"},{"id":104,"name":"rensanning104"}]
    }

    private static void string2ObjectArray() throws JsonParseException, JsonMappingException, IOException {
        String json = "[{\"id\":105,\"name\":\"rensanning105\"},{\"id\":106,\"name\":\"rensanning106\"}]";

        ObjectMapper mapper = new ObjectMapper();
        People[] pp = mapper.readValue(json, People[].class);

        System.out.println(pp);
    }


字符串和集合间的转换
    private static void objectList2String() throws JsonProcessingException {
        People p1 = new People();
        p1.id = 107;
        p1.name = "rensanning107";

        People p2 = new People();
        p2.id = 108;
        p2.name = "rensanning108";

        List<People> p = new ArrayList<People>();
        p.add(p1);
        p.add(p2);

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);

        System.out.println(json); // [{"id":107,"name":"rensanning107"},{"id":108,"name":"rensanning108"}]
    }

    private static void string2ObjectList() throws JsonParseException, JsonMappingException, IOException {
        String json = "[{\"id\":109,\"name\":\"rensanning109\"},{\"id\":110,\"name\":\"rensanning110\"}]";

        ObjectMapper mapper = new ObjectMapper();
        @SuppressWarnings("unchecked")
        List<People> pp = mapper.readValue(json, List.class);

        System.out.println(pp); // [{id=109, name=rensanning109}, {id=110, name=rensanning110}]
    }


其他
    private static void readValueFromFile() throws JsonParseException, JsonMappingException, IOException {
        String path = new File(".").getAbsoluteFile().getParent();
        File file = new File(path +"/people.json");

        ObjectMapper mapper = new ObjectMapper();
        People people = mapper.readValue(file, People.class);

        System.out.println(people); // People [id=111, name=rensanning111]
    }

引用
{"id":111,"name":"rensanning111"}


    private static void object2StringWithProtectedField() throws JsonProcessingException {
        PeopleField p = new PeopleField();
        p.id = 112;
        p.name = "rensanning112";
        p.setGender("male");

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);

        System.out.println(json); // {"id":112,"name":"rensanning112","gender":"male"}
    }

    static class PeopleField {
        // public字段不需要Getter/Setter
        public int id;
        public String name;
        // protected/private字段需要Getter/Setter
        protected String gender;
        public String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        @Override
        public String toString() {
            return "PeopleField [id=" + id + ", name=" + name + ", gender=" + gender +"]";
        }
    }


2.设置

SerializationFeature
    private static void settingsSerial() throws JsonProcessingException {
        People p = new People();
        p.id = 201;
        p.name = "rensanning201";

        ObjectMapper mapper = new ObjectMapper();
        // 通过enable() / disable()方法设置
        // https://github.com/FasterXML/jackson-databind/wiki/Serialization-Features
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        String json = mapper.writeValueAsString(p);

        System.out.println(json);
//      {
//        "id" : 201,
//        "name" : "rensanning201"
//      }
    }


DeserializationFeature
    private static void settingsDeserial() throws JsonParseException, JsonMappingException, IOException {
        String json = "{\"id\":202, \"name\":\"rensanning202\", \"ttt\":\"123456\"}";

        ObjectMapper mapper = new ObjectMapper();
        // 通过enable() / disable()方法设置
        // https://github.com/FasterXML/jackson-databind/wiki/Deserialization-Features
        // 如果没有以下设置会报错:UnrecognizedPropertyException: Unrecognized field "ttt"
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        People p = mapper.readValue(json, People.class);

        System.out.println(p); // People [id=202, name=rensanning202]
    }


3.类型

TypeReference
    private static void deserialTypeRef() throws JsonParseException, JsonMappingException, IOException {
        String json = "[{\"id\":301, \"name\":\"rensanning301\"}, {\"id\":302, \"name\":\"rensanning302\"}]";

        ObjectMapper mapper = new ObjectMapper();
        TypeReference<List<People>> valueTypeRef = new TypeReference<List<People>>() {};
        List<People> list = mapper.readValue(json, valueTypeRef);

        System.out.println(list); // [People [id=301, name=rensanning301], People [id=302, name=rensanning302]]
    }


4.注解

@JsonProperty
    private static void annotationJsonProperty() throws JsonProcessingException {
        PeopleJsonProperty p = new PeopleJsonProperty();
        p.id = 401;
        p.name = "rensanning 401 JsonProperty";

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);

        System.out.println(json); // {"id":401,"newName":"rensanning 401 JsonProperty"}
    }

    static class PeopleJsonProperty {
        public int id;
        @JsonProperty("newName")
        public String name;
        @Override
        public String toString() {
            return "PeopleJsonProperty [id=" + id + ", name=" + name +"]";
        }
    }


@JsonIgnore
    private static void annotationJsonIgnore() throws JsonProcessingException {
        PeopleJsonIgnore p = new PeopleJsonIgnore();
        p.id = 402;
        p.name = "rensanning 402 JsonIgnore";

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);

        System.out.println(json); // {"id":402}
    }

    static class PeopleJsonIgnore {
        public int id;
        @JsonIgnore
        public String name;
        @Override
        public String toString() {
            return "PeopleJsonIgnore [id=" + id + ", name=" + name +"]";
        }
    }


@JsonIgnoreProperties
    private static void annotationJsonIgnoreProperties() throws IOException {
        PeopleJsonIgnoreProperties p = new PeopleJsonIgnoreProperties();
        p.id = 404;
        p.name = "rensanning 404 JsonIgnoreProperties";
        p.age = 36;

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);

        System.out.println(json); // {"name":"rensanning 404 JsonIgnoreProperties"}

        // ignoreUnknown=true
        String json2 = "{\"id\":405, \"name\":\"rensanning405\", \"ttt\":\"123456\"}";

        ObjectMapper mapper2 = new ObjectMapper();
        PeopleIgnoreUnknown p2 = mapper2.readValue(json2, PeopleIgnoreUnknown.class);

        System.out.println(p2); // PeopleIgnoreUnknown [id=405, name=rensanning405]
    }

    @JsonIgnoreProperties({"id", "age"})
    static class PeopleJsonIgnoreProperties {
        public int id;
        public String name;
        public int age;
        @Override
        public String toString() {
            return "PeopleJsonIgnoreProperties [id=" + id + ", name=" + name + ", age=" + age +"]";
        }
    }

    @JsonIgnoreProperties(ignoreUnknown=true)
    static class PeopleIgnoreUnknown {
        public int id;
        public String name;
        @Override
        public String toString() {
            return "PeopleIgnoreUnknown [id=" + id + ", name=" + name +"]";
        }
    }


@JsonCreator
    private static void annotationJsonCreator() throws JsonParseException, JsonMappingException, IOException {
        String json = "{\"id\":406, \"name\":\"rensanning 406 JsonCreator\"}";

        // 默认采用无参构造函数。
        ObjectMapper mapper = new ObjectMapper();

        // 如果没有无参构造函数需要用@JsonCreator
        PeopleJsonCreator p1 = mapper.readValue(json, PeopleJsonCreator.class);
        System.out.println(p1); // PeopleJsonCreator [id=406, name=rensanning 406 JsonCreator]

        // @JsonCreator不局限于构造函数
        PeopleJsonCreatorFactory p2 = mapper.readValue(json, PeopleJsonCreatorFactory.class);
        System.out.println(p2); // PeopleJsonCreatorFactory [id=406, name=rensanning 406 JsonCreator]
    }

    static class PeopleJsonCreator {
        public int id;
        public String name;
        @JsonCreator
        private PeopleJsonCreator(@JsonProperty("id") int id, @JsonProperty("name") String name) {
            this.id = id;
            this.name = name;
        }
        @Override
        public String toString() {
            return "PeopleJsonCreator [id=" + id + ", name=" + name +"]";
        }
    }

    static class PeopleJsonCreatorFactory {
        public int id;
        public String name;
        @JsonCreator
        public static PeopleJsonCreatorFactory create(@JsonProperty("id") int id, @JsonProperty("name") String name) {
            return new PeopleJsonCreatorFactory(id, name);
        }
        private PeopleJsonCreatorFactory(int id, String name) {
            this.id = id;
            this.name = name;
        }
        @Override
        public String toString() {
            return "PeopleJsonCreatorFactory [id=" + id + ", name=" + name +"]";
        }
    }


@JsonAnyGetter
    private static void annotationJsonAnyGetter() throws JsonProcessingException {
        PepoleJsonAnyGetter p = new PepoleJsonAnyGetter();
        p.id = 407;
        p.name = "rensanning 407 JsonAnyGetter";
        p.properties.put("attr1", "val1");
        p.properties.put("attr2", "val2");

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);

        System.out.println(json); // {"id":407,"name":"rensanning 407 JsonAnyGetter","attr2":"val2","attr1":"val1"}
    }

    static class PepoleJsonAnyGetter {
        public int id;
        public String name;
        private Map<String, String> properties = new HashMap<String, String>();
        @JsonAnyGetter
        public Map<String, String> getProperties() {
            return properties;
        }
    }


@JsonAnySetter
    private static void annotationJsonAnySetter() throws JsonProcessingException, IOException {
        String json = "{\"id\":408, \"name\":\"rensanning 408 JsonAnySetter\",\"attr2\":\"val2\",\"attr1\":\"val1\"}";

        ObjectMapper mapper = new ObjectMapper();
        PepoleJsonAnySetter p = mapper.readValue(json, PepoleJsonAnySetter.class);

        System.out.println(p);
    }

    static class PepoleJsonAnySetter {
        public int id;
        public String name;
        private Map<String, String> properties = new HashMap<String, String>();
        @JsonAnySetter
        public void add(String key, String value) {
            properties.put(key, value);
        }
    }


@JsonSerialize
    private static void annotationJsonSerialize() throws JsonProcessingException, ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String toParse = "2017-05-03 12:30:00";
        Date date = df.parse(toParse);

        PeopleJsonSerialize p = new PeopleJsonSerialize();
        p.id = 409;
        p.name = "rensanning 409 JsonSerialize";
        p.eventDate = date;

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);

        System.out.println(json); // {"id":409,"name":"rensanning 409 JsonSerialize","eventDate":"2017-05-03 12:30:00"}
    }

    static class PeopleJsonSerialize {
        public int id;
        public String name;
        @JsonSerialize(using = CustomDateSerializer.class)
        public Date eventDate;
    }
    @SuppressWarnings("serial")
    static class CustomDateSerializer extends StdSerializer<Date> {
        private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        public CustomDateSerializer() {
            this(null);
        }
        public CustomDateSerializer(Class<Date> t) {
            super(t);
        }
        @Override
        public void serialize(Date value, JsonGenerator gen, SerializerProvider arg2)
          throws IOException, JsonProcessingException {
            gen.writeString(formatter.format(value));
        }
    }


@JsonDeserialize
    private static void annotationJsonDeserialize() throws JsonParseException, JsonMappingException, IOException {
        String json = "{\"id\":410, \"name\":\"rensanning 410 JsonDeserialize\",\"eventDate\":\"20-12-2014 02:30:00\"}";

        ObjectMapper mapper = new ObjectMapper();
        PeopleJsonDeserialize p = mapper.readValue(json, PeopleJsonDeserialize.class);

        System.out.println(p);
    }

    static class PeopleJsonDeserialize {
        public int id;
        public String name;
        @JsonDeserialize(using = CustomDateDeserializer.class)
        public Date eventDate;
    }
    @SuppressWarnings("serial")
    static class CustomDateDeserializer extends StdDeserializer<Date> {
        private static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
        public CustomDateDeserializer() {
            this(null);
        }
        public CustomDateDeserializer(Class<?> vc) {
            super(vc);
        }
        @Override
        public Date deserialize(JsonParser jsonparser, DeserializationContext context)
          throws IOException {
            String date = jsonparser.getText();
            try {
                return formatter.parse(date);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
    }


@JsonView
    private static void annotationJsonView() throws JsonProcessingException {
        PeopleJsonView item = new PeopleJsonView();
        item.id = 412;
        item.name = "rensanning 412 JsonView";
        item.address = "china dalian";

        ObjectMapper mapper = new ObjectMapper();

        String result1 = mapper.writerWithView(Views.Public.class).writeValueAsString(item);
        System.out.println(result1); // {"id":412,"name":"rensanning 412 JsonView"}

        String result2 = mapper.writerWithView(Views.Internal.class).writeValueAsString(item);
        System.out.println(result2); // {"id":412,"name":"rensanning 412 JsonView","address":"china dalian"}
    }

    static class Views {
        public static class Public {
        }
        public static class Internal extends Public {
        }
    }
    static class PeopleJsonView {
        @JsonView(Views.Public.class)
        public int id;
        @JsonView(Views.Public.class)
        public String name;
        @JsonView(Views.Internal.class)
        public String address;
    }


5.类信息

JsonTypeInfo.Id.CLASS
    private static void jsonTypeInfoId() throws JsonProcessingException {
        PeopleInfoId p = new PeopleInfoId();
        p.id = 501;
        p.name = "rensanning 501 JsonTypeInfoId";

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);

        System.out.println(json); // {"@class":"com.rensanning.jackson.Startup$PeopleInfoId","id":501,"name":"rensanning 501 JsonTypeInfoId"}
    }

    @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS)
    static class PeopleInfoId {
        public int id;
        public String name;
        @Override
        public String toString() {
            return "PeopleInfoId [id=" + id + ", name=" + name +"]";
        }
    }


JsonTypeInfo.Id.CUSTOM
    private static void jsonTypeInfoCustomId() throws JsonProcessingException {
        PeopleInfoCustomId p = new PeopleInfoCustomId();
        p.id = 502;
        p.name = "rensanning 502 JsonTypeInfoCustomId";

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);

        System.out.println(json); // {"CustomIdKey":"Startup$PeopleInfoCustomId","id":502,"name":"rensanning 502 JsonTypeInfoCustomId"}
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM,
            include = JsonTypeInfo.As.PROPERTY,
            property = "CustomIdKey")
    @JsonTypeIdResolver(PeopleInfoCustomIdResolver.class)
    static class PeopleInfoCustomId {
        public int id;
        public String name;
        @Override
        public String toString() {
            return "PeopleInfoCustomId [id=" + id + ", name=" + name +"]";
        }
    }
    static class PeopleInfoCustomIdResolver implements TypeIdResolver {
        private static final String COMMAND_PACKAGE = "com.rensanning.jackson";
        private JavaType mBaseType;

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

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

        @Override
        public String idFromValue(Object obj) {
            return idFromValueAndType(obj, obj.getClass());
        }

        @Override
        public String idFromBaseType() {
            return idFromValueAndType(null, mBaseType.getRawClass());
        }

        @Override
        public String idFromValueAndType(Object obj, Class<?> clazz) {
            String name = clazz.getName();
            if (name.startsWith(COMMAND_PACKAGE)) {
                return name.substring(COMMAND_PACKAGE.length() + 1);
            }
            throw new IllegalStateException("class " + clazz + " is not in the package " + COMMAND_PACKAGE);
        }

        @Override
        public String getDescForKnownTypeIds() {
            return null;
        }

        @Override
        public JavaType typeFromId(DatabindContext arg0, String type) throws IOException {
            Class<?> clazz;
            String clazzName = COMMAND_PACKAGE + "." + type;
            try {
                clazz = ClassUtil.findClass(clazzName);
            } catch (ClassNotFoundException e) {
                throw new IllegalStateException("cannot find class '" + clazzName + "'");
            }
            return TypeFactory.defaultInstance().constructSpecializedType(mBaseType, clazz);
        }
    }


JsonTypeInfo.As.WRAPPER_OBJECT
    private static void jsonTypeInfoAs() throws JsonProcessingException {
        PeopleInfoAs p = new PeopleInfoAs();
        p.id = 503;
        p.name = "rensanning 503 jsonTypeInfoAs";

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);

        System.out.println(json); // {"com.rensanning.jackson.Startup$PeopleInfoAs":{"id":503,"name":"rensanning 503 jsonTypeInfoAs"}}
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_OBJECT)
    static class PeopleInfoAs {
        public int id;
        public String name;
        @Override
        public String toString() {
            return "PeopleInfoAs [id=" + id + ", name=" + name +"]";
        }
    }


JsonTypeInfo.As.EXTERNAL_PROPERTY
    private static void jsonTypeInfoAsExternal() throws JsonProcessingException {
        People p = new People();
        p.id = 504;
        p.name = "rensanning 504 JsonTypeInfoAsExternal";

        ExternalClass dto = new ExternalClass();
        dto.people = p;

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(dto);

        System.out.println(json); // {"people":{"id":504,"name":"rensanning 504 JsonTypeInfoAsExternal"},"@class":"com.rensanning.jackson.Startup$People"}
    }

    static class ExternalClass {
        @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.EXTERNAL_PROPERTY)
        public People people;
    }


6.抽象类

@JsonTypeInfo(use=Id.CLASS)
    private static void abstractClassID() throws IOException {
        P11 p11 = new P11();
        p11.id = 601;
        p11.name = "rensanning 601";

        P12 p12 = new P12();
        p12.id = 602;
        p12.name = "rensanning 602";

        Animal1 dto = new Animal1();
        dto.list.add(p11);
        dto.list.add(p12);

        ObjectMapper mapper = new ObjectMapper();

        String json = mapper.writeValueAsString(dto);
        System.out.println(json); // {"list":[{"@class":"com.rensanning.jackson.Startup$P11","id":601,"name":"rensanning 601"},{"@class":"com.rensanning.jackson.Startup$P12","id":602,"name":"rensanning 602"}]}

        dto = (Animal1) mapper.readValue(json, Animal1.class);
        System.out.println(dto);
    }

    @JsonTypeInfo(use=Id.CLASS)
    static abstract class Base1 {
        public int id;
        public String name;
    }
    static class P11 extends Base1 {
        @Override
        public String toString() {
            return "P11 [id=" + id + ", name=" + name + "]";
        }
    }
    static class P12 extends Base1 {
        @Override
        public String toString() {
            return "P12 [id=" + id + ", name=" + name + "]";
        }
    }
    static class Animal1 {
        public List<Base1> list = new ArrayList<Base1>();
    }


mapper.enableDefaultTyping();
    private static void abstractClassType() throws IOException {
        P21 p21 = new P21();
        p21.id = 603;
        p21.name = "rensanning 603";

        P22 p22 = new P22();
        p22.id = 604;
        p22.name = "rensanning 604";

        Animal2 dto = new Animal2();
        dto.list.add(p21);
        dto.list.add(p22);

        ObjectMapper mapper = new ObjectMapper();
        mapper.enableDefaultTyping();

        String json = mapper.writeValueAsString(dto);
        System.out.println(json); // {"list":["java.util.ArrayList",[["com.rensanning.jackson.Startup$P21",{"id":603,"name":"rensanning 603"}],["com.rensanning.jackson.Startup$P22",{"id":604,"name":"rensanning 604"}]]]}

        dto = (Animal2) mapper.readValue(json, Animal2.class);
        System.out.println(dto);
    }

    static abstract class Base2 {
        public int id;
        public String name;
    }
    static class P21 extends Base2 {
        @Override
        public String toString() {
            return "P21 [id=" + id + ", name=" + name + "]";
        }
    }
    static class P22 extends Base2 {
        @Override
        public String toString() {
            return "P22 [id=" + id + ", name=" + name + "]";
        }
    }
    static class Animal2 {
        public List<Base2> list = new ArrayList<Base2>();
    }


@JsonSubTypes
    private static void abstractClassSubType() throws IOException {
        P31 p31 = new P31();
        p31.id = 605;
        p31.name = "rensanning 605";

        P32 p32 = new P32();
        p32.id = 606;
        p32.name = "rensanning 606";

        Animal3 dto = new Animal3();
        dto.list.add(p31);
        dto.list.add(p32);

        ObjectMapper mapper = new ObjectMapper();

        String json = mapper.writeValueAsString(dto);
        System.out.println(json); // {"list":[{"type":"P31","id":605,"name":"rensanning 605"},{"type":"P32","id":606,"name":"rensanning 606"}]}

        dto = (Animal3) mapper.readValue(json, Animal3.class);
        System.out.println(dto);
    }

    @JsonTypeInfo(
          use = JsonTypeInfo.Id.NAME,
          include = JsonTypeInfo.As.PROPERTY,
          property = "type")
        @JsonSubTypes({
          @Type(value = P31.class, name = "P31"),
          @Type(value = P32.class, name = "P32")
        })
    static abstract class Base3 {
        public int id;
        public String name;
    }
    static class P31 extends Base3 {
        @Override
        public String toString() {
            return "P31 [id=" + id + ", name=" + name + "]";
        }
    }
    static class P32 extends Base3 {
        @Override
        public String toString() {
            return "P32 [id=" + id + ", name=" + name + "]";
        }
    }
    static class Animal3 {
        public List<Base3> list = new ArrayList<Base3>();
    }


参考:
http://qiita.com/opengl-8080/items/b613b9b3bc5d796c840c
http://www.baeldung.com/jackson
分享到:
评论

相关推荐

    MATLAB实现基于LSTM-AdaBoost长短期记忆网络结合AdaBoost时间序列预测(含模型描述及示例代码)

    内容概要:本文档详细介绍了基于 MATLAB 实现的 LSTM-AdaBoost 时间序列预测模型,涵盖项目背景、目标、挑战、特点、应用领域以及模型架构和代码示例。随着大数据和AI的发展,时间序列预测变得至关重要。传统方法如 ARIMA 在复杂非线性序列中表现欠佳,因此引入了 LSTM 来捕捉长期依赖性。但 LSTM 存在易陷局部最优、对噪声鲁棒性差的问题,故加入 AdaBoost 提高模型准确性和鲁棒性。两者结合能更好应对非线性和长期依赖的数据,提供更稳定的预测。项目还展示了如何在 MATLAB 中具体实现模型的各个环节。 适用人群:对时间序列预测感兴趣的开发者、研究人员及学生,特别是有一定 MATLAB 编程经验和熟悉深度学习或机器学习基础知识的人群。 使用场景及目标:①适用于金融市场价格预测、气象预报、工业生产故障检测等多种需要时间序列分析的场合;②帮助使用者理解并掌握将LSTM与AdaBoost结合的实现细节及其在提高预测精度和抗噪方面的优势。 其他说明:尽管该模型有诸多优点,但仍存在训练时间长、计算成本高等挑战。文中提及通过优化数据预处理、调整超参数等方式改进性能。同时给出了完整的MATLAB代码实现,便于学习与复现。

    palkert_3ck_01_0918.pdf

    palkert_3ck_01_0918

    pepeljugoski_01_1106.pdf

    pepeljugoski_01_1106

    tatah_01_1107.pdf

    tatah_01_1107

    [AB PLC例程源码][MMS_046393]Motor Speed Reference.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    基于51的步进电机控制系统20250302

    题目:基于单片机的步进电机控制系统 模块: 主控:AT89C52RC 步进电机(ULN2003驱动) 按键(3个) 蓝牙(虚拟终端模拟) 功能: 1、可以通过蓝牙远程控制步进电机转动 2、可以通过按键实现手动与自动控制模式切换。 3、自动模式下,步进电机正转一圈,反转一圈,循环 4、手动模式下可以通过按键控制步进电机转动(顺时针和逆时针)

    [AB PLC例程源码][MMS_041234]Logix Fault Handler.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    [AB PLC例程源码][MMS_042348]Using an Ultra3000 as an Indexer on DeviceNet with a CompactLogix.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    智慧校园平台建设全流程详解:从需求到持续优化

    内容概要:本文详细介绍了建设智慧校园平台所需的六个关键步骤。首先通过需求分析深入了解并确定校方和使用者的具体需求;其次是规划设计阶段,依据所得需求制定全面的建设方案。再者是对现有系统的整合——系统集成,确保新旧平台之间的互操作性和数据一致性。培训支持帮助全校教职工和学生快速熟悉新平台,提高效率。实施试点确保系统逐步稳定部署。最后,强调持续改进的重要性,以适应技术和环境变化。通过这一系列有序的工作,可以使智慧校园建设更为科学高效,减少失败风险。 适用人群:教育领域的决策者和技术人员,包括负责信息化建设和运维的团队成员。 使用场景及目标:用于指导高校和其他各级各类学校规划和发展自身的数字校园生态链;目的是建立更加便捷高效的现代化管理模式和服务机制。 其他说明:智慧校园不仅仅是简单的IT设施升级或软件安装,它涉及到全校范围内的流程再造和创新改革。

    AI淘金实战手册:100+高收益变现案例解析

    该文档系统梳理了人工智能技术在商业场景中的落地路径,聚焦内容生产、电商运营、智能客服、数据分析等12个高潜力领域,提炼出100个可操作性变现模型。内容涵盖AI工具开发、API服务收费、垂直场景解决方案、数据增值服务等多元商业模式,每个思路均配备应用场景拆解、技术实现路径及收益测算框架。重点呈现低代码工具应用、现有平台流量复用、细分领域自动化改造三类轻量化启动方案,为创业者提供从技术选型到盈利闭环的全流程参考。

    palkert_3ck_02_0719.pdf

    palkert_3ck_02_0719

    2006-2023年 地级市-克鲁格曼专业化指数.zip

    克鲁格曼专业化指数,最初是由Krugman于1991年提出,用于反映地区间产业结构的差异,也被用来衡量两个地区间的专业化水平,因而又称地区间专业化指数。该指数的计算公式及其含义可以因应用背景和具体需求的不同而有所调整,但核心都是衡量地区间的产业结构差异或专业化程度。 指标 年份、城市、第一产业人数(first_industry1)、第二产业人数(second_industry1)、第三产业人数(third_industry1)、专业化指数(ksi)。

    [AB PLC例程源码][MMS_046305]R2FX.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    精品推荐-通信技术LTE干货资料合集(19份).zip

    精品推荐,通信技术LTE干货资料合集,19份。 LTE PCI网络规划工具.xlsx LTE-S1切换占比专题优化分析报告.docx LTE_TDD问题定位指导书-吞吐量篇.docx LTE三大常见指标优化指导书.xlsx LTE互操作邻区配置核查原则.docx LTE信令流程详解指导书.docx LTE切换问题定位指导一(定位思路和问题现象).docx LTE劣化小区优化指导手册.docx LTE容量优化高负荷小区优化指导书.docx LTE小区搜索过程学习.docx LTE小区级与邻区级切换参数说明.docx LTE差小区处理思路和步骤.docx LTE干扰日常分析介绍.docx LTE异频同频切换.docx LTE弱覆盖问题分析与优化.docx LTE网优电话面试问题-应答技巧.docx LTE网络切换优化.docx LTE高负荷小区容量优化指导书.docx LTE高铁优化之多频组网优化提升“用户感知,网络价值”.docx

    matlab程序代码项目案例:matlab程序代码项目案例matlab中Toolbox中带有的模型预测工具箱.zip

    matlab程序代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    pepeljugoski_01_0508.pdf

    pepeljugoski_01_0508

    szczepanek_01_0308.pdf

    szczepanek_01_0308

    oif2007.384.01_IEEE.pdf

    oif2007.384.01_IEEE

    stone_3ck_01_0119.pdf

    stone_3ck_01_0119

    oganessyan_01_1107.pdf

    oganessyan_01_1107

Global site tag (gtag.js) - Google Analytics