/src/main/java/com/alibaba/fastjson/serializer/AwtCodec.java

https://github.com/alibaba/fastjson · Java · 350 lines · 290 code · 60 blank · 0 comment · 121 complexity · 64f5446acaac9ffffca6e151da68e63f MD5 · raw file

  1. package com.alibaba.fastjson.serializer;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Point;
  5. import java.awt.Rectangle;
  6. import java.io.IOException;
  7. import java.lang.reflect.Type;
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.JSONException;
  10. import com.alibaba.fastjson.parser.DefaultJSONParser;
  11. import com.alibaba.fastjson.parser.JSONLexer;
  12. import com.alibaba.fastjson.parser.JSONToken;
  13. import com.alibaba.fastjson.parser.ParseContext;
  14. import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
  15. public class AwtCodec implements ObjectSerializer, ObjectDeserializer {
  16. public final static AwtCodec instance = new AwtCodec();
  17. public static boolean support(Class<?> clazz) {
  18. return clazz == Point.class //
  19. || clazz == Rectangle.class //
  20. || clazz == Font.class //
  21. || clazz == Color.class //
  22. ;
  23. }
  24. public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
  25. int features) throws IOException {
  26. SerializeWriter out = serializer.out;
  27. if (object == null) {
  28. out.writeNull();
  29. return;
  30. }
  31. char sep = '{';
  32. if (object instanceof Point) {
  33. Point font = (Point) object;
  34. sep = writeClassName(out, Point.class, sep);
  35. out.writeFieldValue(sep, "x", font.x);
  36. out.writeFieldValue(',', "y", font.y);
  37. } else if (object instanceof Font) {
  38. Font font = (Font) object;
  39. sep = writeClassName(out, Font.class, sep);
  40. out.writeFieldValue(sep, "name", font.getName());
  41. out.writeFieldValue(',', "style", font.getStyle());
  42. out.writeFieldValue(',', "size", font.getSize());
  43. } else if (object instanceof Rectangle) {
  44. Rectangle rectangle = (Rectangle) object;
  45. sep = writeClassName(out, Rectangle.class, sep);
  46. out.writeFieldValue(sep, "x", rectangle.x);
  47. out.writeFieldValue(',', "y", rectangle.y);
  48. out.writeFieldValue(',', "width", rectangle.width);
  49. out.writeFieldValue(',', "height", rectangle.height);
  50. } else if (object instanceof Color) {
  51. Color color = (Color) object;
  52. sep = writeClassName(out, Color.class, sep);
  53. out.writeFieldValue(sep, "r", color.getRed());
  54. out.writeFieldValue(',', "g", color.getGreen());
  55. out.writeFieldValue(',', "b", color.getBlue());
  56. if (color.getAlpha() > 0) {
  57. out.writeFieldValue(',', "alpha", color.getAlpha());
  58. }
  59. } else {
  60. throw new JSONException("not support awt class : " + object.getClass().getName());
  61. }
  62. out.write('}');
  63. }
  64. protected char writeClassName(SerializeWriter out, Class<?> clazz, char sep) {
  65. if (out.isEnabled(SerializerFeature.WriteClassName)) {
  66. out.write('{');
  67. out.writeFieldName(JSON.DEFAULT_TYPE_KEY);
  68. out.writeString(clazz.getName());
  69. sep = ',';
  70. }
  71. return sep;
  72. }
  73. @SuppressWarnings("unchecked")
  74. public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
  75. JSONLexer lexer = parser.lexer;
  76. if (lexer.token() == JSONToken.NULL) {
  77. lexer.nextToken(JSONToken.COMMA);
  78. return null;
  79. }
  80. if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
  81. throw new JSONException("syntax error");
  82. }
  83. lexer.nextToken();
  84. T obj;
  85. if (type == Point.class) {
  86. obj = (T) parsePoint(parser, fieldName);
  87. } else if (type == Rectangle.class) {
  88. obj = (T) parseRectangle(parser);
  89. } else if (type == Color.class) {
  90. obj = (T) parseColor(parser);
  91. } else if (type == Font.class) {
  92. obj = (T) parseFont(parser);
  93. } else {
  94. throw new JSONException("not support awt class : " + type);
  95. }
  96. ParseContext context = parser.getContext();
  97. parser.setContext(obj, fieldName);
  98. parser.setContext(context);
  99. return obj;
  100. }
  101. protected Font parseFont(DefaultJSONParser parser) {
  102. JSONLexer lexer = parser.lexer;
  103. int size = 0, style = 0;
  104. String name = null;
  105. for (;;) {
  106. if (lexer.token() == JSONToken.RBRACE) {
  107. lexer.nextToken();
  108. break;
  109. }
  110. String key;
  111. if (lexer.token() == JSONToken.LITERAL_STRING) {
  112. key = lexer.stringVal();
  113. lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
  114. } else {
  115. throw new JSONException("syntax error");
  116. }
  117. if (key.equalsIgnoreCase("name")) {
  118. if (lexer.token() == JSONToken.LITERAL_STRING) {
  119. name = lexer.stringVal();
  120. lexer.nextToken();
  121. } else {
  122. throw new JSONException("syntax error");
  123. }
  124. } else if (key.equalsIgnoreCase("style")) {
  125. if (lexer.token() == JSONToken.LITERAL_INT) {
  126. style = lexer.intValue();
  127. lexer.nextToken();
  128. } else {
  129. throw new JSONException("syntax error");
  130. }
  131. } else if (key.equalsIgnoreCase("size")) {
  132. if (lexer.token() == JSONToken.LITERAL_INT) {
  133. size = lexer.intValue();
  134. lexer.nextToken();
  135. } else {
  136. throw new JSONException("syntax error");
  137. }
  138. } else {
  139. throw new JSONException("syntax error, " + key);
  140. }
  141. if (lexer.token() == JSONToken.COMMA) {
  142. lexer.nextToken(JSONToken.LITERAL_STRING);
  143. }
  144. }
  145. return new Font(name, style, size);
  146. }
  147. protected Color parseColor(DefaultJSONParser parser) {
  148. JSONLexer lexer = parser.lexer;
  149. int r = 0, g = 0, b = 0, alpha = 0;
  150. for (;;) {
  151. if (lexer.token() == JSONToken.RBRACE) {
  152. lexer.nextToken();
  153. break;
  154. }
  155. String key;
  156. if (lexer.token() == JSONToken.LITERAL_STRING) {
  157. key = lexer.stringVal();
  158. lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
  159. } else {
  160. throw new JSONException("syntax error");
  161. }
  162. int val;
  163. if (lexer.token() == JSONToken.LITERAL_INT) {
  164. val = lexer.intValue();
  165. lexer.nextToken();
  166. } else {
  167. throw new JSONException("syntax error");
  168. }
  169. if (key.equalsIgnoreCase("r")) {
  170. r = val;
  171. } else if (key.equalsIgnoreCase("g")) {
  172. g = val;
  173. } else if (key.equalsIgnoreCase("b")) {
  174. b = val;
  175. } else if (key.equalsIgnoreCase("alpha")) {
  176. alpha = val;
  177. } else {
  178. throw new JSONException("syntax error, " + key);
  179. }
  180. if (lexer.token() == JSONToken.COMMA) {
  181. lexer.nextToken(JSONToken.LITERAL_STRING);
  182. }
  183. }
  184. return new Color(r, g, b, alpha);
  185. }
  186. protected Rectangle parseRectangle(DefaultJSONParser parser) {
  187. JSONLexer lexer = parser.lexer;
  188. int x = 0, y = 0, width = 0, height = 0;
  189. for (;;) {
  190. if (lexer.token() == JSONToken.RBRACE) {
  191. lexer.nextToken();
  192. break;
  193. }
  194. String key;
  195. if (lexer.token() == JSONToken.LITERAL_STRING) {
  196. key = lexer.stringVal();
  197. lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
  198. } else {
  199. throw new JSONException("syntax error");
  200. }
  201. int val;
  202. int token = lexer.token();
  203. if (token == JSONToken.LITERAL_INT) {
  204. val = lexer.intValue();
  205. lexer.nextToken();
  206. } else if (token == JSONToken.LITERAL_FLOAT) {
  207. val = (int) lexer.floatValue();
  208. lexer.nextToken();
  209. } else {
  210. throw new JSONException("syntax error");
  211. }
  212. if (key.equalsIgnoreCase("x")) {
  213. x = val;
  214. } else if (key.equalsIgnoreCase("y")) {
  215. y = val;
  216. } else if (key.equalsIgnoreCase("width")) {
  217. width = val;
  218. } else if (key.equalsIgnoreCase("height")) {
  219. height = val;
  220. } else {
  221. throw new JSONException("syntax error, " + key);
  222. }
  223. if (lexer.token() == JSONToken.COMMA) {
  224. lexer.nextToken(JSONToken.LITERAL_STRING);
  225. }
  226. }
  227. return new Rectangle(x, y, width, height);
  228. }
  229. protected Point parsePoint(DefaultJSONParser parser, Object fieldName) {
  230. JSONLexer lexer = parser.lexer;
  231. int x = 0, y = 0;
  232. for (;;) {
  233. if (lexer.token() == JSONToken.RBRACE) {
  234. lexer.nextToken();
  235. break;
  236. }
  237. String key;
  238. if (lexer.token() == JSONToken.LITERAL_STRING) {
  239. key = lexer.stringVal();
  240. if (JSON.DEFAULT_TYPE_KEY.equals(key)) {
  241. parser.acceptType("java.awt.Point");
  242. continue;
  243. }
  244. if ("$ref".equals(key)) {
  245. return (Point) parseRef(parser, fieldName);
  246. }
  247. lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
  248. } else {
  249. throw new JSONException("syntax error");
  250. }
  251. int token = lexer.token();
  252. int val;
  253. if (token == JSONToken.LITERAL_INT) {
  254. val = lexer.intValue();
  255. lexer.nextToken();
  256. } else if(token == JSONToken.LITERAL_FLOAT) {
  257. val = (int) lexer.floatValue();
  258. lexer.nextToken();
  259. } else {
  260. throw new JSONException("syntax error : " + lexer.tokenName());
  261. }
  262. if (key.equalsIgnoreCase("x")) {
  263. x = val;
  264. } else if (key.equalsIgnoreCase("y")) {
  265. y = val;
  266. } else {
  267. throw new JSONException("syntax error, " + key);
  268. }
  269. if (lexer.token() == JSONToken.COMMA) {
  270. lexer.nextToken(JSONToken.LITERAL_STRING);
  271. }
  272. }
  273. return new Point(x, y);
  274. }
  275. private Object parseRef(DefaultJSONParser parser, Object fieldName) {
  276. JSONLexer lexer = parser.getLexer();
  277. lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
  278. String ref = lexer.stringVal();
  279. parser.setContext(parser.getContext(), fieldName);
  280. parser.addResolveTask(new DefaultJSONParser.ResolveTask(parser.getContext(), ref));
  281. parser.popContext();
  282. parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
  283. lexer.nextToken(JSONToken.RBRACE);
  284. parser.accept(JSONToken.RBRACE);
  285. return null;
  286. }
  287. public int getFastMatchToken() {
  288. return JSONToken.LBRACE;
  289. }
  290. }