JackSon

①FastJson简单用法     ②Google Gson简单用法   ③笔记来源

一、JackSon介绍


  • Jackson 是用来序列化和反序列化 Json 的 Java 的开源框架。
  • Spring MVC 的默认 Json 解析器便是 Jackson。
  • 与其他 Java 的 Json 框架 Gson 等相比, Jackson 解析大的 Json 文件速度比较快。
  • Jackson 运行时占用内存比较低,性能比较好。
  • Jackson 有灵活的 API,可以很容易进行扩展和定制。​​​​​​​

二、JackSon序列化和反序列化


1、实体类 

@Data
public class DgjTestEntity {
    private String name;
    private int age;
    private String sex;
    private Date birthday;
    private String email;
    private String address;

    public DgjTestEntity(String name, int age, String sex, Date birthday, String email, String address) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.birthday = birthday;
        this.email = email;
        this.address = address;
    }
}

2、序列化【writeValueAsString】

public void jacksonTset() throws Exception {

    ObjectMapper mapper = new ObjectMapper();

    /*------- 对象 序列化 Json 字符串 -------*/
    DgjTestEntity entity = new DgjTestEntity("dgs",23,"男",new Date(),"dgs@qq.com","");
    String jsonObjectToString = mapper.writeValueAsString(entity);
    System.out.println(jsonObjectToString);
    //{"name":"dgs","age":23,"sex":"男","birthday":1650872520038,"email":"dgs@qq.com","address":""}

    DgjTestEntity entity1 = new DgjTestEntity("dgs",23,"男",new Date(),"dgs@qq.com","");
    DgjTestEntity entity2 = new DgjTestEntity("dgs",23,"男",new Date(),"dgs@qq.com","");
    /*------- Map 序列化 Json 字符串 -------*/
    Map     map     = new HashMap();
    map.put("entity1",entity1);
    map.put("entity2",entity2);
    String jsonMapToString = mapper.writeValueAsString(map);
    System.out.println(jsonMapToString);
    //{"entity1":{"name":"dgs","age":23,"sex":"男","birthday":1650872840157,"email":"dgs@qq.com","address":""},
    // "entity2":{"name":"dgs","age":23,"sex":"男","birthday":1650872840157,"email":"dgs@qq.com","address":""}}

    /*------- List 序列化 Json 字符串 -------*/
    List    list    = new ArrayList();
    list.add(entity1);
    list.add(entity2);
    String jsonListToString = mapper.writeValueAsString(list);
    System.out.println(jsonListToString);
    //[{"name":"dgs","age":23,"sex":"男","birthday":1650872840157,"email":"dgs@qq.com","address":""},
    // {"name":"dgs","age":23,"sex":"男","birthday":1650872840157,"email":"dgs@qq.com","address":""}]

    /*------- int[] 序列化 Json 字符串 -------*/
    int[] intArray = {1,2,3,4,5,6,7,8,9,10};
    String intArrayToJson = mapper.writeValueAsString(intArray);
    System.out.println(intArrayToJson);
    //[1,2,3,4,5,6,7,8,9,10]

    /*------- String[] 序列化 Json 字符串 -------*/
    String[] stringArray = {"1,2,3,4,5,6,7,8,9,10","DGS"};
    String stringArrayToJson = mapper.writeValueAsString(stringArray);
    System.out.println(stringArrayToJson);
}

3、反序列化 【readValue】

public void jacksonTset() throws Exception {

    ObjectMapper mapper = new ObjectMapper();

    String jsonToOject = "{\"name\":\"dgs\",\"age\":23,\"sex\":\"男\",\"birthday\":1650872520038,\"email\":\"dgs@qq.com\",\"address\":\"\"}";
    String jsonToMap = "{\"entity1\":{\"name\":\"dgs\",\"age\":23,\"sex\":\"男\",\"birthday\":1650872840157,\"email\":\"dgs@qq.com\",\"address\":\"\"},\n" +
            "         \"entity2\":{\"name\":\"dgs\",\"age\":23,\"sex\":\"男\",\"birthday\":1650872840157,\"email\":\"dgs@qq.com\",\"address\":\"\"}}";
    String jsonToList = "[{\"name\":\"dgs\",\"age\":23,\"sex\":\"男\",\"birthday\":1650872840157,\"email\":\"dgs@qq.com\",\"address\":\"\"},\n" +
            "        {\"name\":\"dgs\",\"age\":23,\"sex\":\"男\",\"birthday\":1650872840157,\"email\":\"dgs@qq.com\",\"address\":\"\"}]";
    String jsonToIntList = "[1,2,3,4,5,6,7,8,9,10]";
    String jsonToStringList = "[\"1,2,3,4,5,6,7,8,9,10\",\"DGS\"]";

    DgjTestEntity entity = mapper.readValue(jsonToOject, DgjTestEntity.class);
    System.out.println(entity);
    //DgjTestEntity(name=dgs, age=23, sex=男, birthday=Mon Apr 25 15:42:00 CST 2022, email=dgs@qq.com, address=)

    Map<String, DgjTestEntity> map = mapper.readValue(jsonToMap, new TypeReference<Map<String, DgjTestEntity>>() {});
    System.out.println(map);
    //{entity1=DgjTestEntity(name=dgs, age=23, sex=男, birthday=Mon Apr 25 15:47:20 CST 2022, email=dgs@qq.com, address=),
    // entity2=DgjTestEntity(name=dgs, age=23, sex=男, birthday=Mon Apr 25 15:47:20 CST 2022, email=dgs@qq.com, address=)}

    List<DgjTestEntity> list = mapper.readValue(jsonToList, new TypeReference<List<DgjTestEntity>>() {});
    System.out.println(list);
    //[DgjTestEntity(name=dgs, age=23, sex=男, birthday=Mon Apr 25 15:47:20 CST 2022, email=dgs@qq.com, address=),
    // DgjTestEntity(name=dgs, age=23, sex=男, birthday=Mon Apr 25 15:47:20 CST 2022, email=dgs@qq.com, address=)]

    int[] ints = mapper.readValue(jsonToIntList, int[].class);
    System.out.println(ints);

    String[] strings = mapper.readValue(jsonToStringList, String[].class);
    System.out.println(strings.toString());

}

