fastjson、jackjson、gson区别和注意点

转换

准备一个vo
@Data
public class Person implements Serializable {
    private Integer id;
    private BigDecimal amount;
    private BigDecimal amountNUll;
    private String name;
    private String P;//就一个大写字母
    private String XYZabc;//连续3个大写字母
    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转object

 	String json = "{\"XYZabc\":\"XYZ\",\"amount\":8.88,\"createTime\":1654931545521,\"id\":1,\"name\":\"DZL\",\"p\":\"PPP\"}";
        // 方式1: 将json字符串转为Java对象
        Person person = JSON.parseObject(json,Person.class);
        System.out.println("fastjson:java对象->>>"+person);

        // 方式2: 将json字符串转为Java对象
        Person newPerson2 = JSONObject.parseObject(json, Person.class);
        System.out.println("fastjson:java对象->>>"+newPerson2);

        System.out.println("=========================================================");
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Person jackPerson = objectMapper.readValue(json, Person.class);
            System.out.println("java对象->>>"+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对象->>>"+gsonPerson);
  • jackjson 搞不定大写的P,XYZabc这样的属性key,都需要改小写,fast和gson都没大问题
  • gson 不认识1654931545521 这样的时间戳

2、object转string

 	Person person = new Person(1,"DZL",new Date());
        person.setP("PPP");
        person.setXY("XYZ");
        person.setAmount(new BigDecimal("8.88"));

        // 方式1:将对象转为json字符串
        String json = JSON.toJSONString(person);
        System.out.println("fast json字符串1->>>"+json);
        System.out.println("=========================================================");
        // 方式2:将对象转为json字符串
        String json2 = JSONObject.toJSONString(person);
        System.out.println("fast json字符串1->>>"+json2);

        System.out.println("=========================================================");

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String jackStr = objectMapper.writeValueAsString(person);
            System.out.println("jack json字符串->>>"+jackStr);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println("=========================================================");

        Gson gson = new Gson();
        String gsonStr = gson.toJson(person);
        System.out.println("gson json字符串->>>"+gsonStr);

结果

fast json字符串1->>>{"XYZabc":"XYZ","amount":8.88,"createTime":1654931545521,"id":1,"name":"DZL","p":"PPP"}
=========================================================
fast json字符串1->>>{"XYZabc":"XYZ","amount":8.88,"createTime":1654931545521,"id":1,"name":"DZL","p":"PPP"}
=========================================================
jack json字符串->>>{"id":1,"amount":8.88,"amountNUll":null,"name":"DZL","createTime":1654931545521,"p":"PPP","xyzabc":"XYZ","xynull":null}
=========================================================
gson json字符串->>>{"id":1,"amount":8.88,"name":"DZL","P":"PPP","XYZabc":"XYZ","createTime":"Jun 11, 2022 3:12:25 PM"}

区别
1、字符串和BigDecimal都的值如果是null,fastjson,和gson都默认没有打印,jack是默认打印的
2、属性名称首字母大写,如一个P,fast,jack变小写了,gson依然是大写;
3、属性名称如XYZabc 首字母2个大写字母的,fast和gson保持原样,fast全小写了

3、array转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转array

5、注释

5.1、排序

// fastjson
类上面加
@JSONType(orders = {"id","name","XYZabc","createTime"})

// jacckjson
    @JsonProperty(index = 2)
    private String channel

5.2、豁免或包含

  • fast
    // 序列化的时候,豁免realName
    @JSONField(serialize=false)
    public String realName;

@JSONType(ignores ={“id”,“sex”})
public class 我是一个类

@JSONType(includes={“name”,“sex”})
public class 我是一个类

  • jack
  • gson

5.3、 format

  • fast 起个别名
    @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、非空是否打印(忽略null)

// jackjson
@JsonIgnore
private LocalDateTime createTime;

@JsonIgnore
public LocalDateTime getCreateTime(){
	return createTime;
}

//  fastjson
@JSONField(serialize = false)
private String name;
或者类上面用包含,排除等方法

//  gson
 @Expose
    private String user;

5.5、 映射别名

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值