package com.alibaba.fastjson.serializer;
import java.awt.Font;
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 FontCodec implements ObjectSerializer, ObjectDeserializer {
public final static FontCodec instance = new FontCodec();
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
SerializeWriter out = serializer.getWriter();
Font font = (Font) object;
if (font == null) {
out.writeNull();
return;
}
char sep = '{';
if (out.isEnabled(SerializerFeature.WriteClassName)) {
out.write('{');
out.writeFieldName(JSON.DEFAULT_TYPE_KEY);
out.writeString(Font.class.getName());
sep = ',';
}
out.writeFieldValue(sep, "name", font.getName());
out.writeFieldValue(',', "style", font.getStyle());
out.writeFieldValue(',', "size", font.getSize());
out.write('}');
}
@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();
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 (T) new Font(name, style, size);
}
public int getFastMatchToken() {
return JSONToken.LBRACE;
}
}