Fastjson JSONObject深度复制
目录
1,new JSONObject (Object) - 失败
如何对fajstjson JSONObject类型的数据进行深度复制,有几种方式呢?
1,new JSONObject (Object) - 失败
代码:
public static void main(String[] args) throws IOException, ClassNotFoundException {
testNewJson();
}
public static void testNewJson(){
System.out.println("====== new JSONObject fail =======");
JSONObject one = new JSONObject();
one.put("one","fish");
JSONObject two = new JSONObject(one);
System.out.println("init: one: "+ one.toString());
System.out.println("init: two: "+ two.toString());
System.out.println("====== add grass =======");
two.put("two","grass");
System.out.println("change: one: "+ one.toString());
System.out.println("change: two: "+ two.toString());
System.out.println("====== new JSONObject fail =======");
}
结果:
====== new JSONObject fail =======
init: one: {"one":"fish"}
init: two: {"one":"fish"}
====== add grass =======
change: one: {"one":"fish","two":"grass"}
change: two: {"one":"fish","two":"grass"}
====== new JSONObject fail =======
new JSONObject(one) 这种方式失败了 new JSONObject(Object)深复制失败的原因
2,Put All;
代码:
public static void main(String[] args) throws IOException, ClassNotFoundException {
testPutAllJson();
}
public static void testPutAllJson(){
System.out.println("====== put all =======");
JSONObject one = new JSONObject();
one.put("one","fish");
JSONObject two = new JSONObject();
two.putAll(one);
System.out.println("init: one: "+ one.toString());
System.out.println("init: two: "+ two.toString());
System.out.println("====== add fox =======");
two.put("two","fox");
System.out.println("change: one: "+ one.toString());
System.out.println("change: two: "+ two.toString());
System.out.println();
}
结果:
====== put all =======
init: one: {"one":"fish"}
init: two: {"one":"fish"}
====== add fox =======
change: one: {"one":"fish"}
change: two: {"one":"fish","two":"fox"}
putAll的方式行得通
3,串行化复制;
代码:
public static void main(String[] args) throws IOException, ClassNotFoundException {
testDeppCopyJson();
}
public static void testDeppCopyJson() throws IOException, ClassNotFoundException {
System.out.println("====== serializable success =======");
JSONObject one = new JSONObject();
one.put("one","fish");
JSONObject two = CloneUtils.clone(one);
two.putAll(one);
System.out.println("init: one: "+ one.toString());
System.out.println("init: two: "+ two.toString());
System.out.println("====== add wolf =======");
two.put("two","wolf");
System.out.println("change: one: "+ one.toString());
System.out.println("change: two: "+ two.toString());
System.out.println();
}
串行化代码:
public class CloneUtils {
public static <T extends Serializable> T clone(T src) throws IOException, ClassNotFoundException {
ObjectInputStream ois = getInputStream(src);
//返回生成的新对象
@SuppressWarnings("unchecked")
T cloneObj = (T) ois.readObject();
ois.close();
return cloneObj;
}
public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
ObjectInputStream ois = getInputStream(src);
@SuppressWarnings("unchecked")
List<T> dest = (List<T>) ois.readObject();
ois.close();
return dest;
}
public static <K,V> Map<K,V> cloneMap(Map<K,V> src) throws IOException, ClassNotFoundException {
ObjectInputStream ois = getInputStream(src);
@SuppressWarnings("unchecked")
Map<K, V> result = (Map<K, V>) 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: one: {"one":"fish"}
init: two: {"one":"fish"}
====== add wolf =======
change: one: {"one":"fish"}
change: two: {"one":"fish","two":"wolf"}
总结:
在使用fastjon JSONObject复制的时候,一般用第二种putAll的方式,如果大量使用,也可以考虑用第三种方式。