Fastjson JSONArray深度复制

Fastjson JSONArray深度复制

目录

Fastjson JSONArray深度复制

1,new JSONArray (Object);

2,AddAll;

3,串行化复制;

总结:


如何对fajstjson JSONArray类型的数据进行深度复制,有几种方式呢?

 

1,new JSONArray (Object);

代码:

public static void main(String[] args) throws IOException, ClassNotFoundException {
    testNewJsonArr();
}

public static void testNewJsonArr(){
    System.out.println("====== new JSONArray fail =======");
    JSONArray oneArr = new JSONArray();
    JSONObject one = new JSONObject();
    one.put("one","fish");
    oneArr.add(one);
    JSONArray twoArr = new JSONArray(oneArr);
    System.out.println("init: oneArr: "+ oneArr.toString());
    System.out.println("init: twoArr: "+ twoArr.toString());
    System.out.println("====== add bird =======");
    JSONObject two = new JSONObject();
    two.put("two","bird");
    twoArr.add(two);
    System.out.println("change: oneArr: "+ oneArr.toString());
    System.out.println("change: twoArr: "+ twoArr.toString());
    System.out.println("====== fail =======");
}

        

结果:

====== new JSONArray fail =======

init: oneArr: [{"one":"fish"}]

init: twoArr: [{"one":"fish"}]

====== add bird =======

change: oneArr: [{"one":"fish"},{"two":"bird"}]

change: twoArr: [{"one":"fish"},{"two":"bird"}]

====== fail =======

new JSONArray(Object) 这种方式失败了,详细原因看 new JSONArray(Object)深复制失败的原因

2,AddAll;

代码:

public static void main(String[] args) throws IOException, ClassNotFoundException {
    testAddAllJsonArr();
}

public static void testAddAllJsonArr(){
    System.out.println("====== put all success =======");
    JSONArray oneArr = new JSONArray();
    JSONObject one = new JSONObject();
    one.put("one","fish");
    oneArr.add(one);
    JSONArray twoArr = new JSONArray();
    twoArr.addAll(oneArr);
    System.out.println("init: oneArr: "+ oneArr.toString());
    System.out.println("init: twoArr: "+ twoArr.toString());
    System.out.println("====== add bird =======");
    JSONObject two = new JSONObject();
    two.put("two","bird");
    twoArr.add(two);
    System.out.println("change: oneArr: "+ oneArr.toString());
    System.out.println("change: twoArr: "+ twoArr.toString());
    System.out.println();
}

结果:

====== put all success =======

init: oneArr: [{"one":"fish"}]

init: twoArr: [{"one":"fish"}]

====== add bird =======

change: oneArr: [{"one":"fish"}]

change: twoArr: [{"one":"fish"},{"two":"bird"}]

allAll的方式行得通

 

3,串行化复制;

代码:

public static void main(String[] args) throws IOException, ClassNotFoundException {
    testDeppCopyJsonArr();
}

public static void testDeppCopyJsonArr() throws IOException, ClassNotFoundException {
    System.out.println("====== serializable success =======");
    JSONArray oneArr = new JSONArray();
    JSONObject one = new JSONObject();
    one.put("one","fish");
    oneArr.add(one);
    JSONArray twoArr = CloneUtils.clone(oneArr);;
    System.out.println("init: oneArr: "+ oneArr.toString());
    System.out.println("init: twoArr: "+ twoArr.toString());
    System.out.println("====== add bird =======");
    JSONObject two = new JSONObject();
    two.put("two","bird");
    twoArr.add(two);
    System.out.println("change: oneArr: "+ oneArr.toString());
    System.out.println("change: twoArr: "+ twoArr.toString());
    System.out.println();
}

 

串行化代码:

public class CloneUtils {

    public static  T clone(T src) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = getInputStream(src);
        //返回生成的新对象
        @SuppressWarnings("unchecked")
        T cloneObj = (T) ois.readObject();
        ois.close();
        return cloneObj;

    }

    public static  List deepCopy(List src) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = getInputStream(src);
        @SuppressWarnings("unchecked")
        List dest = (List) ois.readObject();
        ois.close();
        return dest;
    }
 
    public static  Map cloneMap(Map src) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = getInputStream(src);
        @SuppressWarnings("unchecked")
        Map result =   (Map)     ois.readObject();
        ois.close();
        return result;

    }

    private static ObjectInputStream getInputStream(Object src) throws IOException, ClassNotFoundException {
        //写入字节流
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream obs = new ObjectOutputStream(out);
        obs.writeObject(src);
        obs.close();
        //分配内存,写入原始对象,生成新对象
        ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
        return new ObjectInputStream(ios);
    }
}

 

结果:

====== serializable success =======

init: oneArr: [{"one":"fish"}]

init: twoArr: [{"one":"fish"}]

====== add bird =======

change: oneArr: [{"one":"fish"}]

change: twoArr: [{"one":"fish"},{"two":"bird"}]

 

总结:

     在使用fastjon JSONArray复制的时候,一般用第二种addAll的方式,如果大量使用,也可以考虑用第三种方式。

 

 

你可能感兴趣的:(java,java,fastjson,JSONArray,深度复制)