fastjson和jackson使用对比
2020-12-26 14:29
标签:parser 编写 static figure ack 结果 png row code Fastjson是一个Java语言编写的高性能的JSON处理器,由阿里巴巴公司开发。无依赖,不需要例外额外的jar,能够直接跑在JDK上。 FastJson在复杂类型的Bean转换Json上会出现一些问题,可能会出现引用的类型,导致Json转换出错,需要制定引用。 FastJson采用独创的算法,将parse的速度提升到极致,超过所有json库。 fastjson将javabean转成字符串,结果为:{"username":"lisi"},默认过滤null值 jackson默认保留null值 如果不想过滤,fastjson可以使用以下方式 结果为:{"password":null,"username":"lisi"} jackson默认就是保留null值 输出为: fastjson的JSONField注解提供了多种功能的组合 jackson不同的功能使用不同的注解来实现 fastjson默认会忽略未知属性 所以我们要将忽略未知属性的特性去掉 jackson默认未知属性报错 fastjson默认忽略未知属性 jackson默认未知属性报错,但也提供了反序列化特性来支持忽略。 fastjson反序列化数组和反序列化bean一样简单 上述两种方式都可以反序列化泛型集合或集合 fastjson和jackson使用对比 标签:parser 编写 static figure ack 结果 png row code 原文地址:https://www.cnblogs.com/strongmore/p/13906831.html介绍
Jackson是当前用的比较广泛的,用来序列化和反序列化json的Java开源框架。Jackson社区相对比较活跃,更新速度也比较快, 从Github中的统计来看,Jackson是最流行的json解析器之一,Spring MVC的默认json解析器便是Jackson。添加maven依赖
将java bean序列化为json字符串
过滤null值
public class Client1 {
public static void main(String[] args) {
User user = new User();
user.setUsername("lisi");
System.out.println(JSON.toJSONString(user));
}
@Setter
@Getter
static class User {
private String username;
private String password;
}
}
public class Client2 {
public static void main(String[] args) throws JsonProcessingException {
User user = new User();
user.setUsername("lisi");
ObjectMapper objectMapper = new ObjectMapper();
//只包含非null值
objectMapper.setSerializationInclusion(Include.NON_NULL);
System.out.println(objectMapper.writeValueAsString(user));
}
@Setter
@Getter
static class User {
private String username;
private String password;
}
}
保留null值
public class Client1 {
public static void main(String[] args) {
User user = new User();
user.setUsername("lisi");
//保留null值
System.out.println(JSON.toJSONString(user, SerializerFeature.WriteMapNullValue));
}
@Setter
@Getter
static class User {
private String username;
private String password;
}
}
public class Client2 {
public static void main(String[] args) throws JsonProcessingException {
User user = new User();
user.setUsername("lisi");
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.writeValueAsString(user));
}
@Setter
@Getter
static class User {
private String username;
private String password;
}
}
美化显示
public class Client1 {
public static void main(String[] args) {
User user = new User();
user.setUsername("lisi");
//保留null值,字符串格式化方式显示
System.out.println(JSON.toJSONString(user, SerializerFeature.WriteMapNullValue,
SerializerFeature.PrettyFormat));
}
@Setter
@Getter
static class User {
private String username;
private String password;
}
}
public class Client2 {
public static void main(String[] args) throws JsonProcessingException {
User user = new User();
user.setUsername("lisi");
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(user));
}
@Setter
@Getter
static class User {
private String username;
private String password;
}
}
{
"password":null,
"username":"lisi"
}
忽略属性和属性别名
public class Client1 {
public static void main(String[] args) {
User user = new User();
user.setOpenId("lisi");
user.setPassword("123456");
System.out.println(JSON.toJSONString(user));
}
@Setter
@Getter
@ToString
static class User {
/**
* 序列化忽略
*/
@JSONField(serialize = false)
private String openId;
/**
* 序列化名称为pwd
*/
@JSONField(name = "pwd")
private String password;
}
}
public class Client2 {
public static void main(String[] args) throws JsonProcessingException {
User user = new User();
user.setUsername("lisi");
user.setPassword("123456");
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.writeValueAsString(user));
}
@Setter
@Getter
static class User {
@JsonIgnore
private String username;
@JsonProperty("pwd")
private String password;
}
}
json字符串反序列化为java bean
未知属性报错
public class Client1 {
public static void main(String[] args) {
String jsonStr = "{\"username\":\"lisi\",\"pwd\":\"123456\"}";
int featureValue = JSON.DEFAULT_PARSER_FEATURE & ~Feature.IgnoreNotMatch.getMask();
User user = JSON.parseObject(jsonStr, User.class, featureValue);
System.out.println(user);
}
@Setter
@Getter
@ToString
static class User {
private String username;
private String password;
}
}
public class Client2 {
public static void main(String[] args) throws IOException {
String jsonStr = "{\"username\":\"lisi\",\"pwd\":\"123456\"}";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(jsonStr, User.class);
System.out.println(user);
}
@Setter
@Getter
@ToString
static class User {
private String username;
private String password;
}
}
忽略未知属性
public class Client1 {
public static void main(String[] args) {
String jsonStr = "{\"username\":\"lisi\",\"pwd\":\"123456\"}";
User user = JSON.parseObject(jsonStr, User.class);
System.out.println(user);
}
@Setter
@Getter
@ToString
static class User {
private String username;
private String password;
}
}
public class Client2 {
public static void main(String[] args) throws IOException {
String jsonStr = "{\"username\":\"lisi\",\"pwd\":\"123456\"}";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
User user = objectMapper.readValue(jsonStr, User.class);
System.out.println(user);
}
@Setter
@Getter
@ToString
static class User {
private String username;
private String password;
}
}
将java bean 数组序列化为字符串
public class Client1 {
public static void main(String[] args) {
List
public class Client2 {
public static void main(String[] args) throws JsonProcessingException {
List
json字符串反序列化为java bean数组
public class Client1 {
public static void main(String[] args) {
String jsonStr = "[{\"username\":\"lisi\",\"password\":\"123\"},{\"username\":\"Tony\",\"password1\":\"456\"}]";
List
public class Client2 {
public static void main(String[] args) throws IOException {
String jsonStr = "[{\"username\":\"lisi\",\"password\":\"123\"},{\"username\":\"Tony\",\"password\":\"456\"}]";
ObjectMapper objectMapper = new ObjectMapper();
List
>() {
});
System.out.println(userList);
}
@Setter
@Getter
@ToString
static class User {
private String username;
private String password;
}
}
public class Client2 {
public static void main(String[] args) throws IOException {
String jsonStr = "[{\"username\":\"lisi\",\"password\":\"123\"},{\"username\":\"Tony\",\"password\":\"456\"}]";
ObjectMapper objectMapper = new ObjectMapper();
//创建一个泛型类型
JavaType javaType = objectMapper.getTypeFactory()
.constructParametricType(List.class, User.class);
List
上一篇:Vue.js 源码目录设计
文章标题:fastjson和jackson使用对比
文章链接:http://soscw.com/index.php/essay/38368.html