com.alibaba.fastjson.util.ServiceLoader Java Examples
The following examples show how to use
com.alibaba.fastjson.util.ServiceLoader.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSONSerializer.java From letv with Apache License 2.0 | 4 votes |
public ObjectSerializer getObjectWriter(Class<?> clazz) { AutowiredObjectSerializer autowired; ObjectSerializer objectSerializer = (ObjectSerializer) this.config.get(clazz); if (objectSerializer == null) { try { for (AutowiredObjectSerializer o : ServiceLoader.load(AutowiredObjectSerializer.class, Thread.currentThread().getContextClassLoader())) { if (o instanceof AutowiredObjectSerializer) { autowired = o; for (Type forType : autowired.getAutowiredFor()) { this.config.put(forType, autowired); } } } } catch (ClassCastException e) { } objectSerializer = (ObjectSerializer) this.config.get(clazz); } if (objectSerializer == null) { ClassLoader classLoader = JSON.class.getClassLoader(); if (classLoader != Thread.currentThread().getContextClassLoader()) { try { for (AutowiredObjectSerializer o2 : ServiceLoader.load(AutowiredObjectSerializer.class, classLoader)) { if (o2 instanceof AutowiredObjectSerializer) { autowired = o2; for (Type forType2 : autowired.getAutowiredFor()) { this.config.put(forType2, autowired); } } } } catch (ClassCastException e2) { } objectSerializer = (ObjectSerializer) this.config.get(clazz); } } if (objectSerializer == null) { if (Map.class.isAssignableFrom(clazz)) { this.config.put(clazz, MapSerializer.instance); } else if (List.class.isAssignableFrom(clazz)) { this.config.put(clazz, ListSerializer.instance); } else if (Collection.class.isAssignableFrom(clazz)) { this.config.put(clazz, CollectionSerializer.instance); } else if (Date.class.isAssignableFrom(clazz)) { this.config.put(clazz, DateSerializer.instance); } else if (JSONAware.class.isAssignableFrom(clazz)) { this.config.put(clazz, JSONAwareSerializer.instance); } else if (JSONStreamAware.class.isAssignableFrom(clazz)) { this.config.put(clazz, JSONStreamAwareSerializer.instance); } else if (clazz.isEnum() || (clazz.getSuperclass() != null && clazz.getSuperclass().isEnum())) { this.config.put(clazz, EnumSerializer.instance); } else if (clazz.isArray()) { Class<?> componentType = clazz.getComponentType(); ObjectSerializer compObjectSerializer = getObjectWriter(componentType); this.config.put(clazz, new ArraySerializer(componentType, compObjectSerializer)); } else if (Throwable.class.isAssignableFrom(clazz)) { this.config.put(clazz, new ExceptionSerializer(clazz)); } else if (TimeZone.class.isAssignableFrom(clazz)) { this.config.put(clazz, TimeZoneCodec.instance); } else if (Appendable.class.isAssignableFrom(clazz)) { this.config.put(clazz, AppendableSerializer.instance); } else if (Charset.class.isAssignableFrom(clazz)) { this.config.put(clazz, CharsetCodec.instance); } else if (Enumeration.class.isAssignableFrom(clazz)) { this.config.put(clazz, EnumerationSeriliazer.instance); } else if (Calendar.class.isAssignableFrom(clazz)) { this.config.put(clazz, CalendarCodec.instance); } else if (Clob.class.isAssignableFrom(clazz)) { this.config.put(clazz, ClobSeriliazer.instance); } else { boolean isCglibProxy = false; boolean isJavassistProxy = false; Class[] arr$ = clazz.getInterfaces(); int len$ = arr$.length; int i$ = 0; while (i$ < len$) { Class<?> item = arr$[i$]; if (item.getName().equals("net.sf.cglib.proxy.Factory")) { isCglibProxy = true; break; } else if (item.getName().equals("javassist.util.proxy.ProxyObject")) { isJavassistProxy = true; break; } else { i$++; } } if (isCglibProxy || isJavassistProxy) { ObjectSerializer superWriter = getObjectWriter(clazz.getSuperclass()); this.config.put(clazz, superWriter); return superWriter; } else if (Proxy.isProxyClass(clazz)) { this.config.put(clazz, this.config.createJavaBeanSerializer(clazz)); } else { this.config.put(clazz, this.config.createJavaBeanSerializer(clazz)); } } objectSerializer = (ObjectSerializer) this.config.get(clazz); } return objectSerializer; }