# Java基础\_对象正反序列化

![img](https://3937520527-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lz_ry1cyy2sKkwvpUnh%2Fuploads%2Fgit-blob-28ff57735c830d0cdf0fab53746c808fdcba1bbf%2F%E5%BC%82%E4%B8%96%E7%95%8C%E8%95%BE%E5%A7%86_0.jpg?alt=media)

\[TOC]

***

## 对象正反序列化

```java
import java.io.*;
import java.util.Base64;

/**
 * 对象编码工具类
 */
public class ObjEncodeUtil {
    public static String encodeBase64(Object object) {
        if (object == null) {
            return null;
        }
        ObjectOutputStream oos = null;
        String result;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(object);
            result = Base64.getEncoder().encodeToString(bos.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static <T> T decodeBase64(String str) {
        ObjectInputStream ois = null;
        T result;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(str));
            ois = new ObjectInputStream(bis);
            result = (T) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}

class Bean{
        int length;
        double width;

        public Bean() {
        }

        public Bean(int length, double width) {
            this.length = length;
            this.width = width;
        }

        public int getLength() {
            return length;
        }

        public void setLength(int length) {
            this.length = length;
        }

        public double getWidth() {
            return width;
        }

        public void setWidth(double width) {
            this.width = width;
        }
    }

Bean bean = new Bean(5,3.2);
String modelStr = ObjEncodeUtil.encodeBase64(bean);
Bean bean2 = ObjEncodeUtil.decodeBase64(modelStr);
```

```java
import com.alibaba.fastjson.JSON;
ModelBean bean = new ModelBean();

String jsonResult = JSON.toJSONString(bean);
ModelBean bean2 = JSON.parseObject(jsonResult, ModelBean.class);
```
