json解析的原理
程序员文章站
2022-05-31 19:25:58
...
众所周知,json格式的字符串,有很多公用包可以做到,那具体怎么做的呢?
其实猜也可以猜到,大概是通过类Class的反射,获取每个属性,然后输出成响应格式的文本,来看看fastjson-1.2.4具体实现:
public static final String toJSONString(Object object) { return toJSONString(object, new SerializerFeature[0]); } public static final String toJSONString(Object object, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) { serializer.config(feature, true); } serializer.write(object); return out.toString(); } finally { out.close(); } }
public final void write(Object object) { if (object == null) { out.writeNull(); return; } Class<?> clazz = object.getClass(); ObjectSerializer writer = getObjectWriter(clazz); try { writer.write(this, object, null, null, 0); } catch (IOException e) { throw new JSONException(e.getMessage(), e); } }
public ObjectSerializer getObjectWriter(Class<?> clazz) { ObjectSerializer writer = get(clazz); if (writer == null) { try { final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); for (Object o : ServiceLoader.load(AutowiredObjectSerializer.class, classLoader)) { if (!(o instanceof AutowiredObjectSerializer)) { continue; } AutowiredObjectSerializer autowired = (AutowiredObjectSerializer) o; for (Type forType : autowired.getAutowiredFor()) { put(forType, autowired); } } } catch (ClassCastException ex) { // skip } writer = get(clazz); } if (writer == null) { final ClassLoader classLoader = JSON.class.getClassLoader(); if (classLoader != Thread.currentThread().getContextClassLoader()) { try { for (Object o : ServiceLoader.load(AutowiredObjectSerializer.class, classLoader)) { if (!(o instanceof AutowiredObjectSerializer)) { continue; } AutowiredObjectSerializer autowired = (AutowiredObjectSerializer) o; for (Type forType : autowired.getAutowiredFor()) { put(forType, autowired); } } } catch (ClassCastException ex) { // skip } writer = get(clazz); } } if (writer == null) { if (Map.class.isAssignableFrom(clazz)) { put(clazz, MapSerializer.instance); } else if (List.class.isAssignableFrom(clazz)) { put(clazz, ListSerializer.instance); } else if (Collection.class.isAssignableFrom(clazz)) { put(clazz, CollectionSerializer.instance); } else if (Date.class.isAssignableFrom(clazz)) { put(clazz, DateSerializer.instance); } else if (JSONAware.class.isAssignableFrom(clazz)) { put(clazz, JSONAwareSerializer.instance); } else if (JSONSerializable.class.isAssignableFrom(clazz)) { put(clazz, JSONSerializableSerializer.instance); } else if (JSONStreamAware.class.isAssignableFrom(clazz)) { put(clazz, JSONStreamAwareSerializer.instance); } else if (clazz.isEnum() || (clazz.getSuperclass() != null && clazz.getSuperclass().isEnum())) { put(clazz, EnumSerializer.instance); } else if (clazz.isArray()) { Class<?> componentType = clazz.getComponentType(); ObjectSerializer compObjectSerializer = getObjectWriter(componentType); put(clazz, new ArraySerializer(componentType, compObjectSerializer)); } else if (Throwable.class.isAssignableFrom(clazz)) { put(clazz, new ExceptionSerializer(clazz)); } else if (TimeZone.class.isAssignableFrom(clazz)) { put(clazz, TimeZoneCodec.instance); } else if (Appendable.class.isAssignableFrom(clazz)) { put(clazz, AppendableSerializer.instance); } else if (Charset.class.isAssignableFrom(clazz)) { put(clazz, CharsetCodec.instance); } else if (Enumeration.class.isAssignableFrom(clazz)) { put(clazz, EnumerationSeriliazer.instance); } else if (Calendar.class.isAssignableFrom(clazz)) { put(clazz, CalendarCodec.instance); } else if (Clob.class.isAssignableFrom(clazz)) { put(clazz, ClobSeriliazer.instance); } else { boolean isCglibProxy = false; boolean isJavassistProxy = false; for (Class<?> item : clazz.getInterfaces()) { if (item.getName().equals("net.sf.cglib.proxy.Factory") || item.getName().equals("org.springframework.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); put(clazz, superWriter); return superWriter; } if (Proxy.isProxyClass(clazz)) { put(clazz, createJavaBeanSerializer(clazz)); } else { put(clazz, createJavaBeanSerializer(clazz)); } } writer = get(clazz); } return writer; }
看到这里就差不多明白了,就是根据其不同类型,拿到具体的ObjectSerializer子类,来做不同的输入操作。
上一篇: “泛舟之役”不是战争是什么?“泛舟之役”的事件始末!
下一篇: jackson自定义序列化和反序列化
推荐阅读
-
文件粉碎的基本原理及解析介绍
-
如何使用jQuery定时器的原理实现banner图片切换
-
解析:php调用MsSQL存储过程使用内置RETVAL获取过程中的return值
-
JS获取url参数,JS发送json格式的POST请求方法
-
spirngmvc js传递复杂json参数到controller的实例
-
jquery ajax,ashx,json的用法总结
-
LRUCache的实现原理及利用python实现的方法
-
H3C终端控制台的简单解析 (用终端仿真到路由器里,对路由器作配置)
-
Android插件化原理解析--插件化加载机制
-
jQuery中关于ScrollableGridPlugin.js(固定表头)插件的使用逐步解析