검색결과 리스트
Gson 공부에 해당되는 글 3건
- 2014.02.11 Gson의 재밌는 특징
- 2014.02.06 Gson을 이용하여 static variable을 serialize하기
- 2014.02.06 Gson generic type을 deserialize하기
글
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[]으로 받으면 되므로 편리한것같다.
글
Gson을 이용하여 static variable을 serialize하기
Gson 공부
2014. 2. 6. 16:23
Gson은 class의 static field를 transient하게 기본적으로 취급하는것 같다. 이 때문에 gson을 통해서 직렬화를 해도 값이 나타지가 않는다. 이를 해결하기 위해서 GsonBuilder의 설정을 이용하면 해결되는것을 볼 수 있다.
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package pkg1; import java.lang.reflect.Type; import com.google.gson.*; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Modifier; import java.util.*; class Bar{ static int account = 5; Collection<Integer> collection = new ArrayList<Integer>(); Bar(){ collection.add(5); collection.add(6); } } public class MytestClass { public static void main(String[] args) { Bar _bar = new Bar(); Gson gson_bar = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create(); String json = gson_bar.toJson(_bar,_bar.getClass()); System.out.println("_Bar: "+json); Bar _bar_2 = new Bar(); _bar_2 = gson_bar.fromJson(json, Bar.class); Iterator<Integer> iterator = _bar_2.collection.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } System.out.println(_bar_2.collection.size() ); } } |
map도 잘됨
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | package pkg1; import java.lang.reflect.Type; import com.google.gson.*; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Modifier; import java.util.*; class Foo<T>{ T value; void setValue(T value){ this.value = value; } } class Bar{ static int account = 5; Collection<Integer> collection = new ArrayList<Integer>(); static Map<Integer, String> map = new HashMap<Integer,String>(); Bar(){ collection.add(5); collection.add(6); map.put(100, "aaa"); map.put(200, "bbb"); } } public class MytestClass { public static void main(String[] args) { Gson gson = new Gson(); Foo<Integer> foo = new Foo<Integer>(); foo.setValue(5); Type fooType = new TypeToken<Foo<Integer>>(){}.getType(); String json = gson.toJson(foo,fooType); System.out.println(json); Foo<Integer> bar = new Foo<Integer>(); bar = gson.fromJson(json, fooType ); System.out.println("bar: "+(int)bar.value); Bar _bar = new Bar(); Gson gson_bar = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create(); json = gson_bar.toJson(_bar,_bar.getClass()); System.out.println("_Bar: "+json); Bar _bar_2 = new Bar(); _bar_2 = gson_bar.fromJson(json, Bar.class); Iterator<Integer> iterator = _bar_2.collection.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } iterator = _bar_2.map.keySet().iterator(); while(iterator.hasNext()){ System.out.println(_bar_2.map.get((Integer)iterator.next())); } System.out.println(_bar_2.collection.size() ); } } |
글
Gson generic type을 deserialize하기
Gson 공부
2014. 2. 6. 15:13
Gson을 공부하는데 generic type의 클래스를 Deserialize하는데 문제가 생겼다.
https://sites.google.com/site/gson/gson-user-guide
에 보면 해결법이 있는데, Type class를 import해야하는데
어떤걸 import해야하는지몰라서 해멨었다.
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 | package pkg1; import java.lang.reflect.Type; import com.google.gson.*; import com.google.gson.reflect.*; class Foo<T>{ T value; void setValue(T value){ this.value = value; } } public class MytestClass { public static void main(String[] args) { Gson gson = new Gson(); Foo<Integer> foo = new Foo<Integer>(); foo.setValue(5); Type fooType = new TypeToken<Foo<Integer>>(){}.getType(); String json = gson.toJson(foo,fooType); System.out.println(json); Foo<Integer> bar = new Foo<Integer>(); bar = gson.fromJson(json, fooType ); System.out.println("bar: "+(int)bar.value); } } |