基础测试
package com.ai;
import com.ai.test.daily.Student;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
//@SpringBootTest(classes = com.ai.main.Application.class)
//@RunWith(SpringRunner.class)
public class Application {
private Student student=new Student();
private Student student1=new Student();
@Before
public void before(){
student.setAge(18);
student.setBirthday(new Date());
student.setEmail("333333.qq.com");
student.setId(1);
student.setName("李强");
student1.setAge(180);
student1.setBirthday(new Date());
student1.setEmail("333333.qq.com");
student1.setId(2);
student1.setName("张三");
}
@Test //bean->json
public void beanToJson() {
String jsonString = JSON.toJSONString(student);
System.out.println("");
System.out.println("+++++++++++++++++++++++++++++++++bean->json+++++++++++++++++++++++++++++++++");
System.out.println(jsonString);
}
@Test //json-> bean
public void jsonToBean() {
String jsonString = "{\"age\":18,\"birthday\":1636371553598,\"email\":\"333333.qq.com\",\"id\":1,\"name\":\"李强\"}";
Student student = JSON.parseObject(jsonString, Student.class);
System.out.println("");
System.out.println("+++++++++++++++++++++++++++++++++json-> bean+++++++++++++++++++++++++++++++++");
System.out.println(student.toString());
}
@Test //list-> json
public void listToJson() {
ArrayList<Student> list = new ArrayList<>();
list.add(student);
list.add(student1);
String jsonString = JSON.toJSONString(list);
System.out.println("");
System.out.println("+++++++++++++++++++++++++++++++++list-> json+++++++++++++++++++++++++++++++++");
System.out.println(jsonString);
}
@Test //json-> list
public void jsonToList() {
String jsonString = "[{\"age\":18,\"birthday\":1636372096907,\"email\":\"333333.qq.com\",\"id\":1,\"name\":\"李强\"},{\"age\":180,\"birthday\":1636372096907,\"email\":\"333333.qq.com\",\"id\":2,\"name\":\"张三\"}]";
List<Student> students = JSON.parseArray(jsonString, Student.class);
System.out.println("");
System.out.println("+++++++++++++++++++++++++++++++++json-> list+++++++++++++++++++++++++++++++++");
System.out.println(students.toString());
}
@Test //map-> json
public void mapToJson() {
HashMap<String, Student> map = new HashMap<>();
map.put("student",student);
map.put("student1",student1);
String jsonString = JSON.toJSONString(map);
System.out.println("");
System.out.println("+++++++++++++++++++++++++++++++++map-> json+++++++++++++++++++++++++++++++++");
System.out.println(jsonString);
}
@Test //json-> map
public void jsonToMap() {
String jsonString = "{\"student\":{\"age\":18,\"birthday\":1636372534788,\"email\":\"333333.qq.com\",\"id\":1,\"name\":\"李强\"},\"student1\":{\"age\":180,\"birthday\":1636372534788,\"email\":\"333333.qq.com\",\"id\":2,\"name\":\"张三\"}}";
HashMap<String, Student> map = JSON.parseObject(jsonString, new TypeReference<HashMap<String, Student>>(){});
System.out.println("");
System.out.println("+++++++++++++++++++++++++++++++++json-> map+++++++++++++++++++++++++++++++++");
System.out.println(map.toString());
}
@Test
//SerializerFeature 枚举类: 进行序列化时,特殊化定制
// QuoteFieldNames 输出key时是否使用双引号,默认为true
// UseSingleQuotes 使用单引号而不是双引号,默认为false
// WriteMapNullValue 是否输出值为null的字段,默认为false
// WriteEnumUsingToString Enum输出name()或者original,默认为false
// UseISO8601DateFormat Date使用ISO8601格式输出,默认为false
// WriteNullListAsEmpty List字段如果为null,输出为[],而非null
// WriteNullStringAsEmpty 字符类型字段如果为null,输出为”“,而非null
// WriteNullNumberAsZero 数值字段如果为null,输出为0,而非null
// WriteNullBooleanAsFalse Boolean字段如果为null,输出为false,而非null
// SkipTransientField 如果是true,类中的Get方法对应的Field是transient,序列化时将会被忽略。默认为true
// SortField 按字段名称排序后输出。默认为false
// WriteTabAsSpecial 把\t做转义输出,默认为false 不推荐
// PrettyFormat 结果是否格式化,默认为false
// WriteClassName 序列化时写入类型信息,默认为false。反序列化是需用到
// DisableCircularReferenceDetect 消除对同一对象循环引用的问题,默认为false
// WriteSlashAsSpecial 对斜杠’/’进行转义
// WriteDateUseDateFormat 全局修改日期格式,默认为false。JSON.DEFFAULT_DATE_FORMAT = “yyyy-MM-dd”;JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);
// DisableCheckSpecialChar 一个对象的字符串属性中如果有特殊字符如双引号,将会在转成json时带有反斜杠转移符。如果不需要转义,可以使用这个属性。默认为false
// BeanToArray 将对象转为array输出
public void SerializerFeature(){
String jsonStringSerializerFeature = JSON.toJSONString(student
, SerializerFeature.WriteDateUseDateFormat // WriteDateUseDateFormat 全局修改日期格式,默认为false。JSON.DEFFAULT_DATE_FORMAT = “yyyy-MM-dd”;JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);
, SerializerFeature.WriteNullStringAsEmpty // WriteNullNumberAsZero 数值字段如果为null,输出为0,而非null
, SerializerFeature.PrettyFormat // UseSingleQuotes 使用单引号而不是双引号,默认为false
);
String jsonString = JSON.toJSONString(student);
//对比
System.out.println("");
System.out.println("+++++++++++++++++++++++++++++++++SerializerFeature++++++++++++++++++++++++++++++++");
System.out.println("jsonStringSerializerFeature"+jsonStringSerializerFeature);
System.out.println("jsonString+++"+jsonString);
}
}
SpringMVC
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import java.nio.charset.Charset;
@Configuration
public class HttpMessageConverterConfig {
//引入Fastjson解析json,不使用默认的jackson
//必须在pom.xml引入fastjson的jar包,并且版必须大于1.2.10
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
//1、定义一个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
SerializerFeature[] serializerFeatures = new SerializerFeature[]{
// 输出key是包含双引号
// SerializerFeature.QuoteFieldNames,
// 是否输出为null的字段,若为null 则显示该字段
// SerializerFeature.WriteMapNullValue,
// 数值字段如果为null,则输出为0
SerializerFeature.WriteNullNumberAsZero,
// List字段如果为null,输出为[],而非null
SerializerFeature.WriteNullListAsEmpty,
// 字符类型字段如果为null,输出为"",而非null
SerializerFeature.WriteNullStringAsEmpty,
// Boolean字段如果为null,输出为false,而非null
SerializerFeature.WriteNullBooleanAsFalse,
// Date的日期转换器
SerializerFeature.WriteDateUseDateFormat,
// 循环引用
SerializerFeature.DisableCircularReferenceDetect,
};
fastJsonConfig.setSerializerFeatures(serializerFeatures);
fastJsonConfig.setCharset(Charset.forName("UTF-8"));
//3、在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
//4、将convert添加到converters中
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}