当前位置:网站首页>Differences and precautions of fastjson, jackjson and gson
Differences and precautions of fastjson, jackjson and gson
2022-07-19 04:57:00 【dzl84394】
transformation
Prepare one vo
@Data
public class Person implements Serializable {
private Integer id;
private BigDecimal amount;
private BigDecimal amountNUll;
private String name;
private String P;// Just a capital letter
private String XYZabc;// continuity 3 Capital letters
private String XYNULL;
private Date createTime;
public Person(Integer id, String name, Date createTime) {
this.id = id;
this.name = name;
this.createTime = createTime;
}
}
1、string turn object
String json = "{\"XYZabc\":\"XYZ\",\"amount\":8.88,\"createTime\":1654931545521,\"id\":1,\"name\":\"DZL\",\"p\":\"PPP\"}";
// The way 1: take json String to Java object
Person person = JSON.parseObject(json,Person.class);
System.out.println("fastjson:java object ->>>"+person);
// The way 2: take json String to Java object
Person newPerson2 = JSONObject.parseObject(json, Person.class);
System.out.println("fastjson:java object ->>>"+newPerson2);
System.out.println("=========================================================");
ObjectMapper objectMapper = new ObjectMapper();
try {
Person jackPerson = objectMapper.readValue(json, Person.class);
System.out.println("java object ->>>"+jackPerson);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
System.out.println("=========================================================");
final Gson gson = new Gson();
Person gsonPerson = gson.fromJson(json, Person.class);
System.out.println("java object ->>>"+gsonPerson);
- jackjson I can't make it in capital P,XYZabc Such a property key, All need to be changed to lowercase ,fast and gson No big problem
- gson incognizance 1654931545521 Such timestamp
2、object turn string
Person person = new Person(1,"DZL",new Date());
person.setP("PPP");
person.setXY("XYZ");
person.setAmount(new BigDecimal("8.88"));
// The way 1: Convert object to json character string
String json = JSON.toJSONString(person);
System.out.println("fast json character string 1->>>"+json);
System.out.println("=========================================================");
// The way 2: Convert object to json character string
String json2 = JSONObject.toJSONString(person);
System.out.println("fast json character string 1->>>"+json2);
System.out.println("=========================================================");
ObjectMapper objectMapper = new ObjectMapper();
try {
String jackStr = objectMapper.writeValueAsString(person);
System.out.println("jack json character string ->>>"+jackStr);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
System.out.println("=========================================================");
Gson gson = new Gson();
String gsonStr = gson.toJson(person);
System.out.println("gson json character string ->>>"+gsonStr);
result
fast json character string 1->>>{"XYZabc":"XYZ","amount":8.88,"createTime":1654931545521,"id":1,"name":"DZL","p":"PPP"}
=========================================================
fast json character string 1->>>{"XYZabc":"XYZ","amount":8.88,"createTime":1654931545521,"id":1,"name":"DZL","p":"PPP"}
=========================================================
jack json character string ->>>{"id":1,"amount":8.88,"amountNUll":null,"name":"DZL","createTime":1654931545521,"p":"PPP","xyzabc":"XYZ","xynull":null}
=========================================================
gson json character string ->>>{"id":1,"amount":8.88,"name":"DZL","P":"PPP","XYZabc":"XYZ","createTime":"Jun 11, 2022 3:12:25 PM"}
difference
1、 String and BigDecimal If the value of all is null,fastjson, and gson No printing by default ,jack It is printed by default
2、 Attribute names are capitalized , Like a P,fast,jack It's lowercase ,gson Still capitalized ;
3、 Attribute names such as XYZabc The first letter 2 A capital letter ,fast and gson Keep it as it is. ,fast All lowercase
3、array turn json
public static void fast_list2str_test(){
List<Person> personList = new ArrayList<>();
personList.add(new Person(1,"DT",new Date()));
personList.add(new Person(2,"DT1",new Date()));
personList.add(new Person(3,"DT2",new Date()));
String json = JSONObject.toJSONString(personList);
System.out.println("fast json->>>"+json);
}
public static void jack_list2str_test() {
ObjectMapper objectMapper = new ObjectMapper();
List<Person> personList = new ArrayList<>();
personList.add(new Person(1,"DT",new Date()));
personList.add(new Person(2,"DT1",new Date()));
personList.add(new Person(3,"DT2",new Date()));
String json = null;
try {
json = objectMapper.writeValueAsString(personList);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
System.out.println("json->>>"+json);
}
public static void gson_list2str_test(){
Gson gson = new Gson();
List<Person> personList = new ArrayList<>();
personList.add(new Person(1,"DT",new Date()));
personList.add(new Person(2,"DT1",new Date()));
personList.add(new Person(3,"DT2",new Date()));
String json = gson.toJson(personList);
System.out.println("json->>>"+json);
}
4、json turn array
5、 notes
5.1、 Sort
// fastjson
Class plus
@JSONType(orders = {"id","name","XYZabc","createTime"})
// jacckjson
@JsonProperty(index = 2)
private String channel
5.2、 Exempt or include
- fast
// When serializing , Immunity realName
@JSONField(serialize=false)
public String realName;
@JSONType(ignores ={“id”,“sex”})
public class I'm a class
@JSONType(includes={“name”,“sex”})
public class I'm a class
- jack
- gson
5.3、 format
fast Name it
@JSONField(name=“gender”)
public String sex;jackjson
@JsonFormat(pattern =“yyyy-MM-dd HH:mm:ss”)
private LocalDateTime createTime;fastjson
@JsonFormat(pattern =“yyyy-MM-dd HH:mm:ss”)
private LocalDateTime createTime;
5.4、 Print if not empty ( Ignore null)
// jackjson
@JsonIgnore
private LocalDateTime createTime;
@JsonIgnore
public LocalDateTime getCreateTime(){
return createTime;
}
// fastjson
@JSONField(serialize = false)
private String name;
Or use include above the class , Elimination and other methods
// gson
@Expose
private String user;
5.5、 Mapping aliases
1)、fastjson
@JSONField(name="person_name")
private String name;
@JSONField(name="person_age")
private Integer age;
2)、jackjson
@JsonAlias({“n”,“Name”})
private String name;
3)、gson
@SerializedName(“Name”)
private String name;
@SerializedName(“Pwd”)
private String pwd;
边栏推荐
猜你喜欢
随机推荐
sleuth入门
Rearrange data according to date JS
DirectExchange交换机的简单使用。
God rewards hard work, keep loving
String字符串根据符号进行特殊截取处理
Hightec new aurix tc37x demo project
shardingsphere的核心概念和快速实战
Learn about scheduled tasks in one article
Shallow chat link tracking
日志加入数据库实现思路
npm安装教程
RestAPI
高等数学笔记:复合函数的二阶导数与参数方程求解曲率
shardingsphere内核原理
中台的订单系统
高等数学笔记:伍月习题选集
NoSQL概述
新生任务-5
Deleting snapshot: error deleting snapshot: Dictionary problem
shardingproxy分库分表实战及同类产品对比






![[FPGA tutorial case 27] realize dual port RAM ping-pong structure through Verilog](/img/64/211c5a6d6e0a8701136fa969d6d9e4.png)


