64 lines
2.1 KiB
Java
64 lines
2.1 KiB
Java
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 <ISM extends Element> String serializeType(Class<ISM> 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 <ISM extends Element> String serializeTypeDefinitions(List<TypeDefinition> typeDefinitions) throws Exception{
|
|
String json = ISMapper.marshal(typeDefinitions);
|
|
return json;
|
|
|
|
}
|
|
|
|
public static List<TypeDefinition> deserializeTypeDefinitions(String json) throws Exception{
|
|
List<TypeDefinition> list = ISMapper.unmarshalList(TypeDefinition.class, json);
|
|
return list;
|
|
}
|
|
|
|
public static <ISM extends Element> TypeDefinition createTypeDefinition(Class<ISM> clz) {
|
|
TypeDefinition typeDefinition = TypeDefinitionImpl.getInstance(clz);
|
|
return typeDefinition;
|
|
}
|
|
|
|
public static String getType(Class<? extends Element> clz){
|
|
return getStaticStringFieldByName(clz, NAME, clz.getSimpleName());
|
|
}
|
|
|
|
public static String getStaticStringFieldByName(Class<? extends Element> 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;
|
|
}
|
|
}
|
|
}
|