package org.zbus.common.json.serializer; import java.awt.Font; import java.io.IOException; import java.lang.reflect.Type; import org.zbus.common.json.JSON; import org.zbus.common.json.JSONException; import org.zbus.common.json.parser.DefaultJSONParser; import org.zbus.common.json.parser.JSONLexer; import org.zbus.common.json.parser.JSONToken; import org.zbus.common.json.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) 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; } }