Fixing json serialization/deserialization issues

This commit is contained in:
Luca Frosini 2019-10-28 17:36:09 +01:00
parent d69f0435bc
commit 45beeff1ef
13 changed files with 84 additions and 30 deletions

View File

@ -22,7 +22,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>

View File

@ -0,0 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8

View File

@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -5,6 +5,8 @@ package org.gcube.informationsystem.base.reference;
import java.util.Arrays;
import org.gcube.informationsystem.base.impl.properties.BasePropertyImpl;
import org.gcube.informationsystem.base.reference.properties.BaseProperty;
import org.gcube.informationsystem.context.impl.entities.ContextImpl;
import org.gcube.informationsystem.context.impl.relations.IsParentOfImpl;
import org.gcube.informationsystem.context.reference.entities.Context;
@ -27,13 +29,15 @@ import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.types.impl.TypeDefinitionImpl;
import org.gcube.informationsystem.types.impl.entities.EntityTypeDefinitionImpl;
import org.gcube.informationsystem.types.impl.properties.PropertyDefinitionImpl;
import org.gcube.informationsystem.types.impl.properties.PropertyTypeDefinitionImpl;
import org.gcube.informationsystem.types.impl.relations.RelationTypeDefinitionImpl;
import org.gcube.informationsystem.types.reference.TypeDefinition;
import org.gcube.informationsystem.types.reference.entities.EntityTypeDefinition;
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
import org.gcube.informationsystem.types.reference.properties.PropertyTypeDefiniton;
import org.gcube.informationsystem.types.reference.properties.PropertyTypeDefinition;
import org.gcube.informationsystem.types.reference.relations.RelationTypeDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -45,13 +49,16 @@ import org.slf4j.LoggerFactory;
*/
public enum AccessType {
PROPERTY_DEFINITION(PropertyDefinition.class, PropertyDefinition.NAME, PropertyDefinitionImpl.class, null),
PROPERTY_TYPE_DEFINITION(PropertyTypeDefiniton.class, PropertyTypeDefiniton.NAME, PropertyTypeDefinitionImpl.class, null),
BASE_PROPERTY(BaseProperty.class, BaseProperty.NAME, BasePropertyImpl.class, null),
PROPERTY(Property.class, Property.NAME, PropertyImpl.class, DummyProperty.class),
CONTEXT(Context.class, Context.NAME, ContextImpl.class, null),
IS_PARENT_OF(IsParentOf.class, IsParentOf.NAME, IsParentOfImpl.class, null),
PROPERTY_DEFINITION(PropertyDefinition.class, PropertyDefinition.NAME, PropertyDefinitionImpl.class, null),
TYPE_DEFINITION(TypeDefinition.class, TypeDefinition.NAME, TypeDefinitionImpl.class, null),
PROPERTY_TYPE_DEFINITION(PropertyTypeDefinition.class, PropertyTypeDefinition.NAME, PropertyTypeDefinitionImpl.class, null),
ENTITY_TYPE_DEFINITION(EntityTypeDefinition.class, EntityTypeDefinition.NAME, EntityTypeDefinitionImpl.class, null),
RELATION_TYPE_DEFINITION(RelationTypeDefinition.class, RelationTypeDefinition.NAME, RelationTypeDefinitionImpl.class, null),

View File

@ -1,7 +1,6 @@
package org.gcube.informationsystem.types;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.gcube.informationsystem.base.reference.ISManageable;
@ -9,9 +8,6 @@ import org.gcube.informationsystem.types.impl.TypeDefinitionImpl;
import org.gcube.informationsystem.types.reference.TypeDefinition;
import org.gcube.informationsystem.utils.ISMapper;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author Luca Frosini (ISTI - CNR)
*/
@ -19,7 +15,8 @@ public class TypeBinder {
private final static String NAME = "NAME";
public static <ISM extends ISManageable> String serializeTypeDefinition(TypeDefinition<ISM> typeDefinition) throws Exception{
@SuppressWarnings("rawtypes")
public static String serializeTypeDefinition(TypeDefinition typeDefinition) throws Exception{
/*
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(typeDefinition);
@ -33,23 +30,37 @@ public class TypeBinder {
return ISMapper.marshal(typeDefinition);
}
public static <ISM extends ISManageable> TypeDefinition<ISM> deserializeTypeDefinition(String json) throws Exception{
@SuppressWarnings("rawtypes")
public static TypeDefinition deserializeTypeDefinition(String json) throws Exception{
/*
ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unchecked")
TypeDefinition<ISM> readValue = mapper.readValue(json, TypeDefinition.class);
*/
TypeDefinition readValue = ISMapper.unmarshal(TypeDefinition.class, json);
return readValue;
}
public static <ISM extends ISManageable> String serializeTypeDefinitions(List<TypeDefinition<ISM>> typeDefinitions) throws Exception{
/*
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(typeDefinitions);
return json;
*/
String json = ISMapper.marshal(typeDefinitions);
return json;
}
public static <ISM extends ISManageable> List<TypeDefinition<ISM>> deserializeTypeDefinitions(String json) throws Exception{
@SuppressWarnings("rawtypes")
public static List<TypeDefinition> deserializeTypeDefinitions(String json) throws Exception{
/*
ObjectMapper mapper = new ObjectMapper();
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, TypeDefinition.class) ;
return mapper.readValue(json, type);
*/
List<TypeDefinition> list = ISMapper.unmarshalList(TypeDefinition.class, json);
return list;
}
public static <ISM extends ISManageable> TypeDefinition<ISM> createTypeDefinition(Class<ISM> clz) {

View File

@ -28,7 +28,7 @@ import com.fasterxml.jackson.annotation.JsonTypeName;
// @JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY)
@JsonTypeName(value=TypeDefinition.NAME)
public class TypeDefinitionImpl<ISM extends ISManageable> extends ISManageableImpl implements TypeDefinition<ISM> {
public abstract class TypeDefinitionImpl<ISM extends ISManageable> extends ISManageableImpl implements TypeDefinition<ISM> {
/**
* Generated Serial Version UID
@ -50,7 +50,9 @@ public class TypeDefinitionImpl<ISM extends ISManageable> extends ISManageableIm
Set<String> interfaceList = new HashSet<>();
if(type==baseClass){
interfaceList.add(topSuperClass);
if(topSuperClass!=null) {
interfaceList.add(topSuperClass);
}
return interfaceList;
}
@ -104,12 +106,14 @@ public class TypeDefinitionImpl<ISM extends ISManageable> extends ISManageableIm
} else if(BaseProperty.class.isAssignableFrom(clz)){
return new PropertyTypeDefinitionImpl(clz);
} else if(TypeDefinition.class.isAssignableFrom(clz)) {
return new TypeDefinitionImpl(clz);
return new DummyTypeDefinitionImpl(clz);
} else {
throw new RuntimeException("Serialization required");
}
}
protected TypeDefinitionImpl() {}
protected TypeDefinitionImpl(Class<ISM> clz) {
this.name = TypeBinder.getType(clz);
this.description = TypeBinder.getStaticStringFieldByName(clz, DESCRIPTION, "");

View File

@ -16,7 +16,7 @@ import org.gcube.informationsystem.utils.ISMapper;
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName(value=EntityTypeDefinition.NAME)
public class EntityTypeDefinitionImpl<E extends BaseEntity> extends TypeDefinitionImpl<E> implements EntityTypeDefinition<E> {
public final class EntityTypeDefinitionImpl<E extends BaseEntity> extends TypeDefinitionImpl<E> implements EntityTypeDefinition<E> {
/**
* Generated Serial Version UID
@ -25,6 +25,10 @@ public class EntityTypeDefinitionImpl<E extends BaseEntity> extends TypeDefiniti
private static final String VERTEX_CLASS_NAME = "V";
protected EntityTypeDefinitionImpl() {
super();
}
public EntityTypeDefinitionImpl(Class<E> clz) {
super(clz);

View File

@ -65,7 +65,9 @@ public final class PropertyDefinitionImpl implements PropertyDefinition {
return name;
}
protected PropertyDefinitionImpl() {
}
public PropertyDefinitionImpl(ISProperty propertyAnnotation, Method method) {
String name = propertyAnnotation.name().isEmpty()?getPropertyNameFromMethodName(method):propertyAnnotation.name();

View File

@ -3,22 +3,26 @@ package org.gcube.informationsystem.types.impl.properties;
import org.gcube.informationsystem.base.reference.properties.BaseProperty;
import org.gcube.informationsystem.types.impl.TypeDefinitionImpl;
import org.gcube.informationsystem.types.reference.TypeDefinition;
import org.gcube.informationsystem.types.reference.properties.PropertyTypeDefiniton;
import org.gcube.informationsystem.types.reference.properties.PropertyTypeDefinition;
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName(value=PropertyTypeDefiniton.NAME)
public class PropertyTypeDefinitionImpl<P extends BaseProperty> extends TypeDefinitionImpl<P> implements PropertyTypeDefiniton<P> {
@JsonTypeName(value=PropertyTypeDefinition.NAME)
public final class PropertyTypeDefinitionImpl<P extends BaseProperty> extends TypeDefinitionImpl<P> implements PropertyTypeDefinition<P> {
/**
* Generated Serial Version UID
*/
private static final long serialVersionUID = 7532701373450638829L;
protected PropertyTypeDefinitionImpl() {
super();
}
public PropertyTypeDefinitionImpl(Class<P> clz) {
super(clz);
this.superClasses = retrieveSuperClasses(clz, BaseProperty.class, clz == BaseProperty.class ? null : BaseProperty.NAME);
if(PropertyTypeDefiniton.class.isAssignableFrom(clz)) {
if(PropertyTypeDefinition.class.isAssignableFrom(clz)) {
this.superClasses.add(TypeDefinition.class.getSimpleName());
}
}

View File

@ -20,7 +20,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName(value=RelationTypeDefinition.NAME)
public class RelationTypeDefinitionImpl<Out extends BaseEntity, In extends BaseEntity, Rel extends BaseRelation<Out,In>>
public final class RelationTypeDefinitionImpl<Out extends BaseEntity, In extends BaseEntity, Rel extends BaseRelation<Out,In>>
extends TypeDefinitionImpl<Rel> implements RelationTypeDefinition<Out,In,Rel> {
/**
@ -35,6 +35,10 @@ public class RelationTypeDefinitionImpl<Out extends BaseEntity, In extends BaseE
@JsonInclude(JsonInclude.Include.NON_NULL)
protected String targetType;
protected RelationTypeDefinitionImpl() {
super();
}
public RelationTypeDefinitionImpl(Class<Rel> clz) {
super(clz);

View File

@ -9,8 +9,8 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(as = PropertyTypeDefinitionImpl.class)
public interface PropertyTypeDefiniton<P extends BaseProperty> extends TypeDefinition<P>, BaseProperty {
public interface PropertyTypeDefinition<P extends BaseProperty> extends TypeDefinition<P>, BaseProperty {
public static final String NAME = "PropertyTypeDefiniton"; // PropertyTypeDefiniton.class.getSimpleName();
public static final String NAME = "PropertyTypeDefinition"; // PropertyTypeDefinition.class.getSimpleName();
}

View File

@ -63,7 +63,13 @@ public abstract class ISMapper {
SchemaAction schemaAction = new ObjectMappingERAction(mapper);
try {
ISMDiscovery.manageISM(schemaAction);
List<Package> packages = new ArrayList<Package>();
for(AccessType accessType : accessTypes) {
Class<ISManageable> clz = accessType.getTypeClass();
packages.add(clz.getPackage());
}
ISMDiscovery.manageISM(schemaAction, packages.toArray(new Package[]{}));
} catch(Exception e) {
logger.error("Error registering types", e);
}

View File

@ -13,7 +13,6 @@ import org.gcube.informationsystem.base.reference.ISManageable;
import org.gcube.informationsystem.base.reference.entities.BaseEntity;
import org.gcube.informationsystem.base.reference.properties.BaseProperty;
import org.gcube.informationsystem.base.reference.relations.BaseRelation;
import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.types.annotations.ISProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -76,7 +75,7 @@ public class ISMDiscovery<ISM extends ISManageable> {
analizeISM(parent);
}
if(root == Property.class) {
if(root == BaseProperty.class) {
for(Method m : clz.getDeclaredMethods()) {
m.setAccessible(true);
@ -116,8 +115,9 @@ public class ISMDiscovery<ISM extends ISManageable> {
@SuppressWarnings("unchecked")
public static void manageISM(SchemaAction schemaAction, Package... packages) throws Exception {
ISMDiscovery<BaseProperty> propertyDiscovery = new ISMDiscovery<>(BaseProperty.class);
if(Objects.nonNull(packages))
if(Objects.nonNull(packages)) {
Arrays.stream(packages).forEach(p -> propertyDiscovery.addPackage(p));
}
propertyDiscovery.discover();
for(Class<BaseProperty> property : propertyDiscovery.getDiscovered()) {
logger.trace("Going to manage : {}", property);
@ -125,8 +125,9 @@ public class ISMDiscovery<ISM extends ISManageable> {
}
ISMDiscovery<BaseEntity> entityDiscovery = new ISMDiscovery<>(BaseEntity.class);
if(Objects.nonNull(packages))
if(Objects.nonNull(packages)) {
Arrays.stream(packages).forEach(p -> entityDiscovery.addPackage(p));
}
entityDiscovery.discover();
for(Class<BaseEntity> entity : entityDiscovery.getDiscovered()) {