package org.gcube.informationsystem.types; import java.lang.reflect.Field; import java.util.List; import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.types.impl.TypeDefinitionImpl; import org.gcube.informationsystem.types.reference.TypeDefinition; import org.gcube.informationsystem.utils.ISMapper; /** * @author Luca Frosini (ISTI - CNR) */ public class TypeBinder { private final static String NAME = "NAME"; public static String serializeTypeDefinition(TypeDefinition typeDefinition) throws Exception{ String json = ISMapper.marshal(typeDefinition); return json; } public static String serializeType(Class type) throws Exception{ TypeDefinition typeDefinition = createTypeDefinition(type); return ISMapper.marshal(typeDefinition); } public static TypeDefinition deserializeTypeDefinition(String json) throws Exception{ TypeDefinition readValue = ISMapper.unmarshal(TypeDefinition.class, json); return readValue; } public static String serializeTypeDefinitions(List typeDefinitions) throws Exception{ String json = ISMapper.marshal(typeDefinitions); return json; } public static List deserializeTypeDefinitions(String json) throws Exception{ List list = ISMapper.unmarshalList(TypeDefinition.class, json); return list; } public static TypeDefinition createTypeDefinition(Class clz) { TypeDefinition typeDefinition = TypeDefinitionImpl.getInstance(clz); return typeDefinition; } public static String getType(Class clz){ return getStaticStringFieldByName(clz, NAME, clz.getSimpleName()); } public static String getStaticStringFieldByName(Class type, String fieldName, String defaultValue){ Field field; try { field = type.getDeclaredField(fieldName); field.setAccessible(true); return (String) field.get(null); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { return defaultValue; } } }