org.zodiac.sdk.json.ext.FieldWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zodiac-sdk-json Show documentation
Show all versions of zodiac-sdk-json Show documentation
Zodiac SDK JSON(JavaScript Object Notation)
package org.zodiac.sdk.json.ext;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import org.zodiac.sdk.json.annotation.JSONNodeAttr;
import org.zodiac.sdk.json.annotation.JSONNodeName;
import org.zodiac.sdk.json.exception.JSONException;
import org.zodiac.sdk.json.util.StringUtil;
public class FieldWrapper {
public final Field field;
public final Class> type;
public final Type genericType;
private String name;
private String format;
private boolean serialize = true;
private boolean deserialize = true;
public FieldWrapper(Class> clz, Field f) {
field = f;
type = f.getType();
genericType = f.getGenericType();
JSONNodeName anno = f.getAnnotation(JSONNodeName.class);
if (anno != null) {
name = anno.value();
}
JSONNodeAttr attr = f.getAnnotation(JSONNodeAttr.class);
if(attr != null){
name = attr.name();
format = attr.format();
serialize = attr.serialize();
deserialize = attr.deserialize();
}
if (StringUtil.isEmpty(name)) {
name = field.getName();
}
}
@Deprecated
public String name(){
return name;
}
public String getName() {
return name;
}
public String getFormat() {
return format;
}
public boolean isDeserialize() {
return deserialize;
}
public boolean isSerialize() {
return serialize;
}
public void setValue(Object tObj, Object val) {
try {
field.set(tObj, val);
} catch (IllegalAccessException e) {
throw new JSONException(e);
}
}
public Object getValue(Object tObj) {
try {
return field.get(tObj);
} catch (IllegalAccessException ex) {
throw new JSONException(ex);
}
}
}