![JAR search and dependency download from the Maven repository](/logo.png)
com.alibaba.fastjson.serializer.AnnotationSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fastjson-to-easyjson Show documentation
Show all versions of fastjson-to-easyjson Show documentation
Adapter alibaba fastjson to other json libraries. the fastjson version: 1.2.58
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache, 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.gnu.org/licenses/lgpl-3.0.html
*
* 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 com.alibaba.fastjson.serializer;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.Map;
/**
* Created by wenshao on 10/05/2017.
*/
public class AnnotationSerializer implements ObjectSerializer {
public static AnnotationSerializer instance = new AnnotationSerializer();
private static volatile Class sun_AnnotationType = null;
private static volatile boolean sun_AnnotationType_error = false;
private static volatile Method sun_AnnotationType_getInstance = null;
private static volatile Method sun_AnnotationType_members = null;
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
Class objClass = object.getClass();
Class[] interfaces = objClass.getInterfaces();
if (interfaces.length == 1 && interfaces[0].isAnnotation()) {
Class annotationClass = interfaces[0];
if (sun_AnnotationType == null && !sun_AnnotationType_error) {
try {
sun_AnnotationType = Class.forName("sun.reflect.annotation.AnnotationType");
} catch (Throwable ex) {
sun_AnnotationType_error = true;
throw new JSONException("not support Type Annotation.", ex);
}
}
if (sun_AnnotationType == null) {
throw new JSONException("not support Type Annotation.");
}
if (sun_AnnotationType_getInstance == null && !sun_AnnotationType_error) {
try {
sun_AnnotationType_getInstance = sun_AnnotationType.getMethod("getInstance", Class.class);
} catch (Throwable ex) {
sun_AnnotationType_error = true;
throw new JSONException("not support Type Annotation.", ex);
}
}
if (sun_AnnotationType_members == null && !sun_AnnotationType_error) {
try {
sun_AnnotationType_members = sun_AnnotationType.getMethod("members");
} catch (Throwable ex) {
sun_AnnotationType_error = true;
throw new JSONException("not support Type Annotation.", ex);
}
}
if (sun_AnnotationType_getInstance == null || sun_AnnotationType_error) {
throw new JSONException("not support Type Annotation.");
}
Object type;
try {
type = sun_AnnotationType_getInstance.invoke(null, annotationClass);
} catch (Throwable ex) {
sun_AnnotationType_error = true;
throw new JSONException("not support Type Annotation.", ex);
}
Map members;
try {
members = (Map) sun_AnnotationType_members.invoke(type);
} catch (Throwable ex) {
sun_AnnotationType_error = true;
throw new JSONException("not support Type Annotation.", ex);
}
JSONObject json = new JSONObject(members.size());
Iterator> iterator = members.entrySet().iterator();
Map.Entry entry;
Object val = null;
while (iterator.hasNext()) {
entry = iterator.next();
try {
val = entry.getValue().invoke(object);
} catch (IllegalAccessException e) {
// skip
} catch (InvocationTargetException e) {
// skip
}
json.put(entry.getKey(), JSON.toJSON(val));
}
serializer.write(json);
return;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy