jackson基本包:jackson-core、jackson-databind、jackson-annotations
xml功能:jackson-dataformat-xml(jdk9及以上需要引入JAXB包)
xml高效类库:woodstox-core
jdk8支持:jackson-datatype-jsr310、jackson-module-parameter-names、jackson-datatype-jdk8
maven引入:
com.fasterxml.jackson.dataformat
jackson-dataformat-xml
2.9.8
com.fasterxml.jackson.datatype
jackson-datatype-jsr310
2.9.8
com.fasterxml.jackson.module
jackson-module-parameter-names
2.9.8
com.fasterxml.jackson.datatype
jackson-datatype-jdk8
2.9.8
org.codehaus.woodstox
stax2-api
4.1
javax.xml.bind
jaxb-api
2.2.11
com.sun.xml.bind
jaxb-core
2.2.11
com.sun.xml.bind
jaxb-impl
2.2.11
javax.activation
activation
1.1.1
Demo:
/**
* 2019年1月9日下午3:04:07
*/
package testJackson;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
/**
* @author XWF
*
*/
public class TestJackson {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
TestJackson testJackson = new TestJackson();
System.out.println("===============pojo,map==================");
testJackson.test1();
System.out.println("=================标签================");
testJackson.test2();
System.out.println("=================树================");
testJackson.test3();
System.out.println("=================流================");
testJackson.test4();
System.out.println("=================jdk8日期================");
testJackson.test5();
System.out.println("=================xml================");
testJackson.test6();
}
//pojo,map
public void test1() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
Stu stu = new Stu();
stu.setName("雷克斯");
stu.setAge(23);
String jsonStr = objectMapper.writeValueAsString(stu);
System.out.println(jsonStr);
Stu newStu = objectMapper.readValue(jsonStr,Stu.class);
System.out.println(newStu);
Map map = new HashMap<>();
map.put(1, "小光");
map.put(2, "小焰");
String jsonmap = objectMapper.writeValueAsString(map);
System.out.println(jsonmap);
Map map2 = objectMapper.readValue(jsonmap, new TypeReference
结果:
参考:
https://blog.csdn.net/u011054333/article/details/80504154
https://blog.csdn.net/m0_37076574/article/details/81317403
https://github.com/FasterXML/jackson-dataformat-xml/wiki/Jackson-XML-annotations
https://blog.csdn.net/huanghanqian/article/details/80862874
https://www.yiibai.com/jackson/jackson_streaming_api.html