fastjson 序列化


fastjson 序列化

 

 

************************

相关类

 

JSON

public abstract class JSON implements JSONStreamAware, JSONAware {


*************
序列化为 String

    public static String toJSONString(Object object)
    public static String toJSONString(Object object, SerializerFeature... features) {
    public static String toJSONString(Object object, int defaultFeatures, SerializerFeature... features) {

    public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features) {
    public static String toJSONString(Object object, SerializeFilter[] filters, SerializerFeature... features) {

    public static String toJSONString(Object object, SerializeConfig config, SerializerFeature... features) {
    public static String toJSONString(Object object, SerializeConfig config, SerializeFilter filter, SerializerFeature... features) {
    public static String toJSONString(Object object, SerializeConfig config, SerializeFilter[] filters, SerializerFeature... features) {
    public static String toJSONString(Object object, SerializeConfig config,  SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) {

    public static String toJSONStringWithDateFormat(Object object, String dateFormat, SerializerFeature... features) {

    public static String toJSONString(Object object, boolean prettyFormat) {

    public static void writeJSONString(Writer writer, Object object, SerializerFeature... features) {
    public static void writeJSONString(Writer writer, Object object, int defaultFeatures, SerializerFeature... features) {

    public static final int writeJSONString(OutputStream os, Object object, SerializerFeature... features) throws IOException {
    public static final int writeJSONString(OutputStream os, Object object, int defaultFeatures, SerializerFeature... features) throws IOException {
    public static final int writeJSONString(OutputStream os, Charset charset, Object object, SerializerFeature... features) throws IOException {
    public static final int writeJSONString(OutputStream os, Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat,  int defaultFeatures, SerializerFeature... features) throws IOException {
    public static final int writeJSONStringWithFastJsonConfig(OutputStream os, Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) throws IOException {



*************
序列化为 byte[]

    public static byte[] toJSONBytes(Object object, SerializerFeature... features)
    public static byte[] toJSONBytes(Object object, int defaultFeatures, SerializerFeature... features) {

    public static byte[] toJSONBytes(Object object, SerializeFilter filter, SerializerFeature... features) {
    public static byte[] toJSONBytes(Object object, SerializeFilter[] filters, SerializerFeature... features) {

    public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializerFeature... features) {
    public static byte[] toJSONBytes(Object object, SerializeConfig config, int defaultFeatures, SerializerFeature... features) {
    public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter filter, SerializerFeature... features) {
    public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter[] filters, int defaultFeatures, SerializerFeature... features) {
    public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) {

    public static byte[] toJSONBytes(Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures,  SerializerFeature... features) {

    public static byte[] toJSONBytesWithFastJsonConfig(Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) {



*************
反序列化为 Object

    public static Object parse(String text) {
    public static Object parse(String text, ParserConfig config) {
    public static Object parse(String text, ParserConfig config, Feature... features) {
    public static Object parse(String text, ParserConfig config, int features) {
    public static Object parse(String text, int features) {
    public static Object parse(String text, Feature... features) {


    public static Object parse(byte[] input, Feature... features) {
    public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Feature... features) {
    public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features) {


*************
反序列化为 JSONObject

    public static JSONObject parseObject(String text) {
    public static JSONObject parseObject(String text, Feature... features) {


*************
反序列化为 T

    public static <T> T parseObject(String text, Class<T> clazz) {
    public static <T> T parseObject(String text, TypeReference<T> type, Feature... features) {
    public static <T> T parseObject(String text, Class<T> clazz, ParseProcess processor, Feature... features) {

    public static <T> T parseObject(String json, Class<T> clazz, Feature... features) {
    public static <T> T parseObject(String json, Type type, Feature... features) {

    public static <T> T parseObject(String input, Type clazz, ParseProcess processor, Feature... features) {
    public static <T> T parseObject(String input, Type clazz, int featureValues, Feature... features) {
    public static <T> T parseObject(String input, Type clazz, ParserConfig config, Feature... features) {
    public static <T> T parseObject(String input, Type clazz, ParserConfig config, int featureValues,
    public static <T> T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor,


    public static <T> T parseObject(byte[] bytes, Type clazz, Feature... features) {
    public static <T> T parseObject(byte[] bytes, int offset, int len, Charset charset, Type clazz, Feature... features) {
    public static <T> T parseObject(byte[] bytes, Charset charset, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) {
    public static <T> T parseObject(byte[] bytes, int offset, int len, Charset charset, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) {

    public static <T> T parseObject(byte[] input,  int off, int len, CharsetDecoder charsetDecoder, Type clazz, Feature... features) {
    public static <T> T parseObject(char[] input, int length, Type clazz, Feature... features) {

    public static <T> T parseObject(InputStream is, Type type, Feature... features) throws IOException {
    public static <T> T parseObject(InputStream is, Charset charset, Type type,  Feature... features) throws IOException {
    public static <T> T parseObject(InputStream is, Charset charset, Type type, ParserConfig config, Feature... features) throws IOException {
    public static <T> T parseObject(InputStream is, Charset charset, Type type,  ParserConfig config, ParseProcess processor, int featureValues, Feature... features) throws IOException {


*************
反序列化为 JSONArray

    public static JSONArray parseArray(String text) {
    public static JSONArray parseArray(String text, ParserConfig parserConfig) {



*************
反序列化为 List

    public static <T> List<T> parseArray(String text, Class<T> clazz) {
    public static <T> List<T> parseArray(String text, Class<T> clazz, ParserConfig config) {

    public static List<Object> parseArray(String text, Type[] types) {
    public static List<Object> parseArray(String text, Type[] types, ParserConfig config) {


*************
其他方法

    public static Object toJSON(Object javaObject) {
    public static Object toJSON(Object javaObject, SerializeConfig config) {

    public static <T> T toJavaObject(JSON json, Class<T> clazz) {

    public <T> T toJavaObject(Class<T> clazz) {
    public <T> T toJavaObject(Type type) {
    public <T> T toJavaObject(TypeReference typeReference) {

    public void writeJSONString(Appendable appendable) {

    public String toString() {
    public String toJSONString() {
    public String toString(SerializerFeature... features) {

 

 

*******************

示例

 

User

@Data
public class User {

    private Integer id;
    private String name;
    private Integer age;
}

 

*******************

对象序列化、反序列化

 

public class JsonTest {

    public static void main(String[] args){
        User user=new User();
        user.setId(12);
        user.setName("瓜田李下");
        user.setAge(18);

        byte[] bytes= JSON.toJSONBytes(user);  //对象序列化成字节数组
        System.out.println(Arrays.toString(bytes));

        User u=JSON.parseObject(bytes,User.class);  //字节数组反序列化成对象
        System.out.println(u);
    }
}

 

*******************

控制台输出

 

[123, 34, 97, 103, 101, 34, 58, 49, 56, 44, 34, 105, 100, 34, 58, 49, 50, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 34, 125]
User(id=12, name=瓜田李下, age=18)

 

 

*******************

列表序列化、反序列化

 

public class JsonTest {

    public static void main(String[] args){
        List<User> list=new ArrayList<>();
        for(int i=0;i<10;i++){
            User u1=new User();
            u1.setId(i+1);
            u1.setName("瓜田李下"+i);
            u1.setAge(23);

            list.add(u1);
        }

        byte[] b1=JSON.toJSONBytes(list);   //对象列表序列化成字节数组
        System.out.println(Arrays.toString(b1));

        List<User> l=JSON.parseArray(new String(b1),User.class); 
                                            //字节数组反序列化成对象列表
        l.foreach(System.out::println);
    }
}

 

*******************

控制台输出

 

[91, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 49, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 48, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 50, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 49, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 51, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 50, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 52, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 51, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 53, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 52, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 54, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 53, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 55, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 54, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 56, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 55, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 57, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 56, 34, 125, 44, 123, 34, 97, 103, 101, 34, 58, 50, 51, 44, 34, 105, 100, 34, 58, 49, 48, 44, 34, 110, 97, 109, 101, 34, 58, 34, -25, -109, -100, -25, -108, -80, -26, -99, -114, -28, -72, -117, 57, 34, 125, 93]
User(id=1, name=瓜田李下0, age=23)
User(id=2, name=瓜田李下1, age=23)
User(id=3, name=瓜田李下2, age=23)
User(id=4, name=瓜田李下3, age=23)
User(id=5, name=瓜田李下4, age=23)
User(id=6, name=瓜田李下5, age=23)
User(id=7, name=瓜田李下6, age=23)
User(id=8, name=瓜田李下7, age=23)
User(id=9, name=瓜田李下8, age=23)
User(id=10, name=瓜田李下9, age=23)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fastjson是一款流行的Java JSON库,用于序列化和反序列化Java对象和JSON数据。在Fastjson 1.2.24及之前的版本中存在一个安全漏洞,被称为Fastjson序列化漏洞(Fastjson Deserialization Vulnerability)或Fastjson序列化漏洞。 该漏洞的主要原因是Fastjson在反序列化过程中存在一些不安全的默认行为,可能导致恶意攻击者利用特制的JSON数据触发远程代码执行。攻击者可以构造恶意JSON数据,利用漏洞触发任意代码执行、命令执行、远程命令执行等攻击。 Fastjson团队在发现漏洞后迅速发布了修复版本,并建议所有使用Fastjson的开发者升级到最新版本以解决安全问题。此外,开发者还可以采取以下措施来防止Fastjson序列化漏洞的利用: 1. 及时升级:确保使用的Fastjson版本是修复了该漏洞的最新版本。 2. 输入验证:在接收JSON数据并进行反序列化之前,对输入进行严格验证和过滤,确保只接受可信任的数据。 3. 白名单机制:限制反序列化过程中可以实例化的类和调用的方法,使用白名单机制来控制允许的操作。 4. 安全配置:通过配置Fastjson的ParserConfig,禁用自动类型识别(autoTypeSupport)或限制白名单(setAccept)等来增强安全性。 总结而言,Fastjson序列化漏洞是由于Fastjson在反序列化过程中的不安全默认行为导致的安全问题。及时升级Fastjson版本、输入验证、白名单机制和安全配置等措施可以帮助防止该漏洞的利用。 请注意,本回答仅涉及Fastjson序列化漏洞的概述,具体防范措施可能因应用场景和需求而有所不同。建议在实际开发中仔细研究并采取适合的安全措施来保护应用程序免受潜在攻击。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值