@IocInit public void initialize() { register(Void.TYPE, VoidResultHandler.class); register(Object.class, ObjectResultHandler.class); register(String.class, StringResultHandler.class); register(HttpStatus.class, HttpStatusResultHandler.class); register(RawText.class, RawTextResultHandler.class); register(RawData.class, RawDataResultHandler.class); register(RawDownload.class, RawDownloadResultHandler.class); if (ClassUtils.available("com.alibaba.fastjson.JSONAware")) { register(JSONAware.class, FastjsonResultHandler.class); } if (ClassUtils.available("com.google.gson.JsonElement")) { register(JsonElement.class, GsonResultHandler.class); } }
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { SerializeWriter out = serializer.out; if (object == null) { out.writeNull(); return; } JSONAware aware = (JSONAware) object; out.write(aware.toJSONString()); }
public void test_3() throws Exception { JSONSerializer serializer = new JSONSerializer(); serializer.write(new JSONAware() { public String toJSONString() { return "null"; } }); Assert.assertEquals("null", serializer.getWriter().toString()); }
public void test_3_s() throws Exception { JSONSerializer serializer = new JSONSerializer(); serializer.write(new JSONAware() { public String toJSONString() { return "null"; } }); Assert.assertEquals("null", serializer.getWriter().toString()); }
@Override public void handle(RequestContext ctx, JSONAware result) throws IOException { HttpServletRequest request = ctx.getRequest(); HttpServletResponse response = ctx.getResponse(); if (result == null) { JSONObject json = new JSONObject(); Enumeration<String> e = request.getAttributeNames(); while (e.hasMoreElements()) { String name = e.nextElement(); json.put(name, request.getAttribute(name)); } for (Map.Entry<String, Object> entry : ctx.getModel().entrySet()) { json.put(entry.getKey(), entry.getValue()); } result = json; } String characterEncoding = request.getCharacterEncoding(); response.setCharacterEncoding(characterEncoding); String mimetype = MimetypeUtils.getJSON(request); response.setContentType(mimetype + "; charset=" + characterEncoding); PrintWriter out = response.getWriter(); out.write(result.toJSONString()); out.flush(); }
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException { serializer.getWriter().write(((JSONAware) object).toJSONString()); }
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { SerializeWriter out = serializer.getWriter(); JSONAware aware = (JSONAware) object; out.write(aware.toJSONString()); }
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException { SerializeWriter out = serializer.getWriter(); JSONAware aware = (JSONAware) object; out.write(aware.toJSONString()); }
public ObjectSerializer getObjectWriter(Class<?> clazz) { ObjectSerializer writer = config.get(clazz); if (writer == null) { if (Map.class.isAssignableFrom(clazz)) { config.put(clazz, MapSerializer.instance); } else if (List.class.isAssignableFrom(clazz)) { config.put(clazz, ListSerializer.instance); } else if (Collection.class.isAssignableFrom(clazz)) { config.put(clazz, CollectionSerializer.instance); } else if (Date.class.isAssignableFrom(clazz)) { config.put(clazz, DateSerializer.instance); } else if (JSONAware.class.isAssignableFrom(clazz)) { config.put(clazz, JSONAwareSerializer.instance); } else if (JSONStreamAware.class.isAssignableFrom(clazz)) { config.put(clazz, JSONStreamAwareSerializer.instance); } else if (clazz.isEnum() || (clazz.getSuperclass() != null && clazz.getSuperclass().isEnum())) { config.put(clazz, EnumSerializer.instance); } else if (clazz.isArray()) { Class<?> componentType = clazz.getComponentType(); ObjectSerializer compObjectSerializer = getObjectWriter(componentType); config.put(clazz, new ArraySerializer(componentType, compObjectSerializer)); } else if (Throwable.class.isAssignableFrom(clazz)) { config.put(clazz, new ExceptionSerializer(clazz)); } else if (TimeZone.class.isAssignableFrom(clazz)) { config.put(clazz, TimeZoneSerializer.instance); } else if (Appendable.class.isAssignableFrom(clazz)) { config.put(clazz, AppendableSerializer.instance); } else if (Charset.class.isAssignableFrom(clazz)) { config.put(clazz, ToStringSerializer.instance); } else if (Enumeration.class.isAssignableFrom(clazz)) { config.put(clazz, EnumerationSeriliazer.instance); } else if (Calendar.class.isAssignableFrom(clazz)) { config.put(clazz, CalendarSerializer.instance); } else { boolean isCglibProxy = false; boolean isJavassistProxy = false; for (Class<?> item : clazz.getInterfaces()) { if (item.getName().equals("net.sf.cglib.proxy.Factory")) { isCglibProxy = true; break; } else if (item.getName().equals("javassist.util.proxy.ProxyObject")) { isJavassistProxy = true; break; } } if (isCglibProxy || isJavassistProxy) { Class<?> superClazz = clazz.getSuperclass(); ObjectSerializer superWriter = getObjectWriter(superClazz); config.put(clazz, superWriter); return superWriter; } if (Proxy.isProxyClass(clazz)) { config.put(clazz, config.createJavaBeanSerializer(clazz)); } else { config.put(clazz, config.createJavaBeanSerializer(clazz)); } } writer = config.get(clazz); } return writer; }