JackSon

JackSon,第1张

FastJson链接

一、JackSon介绍

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

笔记参考出处https://www.bilibili.com/video/BV1gJ411d7ai?p=6


二、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 map = mapper.readValue(jsonToMap, new TypeReference>() {});
    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 list = mapper.readValue(jsonToList, new TypeReference>() {});
    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 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] 
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/741933.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存