Java Jackson - Json Polymorphism

from://http://www.studytrails.com/java/json/java-jackson-Serialization-polymorphism.jsp

Jackson provides a way to maintain sub type information while serializing java objects. It is possible to recreate the exact sub type. The type information can be embedded into the json as a property. In the example below we create a zoo, that has a list of animals. The animal may be an elephant or a lion, and they both extend the Animal abstract class. While deserializing we want to create the exact animal type. We also demonstrate the use of @JsonTypeInfo and @JsonSubTypes annotations.

Data Serialization and Polymorphism Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package  com.studytrails.json.jackson;
 
import  java.io.File;
import  java.io.FileWriter;
import  java.io.IOException;
import  java.util.ArrayList;
import  java.util.List;
 
import  com.fasterxml.jackson.core.JsonGenerationException;
import  com.fasterxml.jackson.databind.JsonMappingException;
import  com.fasterxml.jackson.databind.ObjectMapper;
 
public  class  SerializeExample1 {
     private  static  String outputFile =  "zoo.json" ;
 
     public  static  void  main(String[] args)  throws  JsonGenerationException, JsonMappingException, IOException {
         // let start creating the zoo
         Zoo zoo =  new  Zoo( "Samba Wild Park" "Paz" );
         Lion lion =  new  Lion( "Simba" );
         Elephant elephant =  new  Elephant( "Manny" );
         List<Animal> animals =  new  ArrayList<>();
         animals.add(lion);
         animals.add(elephant);
         zoo.setAnimals(animals);
 
         ObjectMapper mapper =  new  ObjectMapper();
         mapper.writerWithDefaultPrettyPrinter().writeValue( new  FileWriter( new  File(outputFile)), zoo);
     }
}

Before we look at the various classes, lets also see how to deserialize this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package  com.studytrails.json.jackson;
 
import  java.io.File;
import  java.io.IOException;
 
import  org.apache.commons.io.FileUtils;
 
import  com.fasterxml.jackson.core.JsonParseException;
import  com.fasterxml.jackson.databind.JsonMappingException;
import  com.fasterxml.jackson.databind.ObjectMapper;
 
public  class  DeSerializeExample1 {
 
     public  static  void  main(String[] args)  throws  JsonParseException, JsonMappingException, IOException {
         ObjectMapper mapper =  new  ObjectMapper();
         Zoo zoo = mapper.readValue(FileUtils.readFileToByteArray( new  File( "zoo.json" )), Zoo. class );
         System.out.println(zoo);
     }
}

Zoo class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package  com.studytrails.json.jackson;
 
import  java.util.List;
 
import  com.fasterxml.jackson.annotation.JsonCreator;
import  com.fasterxml.jackson.annotation.JsonProperty;
import  com.fasterxml.jackson.annotation.JsonTypeInfo;
import  com.fasterxml.jackson.annotation.JsonTypeInfo.As;
 
@JsonTypeInfo (use = JsonTypeInfo.Id.MINIMAL_CLASS, include = As.PROPERTY, property =  "@class" )
public  class  Zoo {
 
     public  String name;
     public  String city;
     public  List<Animal> animals;
 
     @JsonCreator
     public  Zoo( @JsonProperty ( "name" ) String name,  @JsonProperty ( "city" ) String city) {
         this .name = name;
         this .city = city;
     }
 
     public  void  setAnimals(List<animal> animals) {
         this .animals = animals;
     }
 
     @Override
     public  String toString() {
         return  "Zoo [name="  + name +  ", city="  + city +  ", animals="  + animals +  "]" ;
     }
 
}
 
 
</animal>

Animal Abstract class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package  com.studytrails.json.jackson;
 
import  com.fasterxml.jackson.annotation.JsonProperty;
import  com.fasterxml.jackson.annotation.JsonSubTypes;
import  com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import  com.fasterxml.jackson.annotation.JsonTypeInfo;
import  com.fasterxml.jackson.annotation.JsonTypeInfo.As;
 
@JsonTypeInfo (use = JsonTypeInfo.Id.CLASS, include = As.PROPERTY, property =  "@class" )
@JsonSubTypes ({  @Type (value = Lion. class , name =  "lion" ),  @Type (value = Elephant. class , name =  "elephant" ) })
public  abstract  class  Animal {
     @JsonProperty ( "name" )
     String name;
     @JsonProperty ( "sound" )
     String sound;
     @JsonProperty ( "type" )
     String type;
     @JsonProperty ( "endangered" )
     boolean  endangered;
 
}

Lion class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package  com.studytrails.json.jackson;
 
import  com.fasterxml.jackson.annotation.JsonCreator;
import  com.fasterxml.jackson.annotation.JsonProperty;
 
public  class  Lion  extends  Animal {
 
     private  String name;
 
     @JsonCreator
     public  Lion( @JsonProperty ( "name" ) String name) {
         this .name = name;
     }
 
     public  String getName() {
         return  name;
     }
 
     public  String getSound() {
         return  "Roar" ;
     }
 
     public  String getType() {
         return  "carnivorous" ;
     }
 
     public  boolean  isEndangered() {
         return  true ;
     }
 
     @Override
     public  String toString() {
         return  "Lion [name="  + name +  ", getName()="  + getName() +  ", getSound()="  + getSound() +  ", getType()="  + getType() +  ", isEndangered()="
                 + isEndangered() +  "]" ;
     }
 
}

Elephant class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package  com.studytrails.json.jackson;
 
import  com.fasterxml.jackson.annotation.JsonCreator;
import  com.fasterxml.jackson.annotation.JsonProperty;
 
public  class  Elephant  extends  Animal {
 
     @JsonProperty
     private  String name;
 
     @JsonCreator
     public  Elephant( @JsonProperty ( "name" ) String name) {
         this .name = name;
     }
 
     public  String getName() {
         return  name;
     }
 
     public  String getSound() {
         return  "trumpet" ;
     }
 
     public  String getType() {
         return  "herbivorous" ;
     }
 
     public  boolean  isEndangered() {
         return  false ;
     }
 
     @Override
     public  String toString() {
         return  "Elephant [name="  + name +  ", getName()="  + getName() +  ", getSound()="  + getSound() +  ", getType()="  + getType()
                 ", isEndangered()="  + isEndangered() +  "]" ;
     }
 
}
分类:  android solve


本文转自wanqi博客园博客,原文链接:http://www.cnblogs.com/wanqieddy/p/4013205.html,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值