Gsonã§JSONæååãGenericsãªãªãã¸ã§ã¯ãã«å¤æããã¨ãã«ããã
Gsonã®Gã¯ã¬ã³ãã ã®ãGãã§ãï¼é
Javaã§JSONãæ±ãã¨ãã«ãã¤ããä¸è©±ã«ãªã£ã¦ããã¾ãã
ã§ãã¿ã¤ãã«éããããã£ãã®ã§ã¡ã¢ã
å¤æ対象ã®ã¯ã©ã¹
ãããªæãã§å ±éé¨ä»¥å¤ãã¸ã§ããªã¯ã¹ã§ãã£ã¡ãããçãªã¤ã¡ã¼ã¸ã
class Hoge<T> { String name; int id; T value; @Override public String toString() { return "Hoge{" + "name='" + name + '\'' + ", id=" + id + ", value=" + value + '}'; } } class Fuga { String name; int id; public Fuga(String name, int id) { this.name = name; this.id = id; } @Override public String toString() { return "Fuga{" + "name='" + name + '\'' + ", id=" + id + '}'; } }
Genericsãªãªãã¸ã§ã¯ãããJSONã¸ã®å¤æ
JSONã¸ã®å¤æã¯ãããªæãã§ä½ãèããã«è¡ãã
import com.google.gson.Gson; public class GsonTest { public static void main( String ... args){ Hoge<Fuga> hoge = new Hoge<Fuga>(); hoge.name = "aaaa"; hoge.id = 3; Fuga fuga = new Fuga("bbbb",1000); hoge.value = fuga; System.out.println(String.format("Object->[%s]", hoge)); } }
å®è¡ããã¨ãããªãã
Object->[Hoge{name='aaaa', id=3, value=Fuga{name='bbbb', id=1000}}] JSON->[{"name":"aaaa","id":3,"value":{"name":"bbbb","id":1000}}]
JSONãããªãã¸ã§ã¯ãã¸ã®å¤æ
ãGson User Guideãã«ããæ¹ãæ¸ãã¦ããã
親åã ã
ãµã³ãã«ã©ããã®æé
import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonTest { public static void main( String ... args){ // ä¸ã§ä½ã£ãjsonããªãã¸ã§ã¯ãã«å¤æ hoge = decode(json); System.out.println(String.format("Object(Hoge)->[%s]", hoge)); fuga = hoge.value; System.out.println(String.format("Object(Fuga)->[%s]", fuga)); } private static Hoge<Fuga> decode( String json ){ Gson gson = new Gson(); Type type = new TypeToken<Hoge<Fuga>>(){}.getType(); System.out.println(type.getTypeName()); return gson.fromJson(json, type); } }
å®è¡ããã¨ãããªãã
Main$Hoge<Main$Fuga> Object(Hoge)->[Hoge{name='aaaa', id=3, value=Fuga{name='bbbb', id=1000}}] Object(Fuga)->[Fuga{name='bbbb', id=1000}]
æ±ç¨åãã¦ã¿ã
å®éã¯Hoge
import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonTest { public static void main( String ... args){ // ä¸ã§ä½ã£ãjsonããªãã¸ã§ã¯ãã«å¤æ hoge = decode(json, Fuga.class); System.out.println(String.format("Object(Hoge)->[%s]", hoge)); fuga = hoge.value; System.out.println(String.format("Object(Fuga)->[%s]", fuga)); } private static <T> Hoge<T> decode( String json, Class<T> tClass ) { Gson gson = new Gson(); Type type = new TypeToken<Hoge<T>>(){}.getType(); System.out.println(type.getTypeName()); return gson.fromJson(json, type); } }
å®è¡ããã¨ãããªãã
Main$Hoge<T> Object(Hoge)->[Hoge{name='aaaa', id=3, value={name=bbbb, id=1000.0}}] Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to Main$Fuga at Main.main(Main.java:xx)
Gsonã®fromJsonã®çµæã¯Hogeã¯ã©ã¹ã¾ã§ã¯ãã³ã¼ãã§ãã¦ãããã ãã©ãvalueãFugaã¯ã©ã¹ã§ã¯ãªãã¦ãcom.google.gson.internal.LinkedTreeMapãã«ãªã£ã¦ããã
å°ã£ãâ¦
ã¨ãããã¨ã§ãStackOverFlowããåºçªã§ãã
Once your .java source file is compiled, the type parameter
is obviously thrown away. Since it is not known at compile time, it cannot be stored in bytecode, so it's erased and Gson can't read it.
è¦ããã«
"ï¼Tï¼"ã¯ãã«ãæã«æ¶ããã®ã§åçã«ã¯è¦ããããã¼ã«ãã¼ã«ãã ãã親åå¿ã§LinkedTreeMapã«ãã¨ãã¦ãã£ããã¼ã
ã£ã¦ãã¨ãããã
ã¨ãããã¨ã§ä¸è¨ã®åçã«ãã£ããããªãParameterizedTypeãå®è£
ããã¸ã§ããªã¯ã¹ã®æ
å ±ãä¿æããã¯ã©ã¹ãä½ãã°ããã
class GenericOf<X, Y> implements ParameterizedType { private final Class<X> container; private final Class<Y> wrapped; public GenericOf(Class<X> container, Class<Y> wrapped) { this.container = container; this.wrapped = wrapped; } @Override public Type[] getActualTypeArguments() { return new Type[]{wrapped}; } @Override public Type getRawType() { return container; } @Override public Type getOwnerType() { return null; } }
import com.google.gson.Gson; import java.lang.reflect.ParameterizedType; public class GsonTest { public static void main( String ... args){ // ä¸ã§ä½ã£ãjsonããªãã¸ã§ã¯ãã«å¤æ hoge = decode(json, Fuga.class); System.out.println(String.format("Object(Hoge)->[%s]", hoge)); fuga = hoge.value; System.out.println(String.format("Object(Fuga)->[%s]", fuga)); } private static <T> Hoge<T> decode( String json, Class<T> tClass ) { Gson gson = new Gson(); ParameterizedType type = new GenericOf(Hoge.class,tClass); System.out.println(type.getRawType()); System.out.println(type.getActualTypeArguments()[0]); return gson.fromJson(json, type); } }
å®è¡ããã¨ãããªãã
class Main$Hoge class Main$Fuga Object(Hoge)->[Hoge{name='aaaa', id=3, value=Fuga{name='bbbb', id=1000}}] Object(Fuga)->[Fuga{name='bbbb', id=1000}]
CYBORGããã¡ããG 1 (éè±ç¤¾æ庫 ã 55-4)
- ä½è : å°çå¥
- åºç社/ã¡ã¼ã«ã¼: éè±ç¤¾
- çºå£²æ¥: 2011/08/18
- ã¡ãã£ã¢: æ庫
- ã¯ãªãã¯: 7å
- ãã®ååãå«ãããã°ãè¦ã
Rubie'sã«ã¼ãã¼ãº 大人ç¨ã¸ã§ã¼ã½ã³ ããã¦ã£ã³ ã³ã¹ãã¬ è¡£è£ ãã£ãºãã¼
- åºç社/ã¡ã¼ã«ã¼: RUBIE'S JAPAN
- ã¡ãã£ã¢:
- ãã®ååãå«ãããã°ãè¦ã
ï¼ï¼æ¥ã®éææ¥ãï¼ã¤ã³ãã¢ã¯ã·ã§ã³ãã¼ã«ãã¸ã§ã¤ã½ã³
- åºç社/ã¡ã¼ã«ã¼: NECA
- ã¡ãã£ã¢:
- ãã®ååãå«ãããã°ãè¦ã