Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
384 additions
and
475 deletions.
There are no files selected for viewing
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
315 changes: 315 additions & 0 deletions
315
src/main/java/com/alibaba/fastjson/serializer/AwtCodec.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.