/src/main/java/com/alibaba/fastjson/parser/deserializer/RectangleDeserializer.java
Java | 81 lines | 66 code | 15 blank | 0 comment | 25 complexity | 58bcc397c4ed38cfe01c7b9c63421427 MD5 | raw file
- package com.alibaba.fastjson.parser.deserializer;
- import java.awt.Rectangle;
- import java.lang.reflect.Type;
- import java.util.Collections;
- import java.util.Set;
- import com.alibaba.fastjson.JSONException;
- import com.alibaba.fastjson.parser.DefaultJSONParser;
- import com.alibaba.fastjson.parser.JSONScanner;
- import com.alibaba.fastjson.parser.JSONToken;
- public class RectangleDeserializer implements AutowiredObjectDeserializer {
- public final static RectangleDeserializer instance = new RectangleDeserializer();
- @SuppressWarnings("unchecked")
- public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
- JSONScanner lexer = (JSONScanner) parser.getLexer();
-
- if (lexer.token() == JSONToken.NULL) {
- lexer.nextToken();
- return null;
- }
- if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
- throw new JSONException("syntax error");
- }
- lexer.nextToken();
- int x = 0, y = 0, width = 0, height = 0;
- for (;;) {
- if (lexer.token() == JSONToken.RBRACE) {
- lexer.nextToken();
- break;
- }
- String key;
- if (lexer.token() == JSONToken.LITERAL_STRING) {
- key = lexer.stringVal();
- lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
- } else {
- throw new JSONException("syntax error");
- }
- int val;
- if (lexer.token() == JSONToken.LITERAL_INT) {
- val = lexer.intValue();
- lexer.nextToken();
- } else {
- throw new JSONException("syntax error");
- }
- if (key.equalsIgnoreCase("x")) {
- x = val;
- } else if (key.equalsIgnoreCase("y")) {
- y = val;
- } else if (key.equalsIgnoreCase("width")) {
- width = val;
- } else if (key.equalsIgnoreCase("height")) {
- height = val;
- } else {
- throw new JSONException("syntax error, " + key);
- }
- if (lexer.token() == JSONToken.COMMA) {
- lexer.nextToken(JSONToken.LITERAL_STRING);
- }
- }
- return (T) new Rectangle(x, y, width, height);
- }
- public int getFastMatchToken() {
- return JSONToken.LBRACE;
- }
-
- public Set<Type> getAutowiredFor() {
- return Collections.<Type>singleton(Rectangle.class);
- }
- }