当前位置:   article > 正文

Alibaba fastjson 序列化与反序列化_alibaba fastjson parseobject insecure deserializat

alibaba fastjson parseobject insecure deserialization

    fastjson在官网的定义号称最小最快 多态的json序列化工具。fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jackson。并且还超越了google的二进制协议protocol buf。

JSON这个类是fastjson API的入口,主要的功能都通过这个类提供。下面是这个类的重要几个方法介绍:


序列化API

  1. package com.alibaba.fastjson;
  2. public abstract class JSON {
  3. // 将Java对象序列化为JSON字符串,支持各种各种Java基本类型和JavaBean
  4. public static String toJSONString(Object object, SerializerFeature... features);
  5. // 将Java对象序列化为JSON字符串,返回JSON字符串的utf-8 bytes
  6. public static byte[] toJSONBytes(Object object, SerializerFeature... features);
  7. // 将Java对象序列化为JSON字符串,写入到Writer中
  8. public static void writeJSONString(Writer writer,
  9. Object object,
  10. SerializerFeature... features);
  11. // 将Java对象序列化为JSON字符串,按UTF-8编码写入到OutputStream中
  12. public static final int writeJSONString(OutputStream os, //
  13. Object object, //
  14. SerializerFeature... features);
  15. }



JSON字符串反序列化API


  1. package com.alibaba.fastjson;
  2. public abstract class JSON {
  3. // 将JSON字符串反序列化为JavaBean
  4. public static <T> T parseObject(String jsonStr,
  5. Class<T> clazz,
  6. Feature... features);
  7. // 将JSON字符串反序列化为JavaBean
  8. public static <T> T parseObject(byte[] jsonBytes, // UTF-8格式的JSON字符串
  9. Class<T> clazz,
  10. Feature... features);
  11. // 将JSON字符串反序列化为泛型类型的JavaBean
  12. public static <T> T parseObject(String text,
  13. TypeReference<T> type,
  14. Feature... features);
  15. // 将JSON字符串反序列为JSONObject
  16. public static JSONObject parseObject(String text);
  17. }



Demo


parse Tree

  1. import com.alibaba.fastjson.*;
  2. JSONObject jsonObj = JSON.parseObject(jsonStr);


parse POJO


  1. import com.alibaba.fastjson.JSON;
  2. Model model = JSON.parseObject(jsonStr, Model.class);




parse POJO Generic


  1. import com.alibaba.fastjson.JSON;
  2. Type type = new TypeReference<List<Model>>() {}.getType();
  3. List<Model> list = JSON.parseObject(jsonStr, type);




convert POJO to json string

  1. import com.alibaba.fastjson.JSON;
  2. Model model = ...;
  3. String jsonStr = JSON.toJSONString(model);




convert POJO to json bytes


  1. import com.alibaba.fastjson.JSON;
  2. Model model = ...;
  3. byte[] jsonBytes = JSON.toJSONBytes(model);




write POJO as json string to OutputStream


  1. import com.alibaba.fastjson.JSON;
  2. Model model = ...;
  3. OutputStream os;
  4. JSON.writeJSONString(os, model);




write POJO as json string to Writer


  1. import com.alibaba.fastjson.JSON;
  2. Model model = ...;
  3. Writer writer = ...;
  4. JSON.writeJSONString(writer, model);


总结:


    更多关于fastjson的知识可以去w3c去了解:https://www.w3cschool.cn/fastjson/


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/923037
推荐阅读
相关标签
  

闽ICP备14008679号