Created
June 25, 2012 11:26
-
-
Save yutax77/2988072 to your computer and use it in GitHub Desktop.
Example Flexjson's TypeLocator.
This file contains 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
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); | |
} | |
} |
This file contains 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
public interface Figure { | |
public String say(); | |
public enum Type{ | |
CIRCLE, | |
RECTANGLE; | |
} | |
} |
This file contains 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.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); | |
} | |
} |
This file contains 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 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()); | |
} | |
} |
This file contains 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
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good code, help me a lot.