Skip to content

Instantly share code, notes, and snippets.

@aya-eiya
Created October 7, 2012 14:59
Show Gist options
  • Save aya-eiya/3848596 to your computer and use it in GitHub Desktop.
Save aya-eiya/3848596 to your computer and use it in GitHub Desktop.
GSONでは抽象クラスを使うときには注意が必要 ref: http://qiita.com/items/ca97b4b8befcd43a874e
package jp.eiya.aya.gson
public abstract class MyClass{
private String id=null
private int value=0
public MyClass(String i,int v){
id=i
value=v
}
public String getID(){return id}
public int getValue(){return value}
}
import com.google.gson.*
import jp.eiya.aya.gson.MyClass
def gson = new Gson()
def test=new MyClass("test" , 1){} // 匿名クラスをインスタンス化
def json=gson.toJson(test) // jsonはnullになる
assert(json == /{"id":"test","value":1}/) // 失敗
MyClass test2 = gson.fromJson(json,MyClass.class) // test2はnullになる
assert(test2.getID() == "test") // ぬるぽ
assert(test2.getValue() == 1) // ぬるぽ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment