Java native serialization, json serialization, xstream serialization, hessian serialization

package com.test.thread.newlw.net;

/**
 * @author 25338
 * @version 1.0
 * @date 2021/12/28 15:03
 */
public interface IService {
    
    
    /**
     * 序列化
     * @param t
     * @param <T>
     * @return
     */
    <T> byte[] read(T t);

    /**
     * 反序列化
     * @param bytes
     * @param tClass
     * @param name
     * @param <T>
     * @return
     */
    <T> T write(byte[] bytes,Class<T> tClass,String name);
}

1. java serialization

package com.test.thread.newlw.net;


import com.test.Files.FileUtil;

import java.io.*;

/**
 * @author 25338
 * @version 1.0
 * @date 2021/12/28 15:00
 */
public class Javasericial implements IService {
    
    

    @Override
    public <T> byte[] read(T t) {
    
    
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream outputStream = null;
        try {
    
    
//            outputStream = new ObjectOutputStream(new FileOutputStream(new File("user.txt")));
            outputStream = new ObjectOutputStream(byteArrayOutputStream);
            outputStream.writeObject(t);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }finally {
    
    
            FileUtil.closeO(outputStream);
        }
        return byteArrayOutputStream.toByteArray();
    }

    @Override
    public <T> T write(byte[] bytes, Class<T> tClass,String name) {
    
    
        T t = null;
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        try {
    
    
//            ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(new File(name)));
            ObjectInputStream inputStream = new ObjectInputStream(byteArrayInputStream);
            t = (T)inputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
    
    
            e.printStackTrace();
        }
        return t;
    }


}

json serialization

package com.test.thread.newlw.net;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * @author 25338
 * @version 1.0
 * @date 2021/12/28 16:04
 */
public class FastJsonSericial implements IService {
    
    

    static {
    
    
        //配置--transient可以序列化
        JSON.DEFAULT_GENERATE_FEATURE = SerializerFeature.config(
                JSON.DEFAULT_GENERATE_FEATURE, SerializerFeature.SkipTransientField,false
        );
    }

    @Override
    public <T> byte[] read(T t) {
    
    
        return JSON.toJSONString(t).getBytes();
    }

    @Override
    public <T> T write(byte[] bytes, Class<T> tClass, String name) {
    
    
        return JSON.parseObject(new String(bytes),tClass);
    }
}

3.xstream serialization

package com.test.thread.newlw.net;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author 25338
 * @version 1.0
 * @date 2021/12/28 15:51
 * @description xs序列化
 */
public class XStreamSericial implements IService {
    
    

    private XStream xStream = new XStream(new DomDriver());

    {
    
    
        //安全机制
        xStream.allowTypesByRegExp(new String[] {
    
     ".*" });
    }
    @Override
    public <T> byte[] read(T t) {
    
    
        return xStream.toXML(t).getBytes();
    }

    @Override
    public <T> T write(byte[] bytes, Class<T> tClass, String name) {
    
    
        return (T)xStream.fromXML(new String(bytes));
    }
}

4.hessian serialization

package com.test.thread.newlw.net;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author 25338
 * @version 1.0
 * @date 2021/12/28 17:24
 */
public class HessionSericial implements IService {
    
    

    @Override
    public <T> byte[] read(T t) {
    
    
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        HessianOutput output = new HessianOutput(outputStream);
        try {
    
    
            output.writeObject(t);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        return outputStream.toByteArray();
    }

    @Override
    public <T> T write(byte[] bytes, Class<T> tClass, String name) {
    
    
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        HessianInput input = new HessianInput(inputStream);
        try {
    
    
            return (T)input.readObject();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        return null;
    }
}

5. Test class call - using a simple factory

package com.test.thread.newlw.net;

/**
 * @author 25338
 * @version 1.0
 * @date 2021/12/28 17:22
 */
public class SericialFactory {
    
    


    public static IService instance(Class z){
    
    
        if(z == null){
    
    return null;}
        IService o = null;
        try {
    
    
            o = (IService)z.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
    
    
            e.printStackTrace();
        }
        return o;
    }
}

6. Test class

package com.test.thread.newlw.net;


/**
 * @author 25338
 * @version 1.0
 * @date 2021/12/28 15:54
 */
public class TestSericial {
    
    

    public static void main(String[] args) {
    
    
        //测试--根据需要的class传进去--即可实例化不同对象HessionSericial等
        IService service = SericialFactory.instance(HessionSericial.class);
        User u = new User("小明",25);
        //序列化
        byte[] read = service.read(u);
        System.out.println(new String(read) + ":::" + read.length);
        //反序列化
        User write = service.write(read, User.class, null);
        System.out.println(write.toString());
    }
}

Guess you like

Origin blog.csdn.net/weixin_43795840/article/details/122203637