三、@JsonProperty注解


 对类的属性使用@JsonProperty注解,可以重新指定与该属性对应的JSON字符串中的名字

public void jacksonTset() throws Exception {

    /*实体类
    * @JsonProperty("userName")
    * private String name;
    * */
    
    ObjectMapper mapper = new ObjectMapper();

    /*------- 对象 序列化 Json 字符串 -------*/
    DgjTestEntity entity = new DgjTestEntity("dgs",23,"男",new Date(),"dgs@qq.com","");
    String jsonObjectToString = mapper.writeValueAsString(entity);
    System.out.println(jsonObjectToString);
    //{"age":23,"sex":"男","birthday":1650874382780,"email":"dgs@qq.com","address":"","userName":"dgs"}

}

补充注解:
@JsonIgnore 作用在字段上面,序列化的时候忽略改字段
@JsonIgnoreProperties({"name","age"}) 作用在实体类上面,序列化的时候忽略里面的字段


四、处理对象的Null 属性


方案一:注解@JsonInclude()

public void jacksonTset() throws Exception {
    /*实体类
    * @JsonInclude(JsonInclude.Include.NON_NULL)
    * public class DgjTestEntity {
    *   private String name;
    *   private int age;
    *   private String sex;
    *   private Date birthday;
    *   private String email;
    *   private String address;
    * }
    * */
    ObjectMapper mapper = new ObjectMapper();
    DgjTestEntity entity = new DgjTestEntity(null,23,"男",new Date(),"dgs@qq.com",null);
    String jsonObjectToString = mapper.writeValueAsString(entity);
    System.out.println(jsonObjectToString);
    //{"age":23,"sex":"男","birthday":1650875540546,"email":"dgs@qq.com"}
}

 方案二:配置 setSerializationInclusion

public void jacksonTset() throws Exception {
    /*实体类
    * public class DgjTestEntity {
    *   private String name;
    *   private int age;
    *   private String sex;
    *   private Date birthday;
    *   private String email;
    *   private String address;
    * }
    * */
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    DgjTestEntity entity = new DgjTestEntity(null,23,"男",new Date(),"dgs@qq.com",null);
    String jsonObjectToString = mapper.writeValueAsString(entity);
    System.out.println(jsonObjectToString);
    //{"age":23,"sex":"男","birthday":1650875540546,"email":"dgs@qq.com"}
}

五、格式化Date类型属性


 @JsonFormat 注解提供了 pattern 属性用以自定义其字符串格式

public void jacksonTset() throws Exception {
    /*实体类
    * public class DgjTestEntity {
    *   private String name;
    *   private int age;
    *   private String sex;
    *   @JsonFormat(pattern = "yyyy-MM-dd")
    *   private Date birthday;
    *   private String email;
    *   private String address;
    * }
    * */
    ObjectMapper mapper = new ObjectMapper();
    DgjTestEntity entity = new DgjTestEntity(null,23,"男",new Date(),"dgs@qq.com",null);
    String jsonObjectToString = mapper.writeValueAsString(entity);
    System.out.println(jsonObjectToString);
    //{"name":null,"age":23,"sex":"男","birthday":"2022-04-25","email":"dgs@qq.com","address":null}
}

六、其他


   Jackson 支持 Json 字符串 和 Map 对象 之间的转换

public void jacksonTset() throws Exception {

    ObjectMapper mapper = new ObjectMapper();
    Map<Object,Object> map = new HashMap<>();
    map.put(521,"1314");
    map.put("我不信","我也不行");
    map.put('A',"好好生活");
    map.put(true,new int[]{1,2,3,4});

    String mapToJson = mapper.writeValueAsString(map);
    System.out.println(mapToJson);
    //{"A":"好好生活","521":"1314","我不信":"我也不行","true":[1,2,3,4]}

    Map jsonToMap = mapper.readValue(mapToJson, Map.class);
    for (Object key : jsonToMap.keySet()){
        System.out.print(key+":"+jsonToMap.get(key)+" ");
    }
    //A:好好生活 521:1314 我不信:我也不行 true:[1, 2, 3, 4] 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值