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() + "]"; }} |
Java Jackson - Json Polymorphism的更多相关文章
- Java之JSON操作(Jackson)
Java to JSON: package json.jackson; import bean.User; import com.fasterxml.jackson.databind.ObjectMa ...
- Java解析json(二):jackson
Java解析json(二):jackson 官方参考 Jackson Home Page:https://github.com/FasterXML/jackson Jackson Wiki:htt ...
- Java 的 JSON 开源类库选择比较(zz)
在看了作者的介绍,然后我又到mvnrepository上去看了各个库的的使用数之后,发现只能在jackson和gson之间做选择. 以下是原文 有效选择七个关于Java的JSON开源类库 April ...
- java系列--JSON数据的处理
http://blog.csdn.net/qh_java/article/details/38610599 http://www.cnblogs.com/lanxuezaipiao/archive/2 ...
- Java JWT: JSON Web Token
Java JWT: JSON Web Token for Java and Android JJWT aims to be the easiest to use and understand libr ...
- 开源 JSON 库解析性能对比( Jackson / Json.simple / Gson )
Json 已成为当前服务器与 web 应用之间数据传输的公认标准. 微服务及分布式架构经常会使用 Json 来传输此类文件,因为这已经是 webAPI 的事实标准. 不过正如许多我们习以为常的事情一样 ...
- 让Jackson JSON生成的数据包含的中文以unicode方式编码
本文出处:http://blog.csdn.net/chaijunkun/article/details/8257209,转载请注明.由于本人不定期会整理相关博文,会对相应内容作出完善.因此强烈建 ...
- 如何让Jackson JSON生成的数据包含的中文以unicode方式编码
我们都知道,Jackson JSON以高速.方便和灵活著称.之前的文章中介绍过使用注解的形式来规定如何将一个对象序列化成JSON的方法,以及如何将一个JSON数据反序列化到一个对象上.但是美中不足的一 ...
- java中 json和bean list map之间的互相转换总结
JSON 与 对象 .集合 之间的转换 JSON字符串和java对象的互转[json-lib] 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级 ...
随机推荐
- re模块逐步进阶
Windows 10家庭中文版,Python 3.6.4, 正则表达式,自己一直的水平是 知道,但不熟悉,简单的也能写,复杂的就需要看资料了,距离灵活运用还是差那么一些的. 于是,今天(180831) ...
- TCxGrid 把列移上移下。
T
- 重装Ubuntu时如何保留/home分区中的数据
重装Ubuntu时如何保留/home分区中的数据作者:maowang Windows系统可以在重装时只格式化C盘,从而保留其他分区的数据. Ubuntu系统也可以,只要在安装系统时分出一个/home分 ...
- IOC入门
Spring六大模块 1.SpringCore spring的核心功能:IOC容器,解决对象的创建及依赖关系 2.SpringWeb spring对Web模块的支持 3.SpringDAO s ...
- BootStrap fileinput.js文件上传组件实例代码
1.首先我们下载好fileinput插件引入插件 ? 1 2 3 <span style="font-size:14px;"><link type="t ...
- RocketMQ 部署
1. 下载 下载RocketMQwget https://github.com/alibaba/RocketMQ/releases/download/v3.2.6/alibaba-rocketmq-3 ...
- Nginx+redis的Asp.net
基于Nginx+redis的Asp.net站点搭建 剧情介绍 在传统的信息系统(比如小规模的ERP\MES系统),往往只是进行简单的应用服务器和数据库服务器的分布式部署,以此来提高应用系统的负载能 ...
- Redis学习笔记10--Redis主从复制
redis主从复制配置和使用都非常简单.通过主从复制可以允许多个slave server拥有和master server相同的数据库副本.下面是关于redis主从复制的一些特点: 1.ma ...
- 图解简单C程序的运行时结构
程序在内存中的存储分为三个区域,分别是动态数据区.静态数据区和代码区.函数存储在代码区,全局变量以及静态变量存储在静态数据区,而在程序执行的时候才会在动态数据区产生数据.程序执行的本质就是代码区的指令 ...
- C++ 大学课堂知识点总结
一.从C到C++1.引用int b;int &a = b;//a是引用类型 定义的时候加& 表示引用 其余都是取地址 a是b别名 使用a和使用b是一样的 主要用于 ...