Skip to content

Commit

Permalink
refactor for footprint
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Mar 17, 2016
1 parent 9fe1d21 commit a49b57b
Show file tree
Hide file tree
Showing 20 changed files with 384 additions and 475 deletions.
13 changes: 5 additions & 8 deletions src/main/java/com/alibaba/fastjson/parser/ParserConfig.java
Expand Up @@ -93,6 +93,7 @@
import com.alibaba.fastjson.parser.deserializer.ThrowableDeserializer;
import com.alibaba.fastjson.parser.deserializer.TimeDeserializer;
import com.alibaba.fastjson.serializer.AtomicCodec;
import com.alibaba.fastjson.serializer.AwtCodec;
import com.alibaba.fastjson.serializer.BigDecimalCodec;
import com.alibaba.fastjson.serializer.BigIntegerCodec;
import com.alibaba.fastjson.serializer.BooleanCodec;
Expand All @@ -102,22 +103,18 @@
import com.alibaba.fastjson.serializer.CharsetCodec;
import com.alibaba.fastjson.serializer.ClassCodec;
import com.alibaba.fastjson.serializer.CollectionCodec;
import com.alibaba.fastjson.serializer.ColorCodec;
import com.alibaba.fastjson.serializer.CurrencyCodec;
import com.alibaba.fastjson.serializer.DateCodec;
import com.alibaba.fastjson.serializer.DateFormatCodec;
import com.alibaba.fastjson.serializer.FileCodec;
import com.alibaba.fastjson.serializer.FloatCodec;
import com.alibaba.fastjson.serializer.FontCodec;
import com.alibaba.fastjson.serializer.InetAddressCodec;
import com.alibaba.fastjson.serializer.InetSocketAddressCodec;
import com.alibaba.fastjson.serializer.IntegerCodec;
import com.alibaba.fastjson.serializer.LongCodec;
import com.alibaba.fastjson.serializer.MapCodec;
import com.alibaba.fastjson.serializer.MiscCodec;
import com.alibaba.fastjson.serializer.ObjectArrayCodec;
import com.alibaba.fastjson.serializer.PointCodec;
import com.alibaba.fastjson.serializer.RectangleCodec;
import com.alibaba.fastjson.serializer.ReferenceCodec;
import com.alibaba.fastjson.serializer.StringCodec;
import com.alibaba.fastjson.serializer.TimeZoneCodec;
Expand Down Expand Up @@ -300,10 +297,10 @@ private ParserConfig(ASMDeserializerFactory asmFactory, ClassLoader parentClassL

if (!awtError) {
try {
derializers.put(Class.forName("java.awt.Point"), PointCodec.instance);
derializers.put(Class.forName("java.awt.Font"), FontCodec.instance);
derializers.put(Class.forName("java.awt.Rectangle"), RectangleCodec.instance);
derializers.put(Class.forName("java.awt.Color"), ColorCodec.instance);
derializers.put(Class.forName("java.awt.Point"), AwtCodec.instance);
derializers.put(Class.forName("java.awt.Font"), AwtCodec.instance);
derializers.put(Class.forName("java.awt.Rectangle"), AwtCodec.instance);
derializers.put(Class.forName("java.awt.Color"), AwtCodec.instance);
} catch (Throwable e) {
// skip
awtError = true;
Expand Down
315 changes: 315 additions & 0 deletions src/main/java/com/alibaba/fastjson/serializer/AwtCodec.java
@@ -0,0 +1,315 @@
package com.alibaba.fastjson.serializer;

import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.IOException;
import java.lang.reflect.Type;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;

public class AwtCodec implements ObjectSerializer, ObjectDeserializer {

public final static AwtCodec instance = new AwtCodec();

public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
int features) throws IOException {
SerializeWriter out = serializer.getWriter();

if (object == null) {
out.writeNull();
return;
}

char sep = '{';

if (object instanceof Point) {
Point font = (Point) object;

sep = writeClassName(out, Point.class, sep);

out.writeFieldValue(sep, "x", font.getX());
out.writeFieldValue(',', "y", font.getY());
} else if (object instanceof Font) {
Font font = (Font) object;

sep = writeClassName(out, Font.class, sep);

out.writeFieldValue(sep, "name", font.getName());
out.writeFieldValue(',', "style", font.getStyle());
out.writeFieldValue(',', "size", font.getSize());
} else if (object instanceof Rectangle) {
Rectangle rectangle = (Rectangle) object;

sep = writeClassName(out, Rectangle.class, sep);

out.writeFieldValue(sep, "x", rectangle.getX());
out.writeFieldValue(',', "y", rectangle.getY());
out.writeFieldValue(',', "width", rectangle.getWidth());
out.writeFieldValue(',', "height", rectangle.getHeight());
} else if (object instanceof Color) {
Color color = (Color) object;

sep = writeClassName(out, Color.class, sep);

out.writeFieldValue(sep, "r", color.getRed());
out.writeFieldValue(',', "g", color.getGreen());
out.writeFieldValue(',', "b", color.getBlue());
if (color.getAlpha() > 0) {
out.writeFieldValue(',', "alpha", color.getAlpha());
}
} else {
throw new JSONException("not support awt class : " + object.getClass().getName());
}

out.write('}');

}

protected char writeClassName(SerializeWriter out, Class<?> clazz, char sep) {
if (out.isEnabled(SerializerFeature.WriteClassName)) {
out.write('{');
out.writeFieldName(JSON.DEFAULT_TYPE_KEY);
out.writeString(clazz.getName());
sep = ',';
}
return sep;
}

@SuppressWarnings("unchecked")

public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONLexer lexer = parser.getLexer();

if (lexer.token() == JSONToken.NULL) {
lexer.nextToken(JSONToken.COMMA);
return null;
}

if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
throw new JSONException("syntax error");
}
lexer.nextToken();

if (type == Point.class) {
return (T) parsePoint(parser);
}

if (type == Rectangle.class) {
return (T) parseRectangle(parser);
}

if (type == Color.class) {
return (T) parseColor(parser);
}

if (type == Font.class) {
return (T) parseFont(parser);
}

throw new JSONException("not support awt class : " + type);
}

protected Font parseFont(DefaultJSONParser parser) {
JSONLexer lexer = parser.getLexer();

int size = 0, style = 0;
String name = null;
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");
}


if (key.equalsIgnoreCase("name")) {
if (lexer.token() == JSONToken.LITERAL_STRING) {
name = lexer.stringVal();
lexer.nextToken();
} else {
throw new JSONException("syntax error");
}
} else if (key.equalsIgnoreCase("style")) {
if (lexer.token() == JSONToken.LITERAL_INT) {
style = lexer.intValue();
lexer.nextToken();
} else {
throw new JSONException("syntax error");
}
} else if (key.equalsIgnoreCase("size")) {
if (lexer.token() == JSONToken.LITERAL_INT) {
size = lexer.intValue();
lexer.nextToken();
} else {
throw new JSONException("syntax error");
}
} else {
throw new JSONException("syntax error, " + key);
}

if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(JSONToken.LITERAL_STRING);
}
}

