Skip to content

Instantly share code, notes, and snippets.

@yutax77
Created June 25, 2012 11:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yutax77/2988072 to your computer and use it in GitHub Desktop.
Save yutax77/2988072 to your computer and use it in GitHub Desktop.
Example Flexjson's TypeLocator.
public class Circle implements Figure {
private Type type;
private int radius;
public Circle(){
type = Type.CIRCLE;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public String say() {
return "r=" + radius;
}
public String toJson(){
return new JSONSerializer().exclude("*.class")
.serialize(this);
}
}
public interface Figure {
public String say();
public enum Type{
CIRCLE,
RECTANGLE;
}
}
import flexjson.JSONDeserializer;
import flexjson.locators.TypeLocator;
public class Figures {
private Figures(){}
public static Figure fromJson(String json){
TypeLocator<String> loc = new TypeLocator<String>("type");
loc.add(Type.CIRCLE.toString(), Circle.class);
loc.add(Type.RECTANGLE.toString(), Rectangle.class);
return new JSONDeserializer<Figure>()
.use(null, loc)
.deserialize(json);
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class FiguresTest {
@Test
public void testCircleToJson(){
Circle circle = new Circle();
circle.setRadius(5);
String expected = "{\"radius\":5,\"type\":\"CIRCLE\"}";
assertEquals(expected, circle.toJson());
}
@Test
public void testRectangleToJson(){
Rectangle rectangle = new Rectangle();
rectangle.setX(5);
rectangle.setY(10);
String expected = "{\"type\":\"RECTANGLE\",\"x\":5,\"y\":10}";
assertEquals(expected, rectangle.toJson());
}
@Test
public void testCircleFromJson() {
String circleJson = "{\"radius\":5,\"type\":\"CIRCLE\"}";
Figure actual = Figures.fromJson(circleJson);
assertTrue(actual instanceof Circle);
assertEquals("r=5", actual.say());
}
@Test
public void testRectangleFromJson() {
String circleJson = "{\"x\":5,\"y\":10,\"type\":\"RECTANGLE\"}";
Figure actual = Figures.fromJson(circleJson);
assertTrue(actual instanceof Rectangle);
assertEquals("x=5, y=10", actual.say());
}
}
public class Rectangle implements Figure {
private Type type;
private int x;
private int y;
public Rectangle(){
type = Type.RECTANGLE;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String say() {
return "x=" + x + ", y=" + y;
}
public String toJson(){
return new JSONSerializer().exclude("*.class")
.serialize(this);
}
}
@huangered
Copy link

Good code, help me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment