序列化(使用Jackson)

我的飞书:https://rvg7rs2jk1g.feishu.cn/docx/BVz7d7CEaoDJHLxnuD4cC6XdnMQ

为什么要使用序列化?

将数据标准化.通常情况下,一个程序会有很多对象,但是为了更方便的统一处理,我们将他们进行数据标准化,转换成同样的格式,提高兼容性,这就是序列化

以下我只介绍使用Jackson进行序列化

设置对象

我们先设置一些简单的对象

@Data
public class CommonResult implements Serializable {
    private Integer code;
    private T data;
    private String msg;

    public static  CommonResult success(T data){
        CommonResult commonResult = new CommonResult<>();
        commonResult.setCode(GlobalErrorCodeConstants.SUCCESS.getCode());
        commonResult.setData(data);
        commonResult.setMsg("");
        return commonResult;
    }

    public static  CommonResult error(Integer code,String msg){
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),"code 没有发送异常");
        CommonResult commonResult = new CommonResult<>();
        commonResult.setCode(code);
        commonResult.setMsg(msg);
        return commonResult;
    }

    public static  CommonResult error(ErrorCode code){
        return error(code.getCode(),code.getMsg());
    }
}

序列化

不管是序列化,还是反序列化都是需要使用 ObjectMapper 对象,同时序列化和反序列化都是会抛出异常的!!

        1.序列化

我设定的序列化是将对象转换为字符串类型,需要使用到 ObjectMapper 对象的方法 --> writeValueAsString

方法名

参数

转换结果

writeValueAsString ()

需要进行转化的对象

字符串        

        2.反序列化

反序列化也是需要使用 ObjectMapper 对象的,需要将字符串转换为对应的类型,同时这个类型是可以进行设置的(方法返回的是泛型)

方法名

参数

转换结果

readValue()

(需要进行转换的字符串,要转换为的对象格式)

设定的对象

所以反序列化得到的结果,就是和参数中设定的结果是同一类型的

        3.代码演示

//序列化
ObjectMapper objectMapper = new ObjectMapper();
String str;
try {
    str = objectMapper.writeValueAsString(CommonResult.success("success"));
    System.out.println(str);
} catch (JsonProcessingException e) {
    throw new RuntimeException(e);
}

//反序列化
try {
    CommonResult commonResult = objectMapper.readValue(str, CommonResult.class);
    System.out.println(commonResult);
} catch (JsonProcessingException e) {
    throw new RuntimeException(e);
}

序列化(使用Jackson)_第1张图片

代码首先通过序列化将一个CommonResult类型的数据转换成的字符串,再通过反序列化将字符串转换成CommonResult形式的数据

但是!链表类型的数据进行反序列化时,需要进行特殊处理!

链表操作

        1.序列化

链表的序列化还是和单个对象的序列化是一样的

List> lists = Arrays.asList(
        CommonResult.success("success1"),
        CommonResult.success("success2"));
try {
    str = objectMapper.writeValueAsString(lists);
    System.out.println(str);
} catch (JsonProcessingException e) {
    throw new RuntimeException(e);
}

序列化(使用Jackson)_第2张图片

        2.反序列化

在使用反序列化时,我们无法直接将字符串转换成我们想要的对象,因为直接进行转换,得到的是一个链表,所以我们需要先对类型进行一个声明

这里使用到的是 JavaType,constructParametricType表明,第一个参数表示要生成的对象是一个list类型,第二个参数表明的是list里面的元素是什么类型

JavaType javaType = objectMapper
    .getTypeFactory()
    .constructParametricType(List.class,CommonResult.class);
try {
   lists = objectMapper.readValue(str,javaType);
   for (CommonResult list: lists){
       System.out.println(list);
   }
} catch (JsonProcessingException e) {
    throw new RuntimeException(e);
}

 

所以,spring也构造了反序列化的方法(因为序列化大多都是一样的,所以没有单独列出)

使用SpringBoot反序列化

springboot进行反序列化,都是调用了一个tryParse方法

//反序列化map
public Map parseMap(String json) {
    return (Map)this.tryParse(() -> {
        return (Map)this.getObjectMapper().readValue(json, MAP_TYPE);
    }, Exception.class);
}
//反序列化list
public List parseList(String json) {
    return (List)this.tryParse(() -> {
        return (List)this.getObjectMapper().readValue(json, LIST_TYPE);
    }, Exception.class);
}

//调用方法
protected final  T tryParse(Callable parser, Class check) {
    try {
        return parser.call();
        //调用lambda表达式,统一处理异常
    } catch (Exception var4) {
        if (check.isAssignableFrom(var4.getClass())) {
            //判断异常是否在预期中,是就抛出
            throw new JsonParseException(var4);
        } else {
            ReflectionUtils.rethrowRuntimeException(var4);
            throw new IllegalStateException(var4);
        }
    }
} 
  

你可能感兴趣的:(java,开发语言,spring,boot)