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

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
分享到:
评论

相关推荐

    Jackson 入门Jackson 入门

    以下是关于Jackson入门的关键知识点: 1. **Streaming API**: - Jackson的Streaming API提供了一种低级、高性能的方式来处理JSON,适合大数据量或内存有限的情况。 - `JsonParser`用于读取JSON数据,而`...

    220个JAVA初学者实例

    19. **JSON处理**:JSON作为一种轻量级的数据交换格式,学习使用Gson或Jackson库进行解析和序列化。 20. **单元测试**:使用JUnit进行代码测试,确保程序的正确性。 以上只是Java初学者实例中可能涉及的部分关键...

    jackson电动力学习题答案

    Magyar在其导言部分提到,尽管Jackson的书在某些方面非常出色,但也存在不足之处:书中有时假定了读者对某些材料已有所了解、遗漏了一些重要的步骤以及提供的实例不够充分。这些因素使得该书不太适合作为入门级教材...

    jackson经典电动力学部分习题答案

    该书在某些地方假设了读者对基础材料的熟悉度,省略了关键步骤,并且提供的实例不足。对于初学者而言,这可能是一本难以消化的书籍。然而,Jackson的野心在于他几乎涵盖了经典电磁理论的大部分内容,除了一些明显的...

    spring boot入门例子

    例如,如果检测到`spring-boot-starter-web`,那么它就会自动配置Web相关的设置,包括DispatcherServlet、Jackson JSON序列化等。 3. **嵌入式服务器(Embedded Server)**:Spring Boot 支持嵌入式Servlet容器,如...

    Extjs+Struts整合入门实例

    本入门实例主要探讨的是如何将ExtJS与Struts进行整合,以构建一个图书管理系统的前端和后端。这样的整合使得开发者能够利用ExtJS的前端优势,提供美观且交互性强的用户界面,同时利用Struts在服务器端进行数据处理和...

    HttpClient实例+必备3个jar包

    HttpClient是Apache基金会开发的一个Java库...在实际开发中,HttpClient还可以与其他Java库结合,如Jackson或Gson进行JSON数据的序列化和反序列化,或者与Apache Commons Lang、IO库一起使用,提高代码的效率和灵活性。

    几百个安卓开发实例

    在安卓应用开发的世界里,实例是学习和理解各种概念的关键工具。"几百个安卓开发实例"这个资源集合,正如其标题所示,包含了丰富的...在学习过程中,配合官方文档和在线教程,相信你将能够迅速入门并逐渐精通安卓开发。

    Android开发入门60个小案例+源代码.rar.rar

    "Android开发入门60个小案例+源代码.rar.rar" 提供了一套全面的学习资源,旨在帮助初学者快速熟悉Android应用开发的基本流程和技术要点。以下是对这些小案例及其源代码的详细解析。 1. **环境配置**:首先,学习...

    android开发从入门到精通光盘源代码

    以上只是Android开发中的一部分关键知识点,实际的学习过程需要结合"android开发从入门到精通光盘源代码"提供的实例,通过实践不断巩固和深化理解。通过这种方式,开发者不仅可以学习到理论知识,还能积累实战经验,...

    Google Android开发入门与实战(pdf和源代码)

    这本书涵盖了从Android开发环境的搭建到实际应用开发的全过程,提供了丰富的实例和源代码,确保读者能够亲手实践并验证理论知识。 在Android开发领域,首先需要了解的是Android SDK(Software Development Kit),...

    Android核心技术与实例详解

    总之,《Android核心技术与实例详解》是一本适合初学者和进阶者的参考书籍,它以实例为引导,深入剖析Android开发的核心技术,有助于读者快速提升Android应用开发技能,无论你是希望入门Android开发,还是寻求技术...

    JAVA-基础入门教程

    1. 类与对象:Java是面向对象的语言,类是对象的蓝图,对象是类的实例。 2. 继承:一个类可以继承另一个类的属性和方法,实现代码复用。 3. 封装:通过访问修饰符(public、private、protected)控制成员的可见性,...

    android从入门到精通ppt

    本教程通过PPT的形式,以清晰的逻辑和丰富的实例,逐步引领学习者步入Android的世界。以下是本教程中涵盖的一些核心知识点: 一、Android系统概述 1. Android系统的架构:了解Android系统的层次结构,包括Linux内核...

    Android开发入门与实战体验-ppt

    - **JSON解析**:Gson或Jackson库可将JSON数据转换为Java对象,方便处理。 5. **服务与广播** - **服务(Service)**:后台运行的任务,即使用户离开应用仍能继续执行。 - **广播接收器(BroadcastReceiver)**:...

    Android网络开发从入门到精通源码_张余

    在Android应用开发中,网络编程是一...总的来说,"Android网络开发从入门到精通源码"涵盖了Android网络开发的各个方面,从基础的网络请求到高级的网络优化,通过实例代码深入解析,有助于开发者全面提升网络编程能力。

    springboot 实例

    在“springboot 实例”中,我们可以看到一个入门级的项目,它通常会包含以下几个关键部分: 1. **项目结构**:SpringBoot 应用的基础结构通常包含 `src/main/java` 和 `src/main/resources` 目录。`java` 目录下有...

    Android开发入门与实战源码

    总之,"Android开发入门与实战源码"为初学者提供了一个全面的学习平台,通过实际的代码实例,有助于深入理解和掌握Android开发的各个方面。在阅读和分析这些源码时,应结合理论知识,不断实践,逐步提高自己的开发...

Global site tag (gtag.js) - Google Analytics