Created
October 7, 2012 14:59
-
-
Save aya-eiya/3848596 to your computer and use it in GitHub Desktop.
GSONでは抽象クラスを使うときには注意が必要 ref: http://qiita.com/items/ca97b4b8befcd43a874e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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