Created
June 24, 2012 01:30
-
-
Save yutax77/2980912 to your computer and use it in GitHub Desktop.
The class which has Enum variable.
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 flexjson.JSONDeserializer; | |
import flexjson.JSONSerializer; | |
public class Hoge { | |
private Color color; | |
public Hoge(){} | |
public Color getColor() { | |
return color; | |
} | |
public void setColor(Color color) { | |
this.color = color; | |
} | |
public String toJson(){ | |
return new JSONSerializer().exclude("*.class") | |
.serialize(this); | |
} | |
public static Hoge fromJson(String json){ | |
return new JSONDeserializer<Hoge>() | |
.use(null, Hoge.class) | |
.deserialize(json); | |
} | |
public enum Color { | |
RED, | |
BLUE, | |
GREEN; | |
} | |
} |
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 static org.junit.Assert.assertEquals; | |
import jp.co.pooh.FlexJsonTest.Hoge.Color; | |
import org.junit.Test; | |
public class HogeTest { | |
@Test | |
public void testToJson() { | |
Hoge hoge = new Hoge(); | |
hoge.setColor(Color.BLUE); | |
String json = hoge.toJson(); | |
assertEquals("{\"color\":\"BLUE\"}", json); | |
} | |
@Test | |
public void testFromJson() { | |
String json = "{\"color\":\"BLUE\"}"; | |
Hoge created = Hoge.fromJson(json); | |
assertEquals(Color.BLUE, created.getColor()); | |
} | |
@Test(expected=IllegalArgumentException.class) | |
public void testFailFromJsonByIllegalEnumId() { | |
String json = "{\"color\":\"blue\"}";//lowercase!! | |
Hoge.fromJson(json); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment