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"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </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> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </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 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.entities.ContextImpl;
import org.gcube.informationsystem.context.impl.relations.IsParentOfImpl; import org.gcube.informationsystem.context.impl.relations.IsParentOfImpl;
import org.gcube.informationsystem.context.reference.entities.Context; 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.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.model.reference.relations.Relation; 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.entities.EntityTypeDefinitionImpl;
import org.gcube.informationsystem.types.impl.properties.PropertyDefinitionImpl; import org.gcube.informationsystem.types.impl.properties.PropertyDefinitionImpl;
import org.gcube.informationsystem.types.impl.properties.PropertyTypeDefinitionImpl; import org.gcube.informationsystem.types.impl.properties.PropertyTypeDefinitionImpl;
import org.gcube.informationsystem.types.impl.relations.RelationTypeDefinitionImpl; 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.entities.EntityTypeDefinition;
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition; 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.gcube.informationsystem.types.reference.relations.RelationTypeDefinition;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -44,17 +48,20 @@ import org.slf4j.LoggerFactory;
* Enumerates the basic type names. * Enumerates the basic type names.
*/ */
public enum AccessType { public enum AccessType {
PROPERTY_DEFINITION(PropertyDefinition.class, PropertyDefinition.NAME, PropertyDefinitionImpl.class, null), BASE_PROPERTY(BaseProperty.class, BaseProperty.NAME, BasePropertyImpl.class, null),
PROPERTY_TYPE_DEFINITION(PropertyTypeDefiniton.class, PropertyTypeDefiniton.NAME, PropertyTypeDefinitionImpl.class, null),
PROPERTY(Property.class, Property.NAME, PropertyImpl.class, DummyProperty.class), PROPERTY(Property.class, Property.NAME, PropertyImpl.class, DummyProperty.class),
CONTEXT(Context.class, Context.NAME, ContextImpl.class, null), CONTEXT(Context.class, Context.NAME, ContextImpl.class, null),
IS_PARENT_OF(IsParentOf.class, IsParentOf.NAME, IsParentOfImpl.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), ENTITY_TYPE_DEFINITION(EntityTypeDefinition.class, EntityTypeDefinition.NAME, EntityTypeDefinitionImpl.class, null),
RELATION_TYPE_DEFINITION(RelationTypeDefinition.class, RelationTypeDefinition.NAME, RelationTypeDefinitionImpl.class, null), RELATION_TYPE_DEFINITION(RelationTypeDefinition.class, RelationTypeDefinition.NAME, RelationTypeDefinitionImpl.class, null),
ENTITY(Entity.class, Entity.NAME, EntityImpl.class, null), ENTITY(Entity.class, Entity.NAME, EntityImpl.class, null),
RESOURCE(Resource.class, Resource.NAME, ResourceImpl.class, DummyResource.class), RESOURCE(Resource.class, Resource.NAME, ResourceImpl.class, DummyResource.class),
FACET(Facet.class, Facet.NAME, FacetImpl.class, DummyFacet.class), FACET(Facet.class, Facet.NAME, FacetImpl.class, DummyFacet.class),

View File

@ -1,7 +1,6 @@
package org.gcube.informationsystem.types; package org.gcube.informationsystem.types;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.gcube.informationsystem.base.reference.ISManageable; 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.types.reference.TypeDefinition;
import org.gcube.informationsystem.utils.ISMapper; import org.gcube.informationsystem.utils.ISMapper;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@ -19,7 +15,8 @@ public class TypeBinder {
private final static String NAME = "NAME"; 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(); ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(typeDefinition); String json = mapper.writeValueAsString(typeDefinition);
@ -33,23 +30,37 @@ public class TypeBinder {
return ISMapper.marshal(typeDefinition); 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(); ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
TypeDefinition<ISM> readValue = mapper.readValue(json, TypeDefinition.class); TypeDefinition<ISM> readValue = mapper.readValue(json, TypeDefinition.class);
*/
TypeDefinition readValue = ISMapper.unmarshal(TypeDefinition.class, json);
return readValue; return readValue;
} }
public static <ISM extends ISManageable> String serializeTypeDefinitions(List<TypeDefinition<ISM>> typeDefinitions) throws Exception{ public static <ISM extends ISManageable> String serializeTypeDefinitions(List<TypeDefinition<ISM>> typeDefinitions) throws Exception{
/*
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(typeDefinitions); String json = mapper.writeValueAsString(typeDefinitions);
return json; 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(); ObjectMapper mapper = new ObjectMapper();
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, TypeDefinition.class) ; JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, TypeDefinition.class) ;
return mapper.readValue(json, type); 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) { 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) // @JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY)
@JsonTypeName(value=TypeDefinition.NAME) @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 * Generated Serial Version UID
@ -50,7 +50,9 @@ public class TypeDefinitionImpl<ISM extends ISManageable> extends ISManageableIm
Set<String> interfaceList = new HashSet<>(); Set<String> interfaceList = new HashSet<>();
if(type==baseClass){ if(type==baseClass){
interfaceList.add(topSuperClass); if(topSuperClass!=null) {
interfaceList.add(topSuperClass);
}
return interfaceList; return interfaceList;
} }
@ -104,12 +106,14 @@ public class TypeDefinitionImpl<ISM extends ISManageable> extends ISManageableIm
} else if(BaseProperty.class.isAssignableFrom(clz)){ } else if(BaseProperty.class.isAssignableFrom(clz)){
return new PropertyTypeDefinitionImpl(clz); return new PropertyTypeDefinitionImpl(clz);
} else if(TypeDefinition.class.isAssignableFrom(clz)) { } else if(TypeDefinition.class.isAssignableFrom(clz)) {
return new TypeDefinitionImpl(clz); return new DummyTypeDefinitionImpl(clz);
} else { } else {
throw new RuntimeException("Serialization required"); throw new RuntimeException("Serialization required");
} }
} }
protected TypeDefinitionImpl() {}
protected TypeDefinitionImpl(Class<ISM> clz) { protected TypeDefinitionImpl(Class<ISM> clz) {
this.name = TypeBinder.getType(clz); this.name = TypeBinder.getType(clz);
this.description = TypeBinder.getStaticStringFieldByName(clz, DESCRIPTION, ""); this.description = TypeBinder.getStaticStringFieldByName(clz, DESCRIPTION, "");

View File

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

View File

@ -65,7 +65,9 @@ public final class PropertyDefinitionImpl implements PropertyDefinition {
return name; return name;
} }
protected PropertyDefinitionImpl() {
}
public PropertyDefinitionImpl(ISProperty propertyAnnotation, Method method) { public PropertyDefinitionImpl(ISProperty propertyAnnotation, Method method) {
String name = propertyAnnotation.name().isEmpty()?getPropertyNameFromMethodName(method):propertyAnnotation.name(); 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.base.reference.properties.BaseProperty;
import org.gcube.informationsystem.types.impl.TypeDefinitionImpl; import org.gcube.informationsystem.types.impl.TypeDefinitionImpl;
import org.gcube.informationsystem.types.reference.TypeDefinition; 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; import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName(value=PropertyTypeDefiniton.NAME) @JsonTypeName(value=PropertyTypeDefinition.NAME)
public class PropertyTypeDefinitionImpl<P extends BaseProperty> extends TypeDefinitionImpl<P> implements PropertyTypeDefiniton<P> { public final class PropertyTypeDefinitionImpl<P extends BaseProperty> extends TypeDefinitionImpl<P> implements PropertyTypeDefinition<P> {
/** /**
* Generated Serial Version UID * Generated Serial Version UID
*/ */
private static final long serialVersionUID = 7532701373450638829L; private static final long serialVersionUID = 7532701373450638829L;
protected PropertyTypeDefinitionImpl() {
super();
}
public PropertyTypeDefinitionImpl(Class<P> clz) { public PropertyTypeDefinitionImpl(Class<P> clz) {
super(clz); super(clz);
this.superClasses = retrieveSuperClasses(clz, BaseProperty.class, clz == BaseProperty.class ? null : BaseProperty.NAME); 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()); this.superClasses.add(TypeDefinition.class.getSimpleName());
} }
} }

View File

@ -20,7 +20,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName(value=RelationTypeDefinition.NAME) @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> { 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) @JsonInclude(JsonInclude.Include.NON_NULL)
protected String targetType; protected String targetType;
protected RelationTypeDefinitionImpl() {
super();
}
public RelationTypeDefinitionImpl(Class<Rel> clz) { public RelationTypeDefinitionImpl(Class<Rel> clz) {
super(clz); super(clz);

View File

@ -9,8 +9,8 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(as = PropertyTypeDefinitionImpl.class) @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); SchemaAction schemaAction = new ObjectMappingERAction(mapper);
try { 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) { } catch(Exception e) {
logger.error("Error registering types", 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.entities.BaseEntity;
import org.gcube.informationsystem.base.reference.properties.BaseProperty; import org.gcube.informationsystem.base.reference.properties.BaseProperty;
import org.gcube.informationsystem.base.reference.relations.BaseRelation; 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.gcube.informationsystem.types.annotations.ISProperty;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -76,7 +75,7 @@ public class ISMDiscovery<ISM extends ISManageable> {
analizeISM(parent); analizeISM(parent);
} }
if(root == Property.class) { if(root == BaseProperty.class) {
for(Method m : clz.getDeclaredMethods()) { for(Method m : clz.getDeclaredMethods()) {
m.setAccessible(true); m.setAccessible(true);
@ -116,8 +115,9 @@ public class ISMDiscovery<ISM extends ISManageable> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void manageISM(SchemaAction schemaAction, Package... packages) throws Exception { public static void manageISM(SchemaAction schemaAction, Package... packages) throws Exception {
ISMDiscovery<BaseProperty> propertyDiscovery = new ISMDiscovery<>(BaseProperty.class); ISMDiscovery<BaseProperty> propertyDiscovery = new ISMDiscovery<>(BaseProperty.class);
if(Objects.nonNull(packages)) if(Objects.nonNull(packages)) {
Arrays.stream(packages).forEach(p -> propertyDiscovery.addPackage(p)); Arrays.stream(packages).forEach(p -> propertyDiscovery.addPackage(p));
}
propertyDiscovery.discover(); propertyDiscovery.discover();
for(Class<BaseProperty> property : propertyDiscovery.getDiscovered()) { for(Class<BaseProperty> property : propertyDiscovery.getDiscovered()) {
logger.trace("Going to manage : {}", property); logger.trace("Going to manage : {}", property);
@ -125,8 +125,9 @@ public class ISMDiscovery<ISM extends ISManageable> {
} }
ISMDiscovery<BaseEntity> entityDiscovery = new ISMDiscovery<>(BaseEntity.class); ISMDiscovery<BaseEntity> entityDiscovery = new ISMDiscovery<>(BaseEntity.class);
if(Objects.nonNull(packages)) if(Objects.nonNull(packages)) {
Arrays.stream(packages).forEach(p -> entityDiscovery.addPackage(p)); Arrays.stream(packages).forEach(p -> entityDiscovery.addPackage(p));
}
entityDiscovery.discover(); entityDiscovery.discover();
for(Class<BaseEntity> entity : entityDiscovery.getDiscovered()) { for(Class<BaseEntity> entity : entityDiscovery.getDiscovered()) {