external.com.alibaba.fastjson.serializer.JavaBeanSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sekiro-api Show documentation
Show all versions of sekiro-api Show documentation
ratel api,used for developer on ratel system,an extension for xposed framewrok,ratel api compatable with original xposed framework
/*
* Copyright 1999-2101 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package external.com.alibaba.fastjson.serializer;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.*;
import external.com.alibaba.fastjson.JSON;
import external.com.alibaba.fastjson.JSONException;
import external.com.alibaba.fastjson.PropertyNamingStrategy;
import external.com.alibaba.fastjson.annotation.JSONType;
import external.com.alibaba.fastjson.util.FieldInfo;
import external.com.alibaba.fastjson.util.TypeUtils;
/**
* @author wenshao[[email protected]]
*/
public class JavaBeanSerializer implements ObjectSerializer {
private static final char[] true_chars = new char[] {'t', 'r', 'u', 'e'};
private static final char[] false_chars = new char[] {'f', 'a', 'l', 's', 'e'};
// serializers
private final FieldSerializer[] getters;
private final FieldSerializer[] sortedGetters;
protected int features = 0;
protected final String typeName;
protected final String typeKey;
public JavaBeanSerializer(Class> clazz) {
this(clazz, (PropertyNamingStrategy) null);
}
public JavaBeanSerializer(Class> clazz, PropertyNamingStrategy propertyNamingStrategy){
this(clazz, clazz.getModifiers(), (Map) null, false, true, true, true, propertyNamingStrategy);
}
public JavaBeanSerializer(Class> clazz, String... aliasList){
this(clazz, clazz.getModifiers(), map(aliasList), false, true, true, true, null);
}
private static Map map(String... aliasList) {
Map aliasMap = new HashMap();
for (String alias : aliasList) {
aliasMap.put(alias, alias);
}
return aliasMap;
}
/**
* @since 1.1.49.android
* @param clazz
* @param classModifiers
* @param aliasMap
* @param fieldOnly
* @param jsonTypeSupport
* @param jsonFieldSupport
* @param fieldGenericSupport
*/
public JavaBeanSerializer(Class> clazz, //
int classModifiers, //
Map aliasMap, //
boolean fieldOnly, //
boolean jsonTypeSupport, //
boolean jsonFieldSupport, //
boolean fieldGenericSupport, //
PropertyNamingStrategy propertyNamingStrategy
){
JSONType jsonType = jsonTypeSupport //
? clazz.getAnnotation(JSONType.class) //
: null;
String typeName = null, typeKey = null;
if (jsonType != null) {
features = SerializerFeature.of(jsonType.serialzeFeatures());
typeName = jsonType.typeName();
if (typeName.length() == 0) {
typeName = null;
} else {
for (Class> supperClass = clazz.getSuperclass()
; supperClass != null && supperClass != Object.class
; supperClass = supperClass.getSuperclass()) {
JSONType superJsonType = supperClass.getAnnotation(JSONType.class);
if (superJsonType == null) {
break;
}
typeKey = superJsonType.typeKey();
if (typeKey.length() != 0) {
break;
}
}
for (Class> interfaceClass : clazz.getInterfaces()) {
JSONType superJsonType = interfaceClass.getAnnotation(JSONType.class);
if (superJsonType != null) {
typeKey = superJsonType.typeKey();
if (typeKey.length() != 0) {
break;
}
}
}
if (typeKey != null && typeKey.length() == 0) {
typeKey = null;
}
}
if (propertyNamingStrategy == null) {
PropertyNamingStrategy typeNaming = jsonType.naming();
if (typeNaming != PropertyNamingStrategy.CamelCase) {
propertyNamingStrategy = typeNaming;
}
}
}
this.typeName = typeName;
this.typeKey = typeKey;
{
List fieldInfoList = TypeUtils.computeGetters(clazz, //
classModifiers, //
fieldOnly, //
jsonType, //
aliasMap, //
false, // sorted = false
jsonFieldSupport, //
fieldGenericSupport, //
propertyNamingStrategy);
List getterList = new ArrayList();
for (FieldInfo fieldInfo : fieldInfoList) {
FieldSerializer fieldDeser = new FieldSerializer(fieldInfo);
getterList.add(fieldDeser);
}
getters = getterList.toArray(new FieldSerializer[getterList.size()]);
}
String[] orders = null;
if (jsonType != null) {
orders = jsonType.orders();
}
if (orders != null && orders.length != 0) {
List fieldInfoList = TypeUtils.computeGetters(clazz, //
classModifiers, //
fieldOnly, //
jsonType, //
aliasMap, //
true, // sorted = true
jsonFieldSupport, //
fieldGenericSupport, //
propertyNamingStrategy);
List getterList = new ArrayList();
for (FieldInfo fieldInfo : fieldInfoList) {
FieldSerializer fieldDeser = new FieldSerializer(fieldInfo);
getterList.add(fieldDeser);
}
sortedGetters = getterList.toArray(new FieldSerializer[getterList.size()]);
} else {
FieldSerializer[] sortedGetters = new FieldSerializer[getters.length];
System.arraycopy(getters, 0, sortedGetters, 0, getters.length);
Arrays.sort(sortedGetters);
if (Arrays.equals(sortedGetters, getters)) {
this.sortedGetters = getters;
} else {
this.sortedGetters = sortedGetters;
}
}
}
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
SerializeWriter out = serializer.out;
if (object == null) {
out.writeNull();
return;
}
if ((serializer.context == null //
|| (serializer.context.features & SerializerFeature.DisableCircularReferenceDetect.mask) == 0) //
&& serializer.references != null //
&& serializer.references.containsKey(object)) {
serializer.writeReference(object);
return;
}
final FieldSerializer[] getters;
if ((out.features & SerializerFeature.SortField.mask) != 0) {
getters = this.sortedGetters;
} else {
getters = this.getters;
}
SerialContext parent = serializer.context;
// serializer.setContext(parent, object, fieldName, features);
if ((out.features & SerializerFeature.DisableCircularReferenceDetect.mask) == 0) {
serializer.context = new SerialContext(parent, object, fieldName, features);
if (serializer.references == null) {
serializer.references = new IdentityHashMap