Skip to content

Instantly share code, notes, and snippets.

@yutax77
Created June 24, 2012 01:30
Show Gist options
  • Save yutax77/2980912 to your computer and use it in GitHub Desktop.
Save yutax77/2980912 to your computer and use it in GitHub Desktop.
The class which has Enum variable.
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;
}
}
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