All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.sdk.json.ext.FieldWrapper Maven / Gradle / Ivy

There is a newer version: 1.5.17
Show newest version
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);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy