JSON成员函数
概要
fastjson序列化主要使用入口就是在JSON.java
类中,它提供非常简便和友好的api将java对象转换成json字符串。
/**
* 便捷序列化java对象,序列化对象可以包含任意泛型属性字段,但是不适用本身是泛型的对象。
* 默认序列化返回字符串,可以使用writeJSONString(Writer, Object, SerializerFeature[])
* 将序列化字符串输出到指定输出器中
*/
public static String toJSONString(Object object) {
/**
* 直接调用重载方法,将指定object序列化成json字符串,忽略序列化filter
*/
return toJSONString(object, emptyFilters);
}
使用便捷接口toJSONString方法,可以将任意java对象序列化为json字符串,内部调用toJSONString(Object, SerializeFilter[], SerializerFeature... )
:
public static String toJSONString(Object object, SerializeFilter[] filters, SerializerFeature... features) {
return toJSONString(object, SerializeConfig.globalInstance, filters, null, DEFAULT_GENERATE_FEATURE, features);
}
继续跟踪方法调用到toJSONString(Object, SerializeConfig ,SerializeFilter[], String, int, SerializerFeature... )
:
public static String toJSONString(Object object, /** 序列化对象 */
SerializeConfig config, /** 全局序列化配置 */
SerializeFilter[] filters, /** 序列化拦截器 */
String dateFormat, /** 序列化日期格式 */
int defaultFeatures, /** 默认序列化特性 */
SerializerFeature... features) { /** 自定义序列化特性 */
/** 初始化序列化writer,用features覆盖defaultFeatures配置 */
SerializeWriter out = new SerializeWriter(null, defaultFeatures, features);
try {
/**
* 初始化JSONSerializer,序列化类型由它委托config查找具体
* 序列化处理器处理,序列化结果写入out的buffer中
*/
JSONSerializer serializer = new JSONSerializer(out, config);
if (dateFormat != null && dateFormat.length() != 0) {
serializer.setDateFormat(dateFormat);
/** 调用out 重新配置属性 并且打开WriteDateUseDateFormat特性 */
serializer.config(SerializerFeature.WriteDateUseDateFormat, true);
}
if (filters != null) {
for (SerializeFilter filter : filters) {
/** 添加拦截器 */
serializer.addFilter(filter);
}
}
/** 使用序列化实例转换对象,查找具体序列化实例委托给config查找 */
serializer.write(object);
return out.toString();
} finally {
out.close();
}
}
public static void write(SerializeWriter out, Object object) {
JSONSerializer serializer = new JSONSerializer(out);
serializer.write(object);
}
/**
* @Description
* 这个序列化方法实际并不是真正执行序列化操作,首先做序列化特性配置,
* 然后追加序列化拦截器,开始执行序列化对象操作委托给了config对象查找。
* 我们继续进入serializer.write(object) 查看:
*/
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) {
return config.getObjectWriter(clazz);
}
public ObjectSerializer getObjectWriter(Class<?> clazz) {
return getObjectWriter(clazz, true);
}
/**
* @Author liuhaidong
* @Description
* 内部又调用com.alibaba.fastjson.serializer.SerializeConfig#
* getObjectWriter(java.lang.Class < ? >, boolean),这个类实现相对复杂了一些,
* 我会按照代码顺序梳理所有序列化实例的要点 :
*
* 查找具体序列化实例,查找方法基本思想根据class类型或者实现接口类型进行匹配查找。
* 接下来针对逐个序列化实现依次分析。
*/
public ObjectSerializer getObjectWriter(Class<?> clazz, boolean create) {
/** 首先从内部已经注册查找特定class的序列化实例 */
ObjectSerializer writer = get(clazz);
if (writer == null) {
try {
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
/** 使用当前线程类加载器 查找 META-INF/services/AutowiredObjectSerializer.class实现类 */
for (Object o : ServiceLoader.load(AutowiredObjectSerializer.class, classLoader)) {
if (!(o instanceof AutowiredObjectSerializer)) {
continue;
}
AutowiredObjectSerializer autowired = (AutowiredObjectSerializer) o;
for (Type forType : autowired.getAutowiredFor()) {
//如果存在,注册到内部serializers缓存中
put(forType, autowired);
}
}
} catch (ClassCastException ex) {
// skip
}
writer = get(clazz);
}
if (writer == null) {
final ClassLoader classLoader = JSON.class.getClassLoader();
/** 使用加载JSON类的加载器 查找 META-INF/services/AutowiredObjectSerializer.class实现类 */
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()) {
//如果存在,注册到内部serializers缓存中
put(forType, autowired);
}
}
} catch (ClassCastException ex) {
// skip
}
//尝试在已注册缓存找到特定class的序列化实例
writer = get(clazz);
}
}
for (Module module : modules) {
writer = module.createSerializer(this, clazz);
if (writer != null) {
put(clazz, writer);
return writer;
}
}
if (writer == null) {
String className = clazz.getName();
Class<?> superClass;
if (Map.class.isAssignableFrom(clazz)) {
//如果class实现类Map接口,使用MapSerializer序列化
put(clazz, writer = MapSerializer.instance);
} else if (List.class.isAssignableFrom(clazz)) {
//如果class实现类List接口,使用ListSerializer序列化
put(clazz, writer = ListSerializer.instance);
} else if (Collection.class.isAssignableFrom(clazz)) {
//如果class实现类Collection接口,使用CollectionCodec序列化
put(clazz, writer = CollectionCodec.instance);
} else if (Date.class.isAssignableFrom(clazz)) {
//如果class继承Date,使用DateCodec序列化
put(clazz, writer = DateCodec.instance);
} else if (JSONAware.class.isAssignableFrom(clazz)) {
//如果class实现类JSONAware接口,使用JSONAwareSerializer序列化
put(clazz, writer = JSONAwareSerializer.instance);
} else if (JSONSerializable.class.isAssignableFrom(clazz)) {
//如果class实现类JSONSerializable接口,使用JSONSerializableSerializer序列化
put(clazz, writer = JSONSerializableSerializer.instance);
} else if (JSONStreamAware.class.isAssignableFrom(clazz)) {
//如果class实现类JSONStreamAware接口,使用MiscCodecr序列化
put(clazz, writer = MiscCodec.instance);
} else if (clazz.isEnum()) {
JSONType jsonType = TypeUtils.getAnnotation(clazz, JSONType.class);
if (jsonType != null && jsonType.serializeEnumAsJavaBean()) {
/** 如果是枚举类型,并且启用特性 serializeEnumAsJavaBean
* 使用JavaBeanSerializer序列化(假设没有启用asm)
*/
put(clazz, writer = createJavaBeanSerializer(clazz));
} else {
/** 如果是枚举类型,没有启用特性 serializeEnumAsJavaBean
* 使用EnumSerializer序列化
*/
put(clazz, writer = EnumSerializer.instance);
}
} else if ((superClass = clazz.getSuperclass()) != null && superClass.isEnum()) {
JSONType jsonType = TypeUtils.getAnnotation(superClass, JSONType.class);
if (jsonType != null && jsonType.serializeEnumAsJavaBean()) {
/** 如果父类是枚举类型,并且启用特性 serializeEnumAsJavaBean
* 使用JavaBeanSerializer序列化(假设没有启用asm)
*/
put(clazz, writer = createJavaBeanSerializer(clazz));
} else {
/** 如果父类是枚举类型,没有启用特性 serializeEnumAsJavaBean
* 使用EnumSerializer序列化
*/
put(clazz, writer = EnumSerializer.instance);
}
} else if (clazz.isArray()) {
Class<?> componentType = clazz.getComponentType();
ObjectSerializer compObjectSerializer = getObjectWriter(componentType);
put(clazz, writer = new ArraySerializer(componentType, compObjectSerializer));
} else if (Throwable.class.isAssignableFrom(clazz)) {
SerializeBeanInfo beanInfo = TypeUtils.buildBeanInfo(clazz, null, propertyNamingStrategy);
beanInfo.features |= SerializerFeature.WriteClassName.mask;
put(clazz, writer = new JavaBeanSerializer(beanInfo));
} else if (TimeZone.class.isAssignableFrom(clazz) || Map.Entry.class.isAssignableFrom(clazz)) {
put(clazz, writer = MiscCodec.instance);
} else if (Appendable.class.isAssignableFrom(clazz)) {
put(clazz, writer = AppendableSerializer.instance);
} else if (Charset.class.isAssignableFrom(clazz)) {
put(clazz, writer = ToStringSerializer.instance);
} else if (Enumeration.class.isAssignableFrom(clazz)) {
put(clazz, writer = EnumerationSerializer.instance);
} else if (Calendar.class.isAssignableFrom(clazz) //
|| XMLGregorianCalendar.class.isAssignableFrom(clazz)) {
put(clazz, writer = CalendarCodec.instance);
} else if (TypeUtils.isClob(clazz)) {
put(clazz, writer = ClobSeriliazer.instance);
} else if (TypeUtils.isPath(clazz)) {
put(clazz, writer = ToStringSerializer.instance);
} else if (Iterator.class.isAssignableFrom(clazz)) {
put(clazz, writer = MiscCodec.instance);
} else if (org.w3c.dom.Node.class.isAssignableFrom(clazz)) {
put(clazz, writer = MiscCodec.instance);
} else {
if (className.startsWith("java.awt.") //
&& AwtCodec.support(clazz) //
) {
// awt
if (!awtError) {
try {
String[] names = new String[]{
"java.awt.Color",
"java.awt.Font",
"java.awt.Point",
"java.awt.Rectangle"
};
for (String name : names) {
if (name.equals(className)) {
put(Class.forName(name), writer = AwtCodec.instance);
return writer;
}
}
} catch (Throwable e) {
awtError = true;
// skip
}
}
}
// jdk8
if ((!jdk8Error) //
&& (className.startsWith("java.time.") //
|| className.startsWith("java.util.Optional") //
|| className.equals("java.util.concurrent.atomic.LongAdder")
|| className.equals("java.util.concurrent.atomic.DoubleAdder")
)) {
try {
{
String[] names = new String[]{
"java.time.LocalDateTime",
"java.time.LocalDate",
"java.time.LocalTime",
"java.time.ZonedDateTime",
"java.time.OffsetDateTime",
"java.time.OffsetTime",
"java.time.ZoneOffset",
"java.time.ZoneRegion",
"java.time.Period",
"java.time.Duration",
"java.time.Instant"
};
for (String name : names) {
if (name.equals(className)) {
put(Class.forName(name), writer = Jdk8DateCodec.instance);
return writer;
}
}
}
{
String[] names = new String[]{
"java.util.Optional",
"java.util.OptionalDouble",
"java.util.OptionalInt",
"java.util.OptionalLong"
};
for (String name : names) {
if (name.equals(className)) {
put(Class.forName(name), writer = OptionalCodec.instance);
return writer;
}
}
}
{
String[] names = new String[]{
"java.util.concurrent.atomic.LongAdder",
"java.util.concurrent.atomic.DoubleAdder"
};
for (String name : names) {
if (name.equals(className)) {
put(Class.forName(name), writer = AdderSerializer.instance);
return writer;
}
}
}
} catch (Throwable e) {
// skip
jdk8Error = true;
}
}
if ((!oracleJdbcError) //
&& className.startsWith("oracle.sql.")) {
try {
String[] names = new String[] {
"oracle.sql.DATE",
"oracle.sql.TIMESTAMP"
};
for (String name : names) {
if (name.equals(className)) {
put(Class.forName(name), writer = DateCodec.instance);
return writer;
}
}
} catch (Throwable e) {
// skip
oracleJdbcError = true;
}
}
if ((!springfoxError) //
&& className.equals("springfox.documentation.spring.web.json.Json")) {
try {
put(Class.forName("springfox.documentation.spring.web.json.Json"), //
writer = SwaggerJsonSerializer.instance);
return writer;
} catch (ClassNotFoundException e) {
// skip
springfoxError = true;
}
}
if ((!guavaError) //
&& className.startsWith("com.google.common.collect.")) {
try {
String[] names = new String[] {
"com.google.common.collect.HashMultimap",
"com.google.common.collect.LinkedListMultimap",
"com.google.common.collect.LinkedHashMultimap",
"com.google.common.collect.ArrayListMultimap",
"com.google.common.collect.TreeMultimap"
};
for (String name : names) {
if (name.equals(className)) {
put(Class.forName(name), writer = GuavaCodec.instance);
return writer;
}
}
} catch (ClassNotFoundException e) {
// skip
guavaError = true;
}
}
if ((!jsonnullError) && className.equals("net.sf.json.JSONNull")) {
try {
put(Class.forName("net.sf.json.JSONNull"), writer = MiscCodec.instance);
return writer;
} catch (ClassNotFoundException e) {
// skip
jsonnullError = true;
}
}
if (!jsonobjectError && className.equals("org.json.JSONObject")) {
try {
put(Class.forName("org.json.JSONObject"), writer = JSONObjectCodec.instance);
return writer;
} catch (ClassNotFoundException e) {
// skip
jsonobjectError = true;
}
}
if ((!jodaError) && className.startsWith("org.joda.")) {
try {
String[] names = new String[] {
"org.joda.time.LocalDate",
"org.joda.time.LocalDateTime",
"org.joda.time.LocalTime",
"org.joda.time.Instant",
"org.joda.time.DateTime",
"org.joda.time.Period",
"org.joda.time.Duration",
"org.joda.time.DateTimeZone",
"org.joda.time.UTCDateTimeZone",
"org.joda.time.tz.CachedDateTimeZone",
"org.joda.time.tz.FixedDateTimeZone",
};
for (String name : names) {
if (name.equals(className)) {
put(Class.forName(name), writer = JodaCodec.instance);
return writer;
}
}
} catch (ClassNotFoundException e) {
// skip
jodaError = true;
}
}
if ("java.nio.HeapByteBuffer".equals(className)) {
put(clazz, writer = ByteBufferCodec.instance);
return writer;
}
if ("org.javamoney.moneta.Money".equals(className)) {
put(clazz, writer = MonetaCodec.instance);
return writer;
}
Class[] interfaces = clazz.getInterfaces();
if (interfaces.length == 1 && interfaces[0].isAnnotation()) {
put(clazz, AnnotationSerializer.instance);
return AnnotationSerializer.instance;
}
if (TypeUtils.isProxy(clazz)) {
Class<?> superClazz = clazz.getSuperclass();
ObjectSerializer superWriter = getObjectWriter(superClazz);
put(clazz, superWriter);
return superWriter;
}
if (Proxy.isProxyClass(clazz)) {
Class handlerClass = null;
if (interfaces.length == 2) {
handlerClass = interfaces[1];
} else {
for (Class proxiedInterface : interfaces) {
if (proxiedInterface.getName().startsWith("org.springframework.aop.")) {
continue;
}
if (handlerClass != null) {
handlerClass = null; // multi-matched
break;
}
handlerClass = proxiedInterface;
}
}
if (handlerClass != null) {
ObjectSerializer superWriter = getObjectWriter(handlerClass);
put(clazz, superWriter);
return superWriter;
}
}
if (create) {
writer = createJavaBeanSerializer(clazz);
put(clazz, writer);
}
}
if (writer == null) {
writer = get(clazz);
}
}
return writer;
}