글
Gson의 재밌는 특징
Gson 공부
2014. 2. 11. 11:10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package pkg1; import java.util.ArrayList; import com.google.gson.Gson; public class Mytestjava_bar { public static void main(String[] args) { Gson gson = new Gson(); ArrayList<Unit> mylist = new ArrayList<Unit>(); mylist.add(new Unit("Fucker1",100,100,100)); mylist.add(new Unit("Fucker2",100,100,100)); // String json = gson.toJson(mylist); // System.out.println(json); // // Unit[] unit = gson.fromJson(json, Unit[].class); // System.out.println(unit[0].att); Unit[] units = new Unit[2]; units[0] = new Unit("Fucker1",100,100,100); units[1] = new Unit("Fucker2",100,200,300); json = gson.toJson(units); System.out.println(json); } } |
Gson의 재미있는특징인것 같다.
ArrayList<Unit>의 serialization 결과나 Unit[]의 Serialization 결과나 동일하다.
따라서 Sever에서 ArrayList<Unit>을 Gson을 이용하여 Client에 보낼때,
Client는 동일하게 ArrayList<Unit>으로 deserialization하는게 하니라 Unit[]으로 받으면 되므로 편리한것같다.