return new Font(name, style, size);
}

protected Color parseColor(DefaultJSONParser parser) {
JSONLexer lexer = parser.getLexer();

int r = 0, g = 0, b = 0, alpha = 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("r")) {
r = val;
} else if (key.equalsIgnoreCase("g")) {
g = val;
} else if (key.equalsIgnoreCase("b")) {
b = val;
} else if (key.equalsIgnoreCase("alpha")) {
alpha = val;
} else {
throw new JSONException("syntax error, " + key);
}

if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(JSONToken.LITERAL_STRING);
}
}

return new Color(r, g, b, alpha);
}

protected Rectangle parseRectangle(DefaultJSONParser parser) {
JSONLexer lexer = parser.getLexer();

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 new Rectangle(x, y, width, height);
}

protected Point parsePoint(DefaultJSONParser parser) {
JSONLexer lexer = parser.getLexer();

int x = 0, y = 0;
for (;;) {
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
break;
}

String key;
if (lexer.token() == JSONToken.LITERAL_STRING) {
key = lexer.stringVal();

if (JSON.DEFAULT_TYPE_KEY.equals(key)) {
parser.acceptType("java.awt.Point");
continue;
}

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 : " + lexer.tokenName());
}

if (key.equalsIgnoreCase("x")) {
x = val;
} else if (key.equalsIgnoreCase("y")) {
y = val;
} else {
throw new JSONException("syntax error, " + key);
}

if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(JSONToken.LITERAL_STRING);
}
}

return new Point(x, y);
}

public int getFastMatchToken() {
return JSONToken.LBRACE;
}
}

0 comments on commit a49b57b

Please sign in to comment.